Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define N 9
- struct process{
- char pid;
- int at;
- int et;
- float pri;
- int rem;
- int done;
- int ct;
- int wt;
- int tt;
- }pro[N] = { //{'pid', at, et},
- {'A', 0, 10},
- {'B', 0, 5},
- {'C', 0, 2},
- {'D', 0, 4},
- {'E', 10, 8},
- {'F', 10, 16},
- {'G', 10, 6},
- {'H', 20, 3},
- {'I', 20, 1}
- };
- void init(){ //initially, all processes are unfinished and their remaining and execution times are same.
- int i;
- for(i=0; i< N; i++){
- pro[i].done=0;
- pro[i].rem=pro[i].et;
- }
- }
- int nextProc(int time){
- int i, send=0;
- for(i = 0; i < N; i++)
- if(pro[i].done==0) break; //find first process that's not completed.
- if(i==N) return -1;
- send=i; //send will be returned at the end of function
- for(; i < N; i++){
- if(pro[i].done || pro[i].at > time) continue; //if the process is completed, skip it
- if(time==0||time==10||time==20)
- pro[i].pri=((float)(time-pro[i].at)/(float)pro[i].rem)+1; //calculated at t=0,10,20...
- if (pro[i].pri > pro[send].pri) send = i; //process with highest response time will be returned
- else if(pro[i].pri == pro[send].pri && pro[i].et < pro[send].et) send = i; //compare execution time
- else if(pro[i].et == pro[send].et && pro[i].at < pro[send].at) send = i; //compare arrival times
- else if(pro[i].at == pro[send].at && pro[i].pid < pro[send].pid) send = i; //compare pids
- }
- if(time==0||time==10||time==20){
- printf("\n\nAt t=%d, the processes already arrived but not completed are:-\n", time);
- for(i=0; i < N; i++)
- if(pro[i].done || pro[i].at > time) continue;
- else printf("%c(Pri: %.3f)",pro[i].pid, pro[i].pri);
- printf("\n\n");
- }
- return send;
- }
- void printResult(int sumWT, int sumTT){
- int i;
- 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);
- }
- int main(){
- int i, time = 0, sumWT = 0, sumTT = 0, curr;
- init();
- while(1){
- curr = nextProc(time); //process to be executed
- if(curr == -1) break; //if all processes have completed, print the result
- printf("At time=%d, %c gets CPU.(RR: %.3f, remaining: %d)\n", time, pro[curr].pid, pro[curr].pri, pro[curr].rem);
- pro[curr].rem--; //subtract one time unit from the process's remaining time
- time++;
- if(pro[curr].rem == 0){
- pro[curr].done=1; //if remaining time is 0, the process is completed.
- pro[curr].ct = time; //completed time is the time when process execution is completed
- pro[curr].tt = pro[curr].ct - pro[curr].at;
- pro[curr].wt = pro[curr].tt - pro[curr].et;
- printf("\n%c has finished executing at %d\n\n", pro[curr].pid, time);
- sumWT += pro[curr].wt;
- sumTT += pro[curr].tt;
- }
- }
- printResult(sumWT, sumTT);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement