-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathround_robin.c
65 lines (61 loc) · 1.81 KB
/
round_robin.c
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
#include <stdio.h>
struct process{
int pid;
int wtime;
int ttime;
int btime;
int btime_copy;
int occur;
};
void main(){
int totaltime = 0;
float twaittime = 0;
int timeslice = 0;
int totalbursttime = 0;
int iterator = 0;
int n;
int currentproc = 0;
printf("Enter number of process : ");
scanf("%d",&n);
printf("Enter timeslice : ");
scanf("%d",×lice);
iterator = timeslice ;
struct process p[n];
for(int i=0 ; i<n ; i++){
printf("Process %d burst time : ",i+1);
scanf("%d",&p[i].btime);
p[i].btime_copy = p[i].btime;
p[i].pid = i+1;
totalbursttime += p[i].btime;
p[i].occur = 0;
}
for(int i=0 ; i<totalbursttime ; i++){
//Checks if a single process is ran a full timeslice
//If true change the process to next one and reset the iterator
if((iterator % timeslice == 0 && i != 0)){
currentproc = (currentproc + 1)%n;
iterator = timeslice ;
}
//Check whether the process ended before full timeslice
//If true reset the timeslice and change process to next one
while(p[currentproc].btime_copy == 0){
currentproc = (currentproc + 1)%n;
iterator = timeslice ;
}
p[currentproc].occur ++ ;
totaltime++;
p[currentproc].btime_copy--;
// use this line to print the table thingy to see working
// printf(" %d ",p[currentproc].pid);
iterator--;
p[currentproc].wtime = i - (p[currentproc].occur);
p[currentproc].wtime += 1;
}
printf("\n");
printf("\nPID\tBURST TIME\tWAIT TIME\tTURNAROUND TIME\n");
for(int i=0 ; i<n ; i++){
printf("%d\t%d\t\t%d\t\t%d\n" , p[i].pid , p[i].btime , p[i].wtime , p[i].btime + p[i].wtime);
twaittime += p[i].wtime;
}
printf("Average wait time : %.2f \n",twaittime/n);
}