|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | +struct proc |
| 4 | +{ |
| 5 | + int id; |
| 6 | + int arrival; |
| 7 | + int burst; |
| 8 | + int rem; |
| 9 | + int wait; |
| 10 | + int finish; |
| 11 | + int turnaround; |
| 12 | + float ratio; |
| 13 | +}process[10]; |
| 14 | +struct proc temp; |
| 15 | + int no; |
| 16 | + int chkprocess(int); |
| 17 | + int nextprocess(); |
| 18 | + void roundrobin(int, int, int[], int[]); |
| 19 | + void srtf(int); |
| 20 | + main() |
| 21 | + { |
| 22 | + int n,tq,choice; |
| 23 | + int bt[10],st[10],i,j,k; |
| 24 | + for(; ;) |
| 25 | + { |
| 26 | + printf("Enter the choice \n"); |
| 27 | + printf(" 1. Round Robin\n 2. SRT\n 3. Exit \n"); |
| 28 | + scanf("%d",&choice); |
| 29 | + switch(choice) |
| 30 | + { |
| 31 | + case 1: |
| 32 | + printf("Round Robin scheduling algorithm\n"); |
| 33 | + printf("Enter number of processes:\n"); |
| 34 | + scanf("%d",&n); |
| 35 | + printf("Enter burst time for sequences:"); |
| 36 | + for(i=0;i<n;i++) |
| 37 | + { |
| 38 | + scanf("%d",&bt[i]); |
| 39 | + st[i]=bt[i]; |
| 40 | + } |
| 41 | + printf("Enter time quantum:"); |
| 42 | + scanf("%d",&tq); |
| 43 | + roundrobin(n,tq,st,bt); |
| 44 | + break; |
| 45 | + case 2: |
| 46 | + printf("\n \n ---SHORTEST REMAINING TIME NEXT---\n \n "); |
| 47 | + printf("\n \n Enter the number of processes: "); |
| 48 | + scanf("%d", &n); |
| 49 | + srtf(n); |
| 50 | + break; |
| 51 | + case 3: exit(0); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + void roundrobin(int n,int tq,int st[],int bt[]) |
| 56 | + { |
| 57 | + int time=0; |
| 58 | + int tat[10],wt[10],i,count=0,swt=0,stat=0,temp1,sq=0,j,k; |
| 59 | + float awt=0.0,atat=0.0; |
| 60 | + while(1) |
| 61 | + { |
| 62 | + for(i=0,count=0;i<n;i++) |
| 63 | + { |
| 64 | + temp1=tq; |
| 65 | + if(st[i]==0) |
| 66 | + { |
| 67 | + count++; |
| 68 | + |
| 69 | + continue; |
| 70 | + } |
| 71 | + if(st[i]>tq) |
| 72 | + st[i]=st[i]-tq; |
| 73 | + else |
| 74 | + if(st[i]>=0) |
| 75 | + { |
| 76 | + temp1=st[i]; |
| 77 | + st[i]=0; |
| 78 | + } |
| 79 | + sq=sq+temp1; |
| 80 | + tat[i]=sq; |
| 81 | + } |
| 82 | + if(n==count) |
| 83 | + |
| 84 | + break; |
| 85 | + } |
| 86 | + for(i=0;i<n;i++) |
| 87 | + |
| 88 | + { |
| 89 | + wt[i]=tat[i]-bt[i]; |
| 90 | + |
| 91 | + swt=swt+wt[i]; |
| 92 | + stat=stat+tat[i]; |
| 93 | + } |
| 94 | + awt=(float)swt/n; |
| 95 | + atat=(float)stat/n; |
| 96 | + printf("Process_no Burst time Wait time Turn around time\n"); |
| 97 | + for(i=0;i<n;i++) |
| 98 | + printf("%d\t\t%d\t\t%d\t\t%d\n",i+1,bt[i],wt[i],tat[i]); |
| 99 | + printf("Avg wait time is %f\n Avg turn around time is %f\n",awt,atat); |
| 100 | + } |
| 101 | + int chkprocess(int s) |
| 102 | + { |
| 103 | + int i; |
| 104 | + for(i = 1; i <= s; i++) |
| 105 | + { |
| 106 | + if(process[i].rem != 0) |
| 107 | + return 1; |
| 108 | + } |
| 109 | + return 0; |
| 110 | + } |
| 111 | + |
| 112 | + int nextprocess() |
| 113 | + { |
| 114 | + int min, l, i; |
| 115 | + min = 32000; |
| 116 | + for(i = 1; i <= no; i++) |
| 117 | + { |
| 118 | + if( process[i].rem!=0 && process[i].rem < min) |
| 119 | + { |
| 120 | + min = process[i].rem; |
| 121 | + l = i; |
| 122 | + } |
| 123 | + } |
| 124 | + return l; |
| 125 | + } |
| 126 | +void srtf(int n) |
| 127 | +{ |
| 128 | +int i,j,k,time=0; |
| 129 | +float tavg,wavg; |
| 130 | +for(i = 1; i <= n; i++) |
| 131 | +{ |
| 132 | +process[i].id = i; |
| 133 | +printf("\n\nEnter the arrival time for process %d: ", i); |
| 134 | +scanf("%d", &(process[i].arrival)); |
| 135 | +printf("Enter the burst time for process %d: ", i); |
| 136 | +scanf("%d", &(process[i].burst)); |
| 137 | +process[i].rem = process[i].burst; |
| 138 | +} |
| 139 | +for(i = 1; i <= n; i++) |
| 140 | +{ |
| 141 | +for(j = i + 1; j <= n; j++) |
| 142 | +{ |
| 143 | +if(process[i].arrival > process[j].arrival) |
| 144 | +{ |
| 145 | +temp = process[i]; |
| 146 | +process[i] = process[j]; |
| 147 | +process[j] = temp; |
| 148 | +} |
| 149 | +} |
| 150 | +} |
| 151 | +no = 0; |
| 152 | +j = 1; |
| 153 | +while(chkprocess(n) == 1) |
| 154 | +{ |
| 155 | +if(process[no+1].arrival>time) |
| 156 | +{ |
| 157 | +time++; |
| 158 | +continue; |
| 159 | +} |
| 160 | +if(process[no + 1].arrival == time) |
| 161 | +{ |
| 162 | +while(process[no+1].arrival==time) |
| 163 | +no++; |
| 164 | +if(process[j].rem==0) |
| 165 | +process[j].finish=time; |
| 166 | +j = nextprocess(); |
| 167 | +} |
| 168 | +if(process[j].rem != 0) |
| 169 | +{ |
| 170 | +process[j].rem--; |
| 171 | +for(i = 1; i <= no; i++) |
| 172 | +{ |
| 173 | +if(i != j && process[i].rem != 0) |
| 174 | +process[i].wait++; |
| 175 | +} |
| 176 | +} |
| 177 | +else |
| 178 | +{ |
| 179 | +process[j].finish = time; |
| 180 | +j=nextprocess(); |
| 181 | +time--; |
| 182 | +k=j; |
| 183 | +} |
| 184 | +time++; |
| 185 | +} |
| 186 | +process[k].finish = time; |
| 187 | +printf("\n\n\t\t\t---SHORTEST REMAINING TIME FIRST---"); |
| 188 | +printf("\n\n Process Arrival Burst Waiting Finishing turnaround Tr/Tb \n"); |
| 189 | +printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time", |
| 190 | +"time", "time"); |
| 191 | +for(i = 1; i <= n; i++) |
| 192 | +{ |
| 193 | +process[i].turnaround = process[i].wait + process[i].burst; |
| 194 | +process[i].ratio = (float)process[i].turnaround / (float)process[i].burst; |
| 195 | +printf("%5d %8d %7d %8d %10d %9d %10.1f ", process[i].id, process[i].arrival, |
| 196 | +process[i].burst, process[i].wait, process[i].finish, process[i].turnaround, |
| 197 | +process[i].ratio); |
| 198 | +tavg=tavg+ process[i].turnaround; |
| 199 | +wavg=wavg+process[i].wait; |
| 200 | +printf("\n\n"); |
| 201 | +} |
| 202 | +tavg=tavg/n; |
| 203 | +wavg=wavg/n; |
| 204 | +printf("tavg=%f\t wavg=%f\n",tavg,wavg); |
| 205 | +} |
0 commit comments