Priority Scheduling Implementation
Priority Scheduling Implementation
SCHEDULING
Scheduling is the process of arranging, controlling and optimizing work in a production
process or manufacturing process. Scheduling is used to allocate resources.
1. Preemptive Priority Scheduling: If the new process arrived at the ready queue
has a higher priority than the currently running process, the CPU is preempted,
which means the processing of the current process is stopped and the incoming
new process with higher priority gets the CPU for its execution.
2. Non-Preemptive Priority Scheduling: In case of non-preemptive priority
scheduling algorithm if a new process arrives with a higher priority than the
current running process, the incoming process is put at the head of the ready
queue, which means after the execution of the current process it will be processed.
4.Waiting time: Time spent by the processes in the ready queue waiting for its turn to get
on CPU.
But in case of priority scheduling if new higher priority processes keep coming in
the ready queue then the processes waiting in the ready queue with lower priority
might have to wait for long time before getting the CPU for execution.
In 1973, when the IBM 7904 machine was shut down at MIT, a low-priority
process was found which was submitted in 1967 and had not yet been run.
To prevent starvation of any process, aging concept can be used where priorities
are kept on increasing the low-priority process based on the its waiting time. Aging
process ensures that no process will have to wait for indefinite time for getting
CPU time for processing.
2. If the system crashes eventually, then all the processes having low priority which
are not finished yet, also get lost.
IMPLEMENTATION:
n: number of processes
ft and st are temporary array used for calculating turn around time
temp:temporary variable
p: priority
CODE
#include<stdio.h>
#include<string.h>
int main()
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
scanf("%d",&n);
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
if(p[i]<p[j])
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\n%s\t\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
return 0;
}
OUTPUT
CONCLUSION
Priority scheduling is a non-pre emptive algorithm and one of the most commonly
used scheduling algorithms in batch operating systems. Each process is assigned with
a priority. Process with the highest priority is executed first and so on. Processes with
same priority are executed on first come first served (FCFS) basis. Priority can be
decided based on memory requirements, time requirements or any other resource
requirements.