Priority Scheduling program in C
Here you will learn priority scheduling and the example code of priority scheduling program in c programming.
Priority Scheduling Algorithm
Priority scheduling algorithm is a method of scheduling processes based on priority levels. Each process is assigned a priority, and the scheduler allocates the CPU to the process with the highest priority. First come first served (FCFS) scheduling is used when two processes have the same priority.
It is normally implemented in Operating Systems to schedule multiple process entering the CPU for execution. This code is for Priority Scheduling Non – Preemptive Algorithm in C Programming.
In Short:- Priority scheduling is a type of scheduling algorithm where each process is assigned a priority. The highest priority process is executed first. If two processes have the same priority, then they are executed based on the first come, first served principle.
Types of Priority Scheduling?
Priority Scheduling Preemptive :- In priority scheduling preemptive , the scheduler can interrupt a running process if a higher priority process arrives. This ensures that the most important processes are always given the CPU first.
Priority Scheduling Non-Preemptive :- In priority scheduling non-preemptive , the scheduler will only run a new process if the current process is finished. This can lead to lower priority processes never getting the CPU, which is why preemptive priority scheduling is generally preferred.
Priority Scheduling program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | //Priority Scheduling program in C #include<stdio.h> #include<conio.h> void main() { int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i; printf("\nEnter the number of process : "); scanf("%d",&n); printf("\n Enter process : time priorities n"); for(i=0;i<n;i++) { printf("\nProcess no %d : ",i+1); scanf("%d %d",&pt[i],&pp[i]); p[i]=i+1; } for(i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if(pp[i]<pp[j]) { x=pp[i]; pp[i]=pp[j]; pp[j]=x; x=pt[i]; pt[i]=pt[j]; pt[j]=x; x=p[i]; p[i]=p[j]; p[j]=x; } } } w[0]=0; awt=0; t[0]=pt[0]; atat=t[0]; for(i=1;i<n;i++) { w[i]=t[i-1]; awt+=w[i]; t[i]=w[i]+pt[i]; atat+=t[i]; } printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n"); for(i=0;i<n;i++) printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]); awt/=n; atat/=n; printf("\n Average Wait Time : %d \n",awt); printf("\n Average Turn Around Time : %d \n",atat); getch(); } |
Output
Enter the number of process : 4
Enter process : time priorities
Process no 1 : 3
1
Process no 2 : 4
2
Process no 3 : 5
3
Process no 4 : 6
4
Job | Brust Time | Wait Time | Turn Around Time | Priority |
4 | 6 | 0 | 6 | 4 |
3 | 5 | 6 | 11 | 3 |
2 | 4 | 11 | 15 | 2 |
1 | 3 | 15 | 18 | 1 |
Average Wait Time : 8
Average Turn Around Time : 12
Read Also