Priority Scheduling Algorithm C and C++ Programming Code with Gantt Chart .
C++ Program Code:
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
#include <bits/stdc++.h> using namespace std; const int N=100005; struct process { int priority; int id; int burst_time; int arrival_time; int waiting_time; int finishing_time; int turn_around_time; }; int n; process P[N]; bool operator<(process A, process B) { if(A.priority < B.priority) { return true; } if(A.priority == B.priority) { return A.arrival_time < B.arrival_time; } return false; } void Priority(void) { sort(P, P+n); double total_waiting_time = 0.0; double total_turn_around_time = 0.0; for(int i=0; i<n; i++) { P[i].finishing_time = P[i-1].finishing_time + P[i].burst_time; P[i].turn_around_time = P[i].finishing_time - P[i].arrival_time; P[i].waiting_time = P[i].turn_around_time - P[i].burst_time; total_waiting_time += P[i].waiting_time; total_turn_around_time += P[i].turn_around_time; } cout<<fixed<<setprecision(2); cout<<"Average Waiting Time: "<<(total_waiting_time/n)<<"\n"; cout<<"Average Turn Around Time: "<<(total_turn_around_time/n)<<"\n"; return; } int main() { cout<<"Number of P: "; cin>>n; cout<<"Process Ids:\n"; for(int i=0; i<n; i++) cin>>P[i].id; cout<<"Process Burst Times:\n"; for(int i=0; i<n; i++) cin>>P[i].burst_time; cout<<"Process Arrival Times:\n"; for(int i=0; i<n; i++) cin>>P[i].arrival_time; cout<<"Process Priorities:\n"; for(int i=0; i<n; i++) cin>>P[i].priority; Priority(); return 0; } /** 5 1 2 3 4 5 10 1 2 1 5 0 0 0 0 0 3 1 4 5 2 */ |
C Program Code:
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include<stdio.h> int main() { int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat; printf("Enter Total Number of Process:"); scanf("%d",&n); printf("\nEnter Burst Time and Priority\n"); for(i=0;i<n;i++) { printf("\nP[%d]\n",i+1); printf("Burst Time:"); scanf("%d",&bt[i]); printf("Priority:"); scanf("%d",&pr[i]); p[i]=i+1; //contains process number } //sorting burst time, priority and process number in ascending order using selection sort for(i=0;i<n;i++) { pos=i; for(j=i+1;j<n;j++) { if(pr[j]<pr[pos]) pos=j; } temp=pr[i]; pr[i]=pr[pos]; pr[pos]=temp; temp=bt[i]; bt[i]=bt[pos]; bt[pos]=temp; temp=p[i]; p[i]=p[pos]; p[pos]=temp; } wt[0]=0; //waiting time for first process is zero //calculate waiting time for(i=1;i<n;i++) { wt[i]=0; for(j=0;j<i;j++) wt[i]+=bt[j]; total+=wt[i]; } avg_wt=total/n; //average waiting time total=0; printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"); for(i=0;i<n;i++) { tat[i]=bt[i]+wt[i]; //calculate turnaround time total+=tat[i]; printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]); } avg_tat=total/n; //average turnaround time printf("\n\nAverage Waiting Time=%d",avg_wt); printf("\nAverage Turnaround Time=%d\n",avg_tat); return 0; } |
Output: