Priority CPU Scheduling With Different Arrival Time
Priority CPU Scheduling With Different Arrival Time
Priority CPU Scheduling With Different Arrival Time
Examples –
Input :
process no-> 1 2 3 4 5
arrival time-> 0 1 3 2 4
burst time-> 3 6 1 2 4
priority-> 3 4 9 7 8
Output :
Process_no arrival_time Burst_time Complete_time
Turn_Around_Time Waiting_Time
1 0 3 3 3
0
2 1 6 9 8
2
3 3 1 16 13
12
4 2 2 11 9
7
5 4 4 15 11
7
Average Waiting Time is : 5.6
Average Turn Around time is : 8.8
Recommended: Please try your approach on {IDE} first, before moving on to the
solution.
C++
Java
Python3
3. apply fcfs
*/
#include <bits/stdc++.h>
#define totalprocess 5
struct process
int at,bt,pr,pno;
};
process proc[50];
/*
*/
if(a.at == b.at)
return a.pr<b.pr;
else
return a.at<b.at;
{
// declaring service array that stores cumulative burst time
int service[50];
service[0] = proc[0].at;
wt[0]=0;
for(int i=1;i<totalprocess;i++)
service[i]=proc[i-1].bt+service[i-1];
wt[i]=service[i]-proc[i].at;
if(wt[i]<0)
{
wt[i]=0;
}
for(int i=0;i<totalprocess;i++)
tat[i]=proc[i].bt+wt[i];
void findgc()
double wavg=0,tavg=0;
get_wt_time(wt);
get_tat_time(tat,wt);
int stime[50],ctime[50];
stime[0] = proc[0].at;
ctime[0]=stime[0]+tat[0];
for(int i=1;i<totalprocess;i++)
{
stime[i]=ctime[i-1];
ctime[i]=stime[i]+tat[i]-wt[i];
}
cout<<"Process_no\tStart_time\tComplete_time\tTurn_Around_Time\
tWaiting_Time"<<endl;
for(int i=0;i<totalprocess;i++)
{
wavg += wt[i];
tavg += tat[i];
cout<<proc[i].pno<<"\t\t"<<
stime[i]<<"\t\t"<<ctime[i]<<"\t\t"<<
tat[i]<<"\t\t\t"<<wt[i]<<endl;
}
cout<<wavg/(float)totalprocess<<endl;
cout<<tavg/(float)totalprocess<<endl;
int main()
int arrivaltime[] = { 1, 2, 3, 4, 5 };
int bursttime[] = { 3, 5, 1, 7, 4 };
int priority[] = { 3, 4, 1, 7, 8 };
for(int i=0;i<totalprocess;i++)
proc[i].at=arrivaltime[i];
proc[i].bt=bursttime[i];
proc[i].pr=priority[i];
proc[i].pno=i+1;
}
//Using inbuilt sort function
sort(proc,proc+totalprocess,comp);
findgc();
return 0;
Output:
Process_no Start_time Complete_time Turn_Around_Time Waiting_Time
1 1 4 3 0
2 5 10 8 3
3 4 5 2 1
4 10 17 13 6
5 17 21 16 12
Average Waiting Time is : 4.4
Average Turn Around time is : 8.4
Time Complexity: O(N * logN), where N is the total number of processes.
Auxiliary Space: O(N)
This article is contributed by Amit Verma . If you like GeeksforGeeks and would like to
contribute, you can also write an article using contribute.geeksforgeeks.org or mail your
article to contribute@geeksforgeeks.org. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.