forked from Akriti3011/OS-Mini-Project2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs.c
More file actions
136 lines (112 loc) · 3.77 KB
/
Copy pathfcfs.c
File metadata and controls
136 lines (112 loc) · 3.77 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
FILE *file = fopen(argv[1], "r");
int i=0;
char **process;
float *at;
float *bt;
float *interrupt;
float *wt;
float *priority;
int size=1;
int temp;
float current_time=0,avg_wt=0,avg_tat=0;
// initial allocation
process = (char**)calloc(size,sizeof(char*));
for(i=0;i<size;i++){
process[i]=(char*)calloc(3,sizeof(char));
}
at = (float*)calloc(size,sizeof(float));
bt = (float*)calloc(size,sizeof(float));
interrupt = (float*)calloc(size,sizeof(float));
wt = (float*)calloc(size,sizeof(float));
priority = (float*)calloc(size,sizeof(float));
size=0;
if (file == NULL){
printf("File does not exists \n");
return 1;
}
char id[2];
float a[5];
int j;
while(1){ //end of file condition
int k=fscanf(file, "%s", id);
if(id[0]=='x'){
printf("Reached EOF\n");
break;
}
size++;
if(size!=1){
at = (float*)realloc(at, sizeof(float)*(size));
bt = (float*)realloc(bt, sizeof(float)*(size));
interrupt = (float*)realloc(interrupt, sizeof(float)*(size));
wt = (float*)realloc(wt, sizeof(float)*(size));
priority = (float*)realloc(priority, sizeof(float)*(size));
process = (char **) realloc(process,sizeof(char*)*(size));
process[size-1] =(char*)calloc(3,sizeof(char));
}
for(j=0;j<5;j++)
fscanf(file,"%f",&a[j]);
at[size-1]=a[0];
bt[size-1]=a[1];
interrupt[size-1]=a[2];
wt[size-1]=a[3];
priority[size-1]=a[4];
process[size-1][0]=id[0];process[size-1][1]=id[1];process[size-1][2]='\0';
}
for(i=1;i<size;i++){ //actual arrival times
at[i]=at[i]+at[i-1];
}
for(i=0;i<size;i++){ //total burst time
bt[i]= bt[i] + interrupt[i]+wt[i];
}
for (i=0;i<size-1;i++){ //sort according to arrival time
for (j=0;j<size-i-1;j++){
if(at[j]>at[j+1]){
temp=process[j+1];
process[j+1]=process[j];
process[j]=temp;
temp=at[j+1];
at[j+1]=at[j];
at[j]=temp;
temp=bt[j+1];
bt[j+1]=bt[j];
bt[j]=temp;
}
}
}
float w_time[size],tat[size];
for (i = 0; i < size; ++i)
{
if(at[i]<=current_time)
{
w_time[i]=current_time-at[i];
tat[i]=bt[i]+w_time[i];
current_time=current_time+bt[i];
}
else
{
current_time=at[i];
w_time[i]=current_time-at[i];
tat[i]=bt[i]+w_time[i];
current_time=current_time+bt[i];
}
}
printf("Process\tWaiting_Time\tTurn_a_time\n");
for (i = 0; i <size; i++)
{
printf("%s\t %f\t %f\n",process[i],w_time[i],tat[i]);
}
for(i=0;i<size;i++)
{
avg_wt+=w_time[i]; //Calculating TurnAround Time (TAT)
avg_tat+=tat[i];
}
avg_wt=avg_wt/size;
avg_tat=avg_tat/size;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
return 0;
}