Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define N 12
- struct process{
- char pid;
- int at;
- int et;
- int ct;
- int wt;
- int tt;
- }pro[N] = { //{'pid', at, et},
- {'A', 0, 10},
- {'B', 0, 3},
- {'C', 2, 2},
- {'D', 2, 1},
- {'E', 2, 4},
- {'F', 10, 5},
- {'G', 10, 7},
- {'H', 15, 8},
- {'I', 20, 10},
- {'J', 30, 9},
- {'K', 40, 3},
- {'L', 40, 2}
- };
- int main(){
- int i, time = 0, sumWT = 0, sumTT = 0, sj = 0, done[N] = {0};
- while(1){
- for(i = 0; i < N; i++) if(!done[i] && time >= pro[i].at){sj = i; break;} //select first unexecuted process
- if(i == N) break; //Exit loop if all processes are done
- for(++i;i < N; i++){
- if(done[i] || time < pro[i].at) continue; //ignore unarrived & completed processes
- if(pro[i].et < pro[sj].et) sj = i; //if the execution time is better, make it sj
- else if((pro[i].et == pro[sj].et) && (pro[i].at < pro[sj].at)) sj = i; //if et is same, see at
- else if((pro[i].at == pro[sj].at) && (pro[i].pid < pro[sj].pid)) sj = i; //if at is same, see pid
- }
- if(time < pro[sj].at) {time++; continue;}//idle time
- done[sj] = 1;//sj has been executed
- pro[sj].ct = time + pro[sj].et;
- time = pro[sj].ct; //update time
- pro[sj].wt = pro[sj].ct - (pro[sj].at + pro[sj].et);
- pro[sj].tt = pro[sj].ct - pro[sj].at;
- sumWT += pro[sj].wt;
- sumTT += pro[sj].tt;
- }
- printf("\tpid\tAT\tET\tCT\tWT\tTT\n");
- for(i = 0; i < N; i++) printf("\t%c\t%d\t%d\t%d\t%d\t%d\n", pro[i].pid, pro[i].at, pro[i].et, 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);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement