Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define N 12
- #define AF 25
- struct process{
- char pid;
- int at;
- int et;
- int pri;
- int ct;
- int wt;
- int cwt;
- int tt;
- }pro[N] = {
- {'A', 0, 10, 5},
- {'B', 0, 3, 1},
- {'C', 2, 2, 3},
- {'D', 2, 1, 6},
- {'E', 2, 4, 3},
- {'F', 10, 5, 2},
- {'G', 10, 7, 3},
- {'H', 15, 8, 1},
- {'I', 20, 10, 2},
- {'J', 30, 9, 4},
- {'K', 40, 3, 5},
- {'L', 40, 2, 4}
- };
- int done[N] = {0}, time = 0, sumWT = 0, sumTT = 0;
- int calcNow(){
- int i, now;
- for(i = 0; i < N; i++) if(!done[i] && time >= pro[i].at){now = i; break;}
- for(; i < N; i++){
- if(done[i] || time < pro[i].at) continue;
- if(pro[i].pri < pro[now].pri) now = i;
- else if(pro[i].et == pro[now].et && pro[i].et < pro[now].et) now = i;
- else if((pro[i].et == pro[now].et) && (i < now)) now = i;
- }
- if(time < pro[now].at) return -1;
- return now;
- }
- void execute(int now){
- done[now] = 1;
- printf("%c executes from %d to %d\n", pro[now].pid, time, time+pro[now].et);
- pro[now].ct = time + pro[now].et;
- time = pro[now].ct;
- pro[now].wt = pro[now].ct - (pro[now].at + pro[now].et);
- pro[now].tt = pro[now].ct - pro[now].at;
- sumWT += pro[now].wt;
- sumTT += pro[now].tt;
- }
- void updatePriorities(){
- int i;
- for(i = 0; i < N; i++){
- if(!done[i] && time >= pro[i].at) pro[i].cwt = time - pro[i].at;
- if(pro[i].cwt >= 25 && pro[i].pri){
- pro[i].cwt = 0;
- pro[i].pri -= 1;
- printf("Priority of %c is now %d\n", pro[i].pid, pro[i].pri);
- }
- }
- }
- void printTable(){
- int i;
- printf("\tpid\tAT\tET\tPRI\tCT\tWT\tTT\n");
- for(i = 0; i < N; i++) printf("\t%c\t%d\t%d\t%d\t%d\t%d\t%d\n", pro[i].pid, pro[i].at, pro[i].et, pro[i].pri, pro[i].ct, pro[i].wt, pro[i].tt);
- printf("\nThe average waiting time is %.2f\nThe average turn around time is %.2f\n", sumWT/(float)N, sumTT/(float)N);
- }
- int main(){
- int i, now = 0;
- while(1){
- now = calcNow();
- if(now < 0){time++; continue;}
- execute(now);
- updatePriorities(); //commenting out this line gives Priority Scheduling without Aging
- for(i = 0; i < N; i++) if(!done[i]) break;
- if(i == N) break;
- }
- printTable();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement