-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRR.c
104 lines (86 loc) · 2.32 KB
/
RR.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define BUFFER_SIZE 1024
int main()
{
char buffer[BUFFER_SIZE];
char *record, *line;
int i = 0, j = 0;
FILE *fstream = fopen("Input2.in", "r");
if (fstream == NULL)
{
printf("\n file opening failed ");
return -1;
}
int time_quantum = atoi(fgets(buffer, sizeof(buffer), fstream));
int totalTickets = atoi(fgets(buffer, sizeof(buffer), fstream));
// printf("%d\n%d\n",quantum,totalTickets);
int count,n,time,remain,flag=0;
int wait_time=0,turnaround_time=0,at[50],bt[50],rt[50];
int idx = 0;
while ((line = fgets(buffer, sizeof(buffer), fstream)) != NULL)
{
record = strtok(line, ",");
int i;
int pid;
int arrivalTime;
int burstTime;
int tickets;
for (i = 0; record != NULL; ++i)
{
if (i == 0)
pid = atoi(record);
if (i == 1)
arrivalTime = atoi(record);
if (i == 2)
burstTime = atoi(record);
if (i == 3)
tickets = atoi(record);
record = strtok(NULL, ",");
}
at[idx]=arrivalTime;
bt[idx]=burstTime;
rt[idx]=bt[idx];
idx++;
}
n=idx;
remain=n;
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
int prev = 0 ;
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
prev= time;
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
printf("Time %d : P %d Entering quantum\n", time,count+1);
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("Time %d : P %d Entering quantum\n", prev,count+1);
printf("Time %d : P %d Done Turn around : %d Waiting time : %d\n",time,count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f\n",turnaround_time*1.0/n);
return 0; //
}