-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.c
425 lines (322 loc) · 10.7 KB
/
simulator.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include "insoutput.c"
#include <time.h>
#include "creatingreadyqueue.c"
int choice;
pthread_mutex_t var; //mutex variable
FILE *f;//file pointer of log file
int checkthread[5];//variable checks if thread is completed or not
int times[4]={0};
int turn=0;
int StartNext(int tid) //choose the next thread to run
{
int i=tid;
for(i = (tid + 1) % 4; times[i] == 0; i = (i + 1) % 4)
{
if(i == tid) //if every thread has finished
return;
}
turn = i;
}
void *Run(void *tid) //thread function
{
int insno=1;
int i = (int)tid;
int output;
char arr[50];
char line[BUFSIZ];
FILE *fp;
int status;
time_t start=time(0);
double timetaken,waiting_time;
fp=fopen(ready_queue[i].process_name,"r");
while(times[i]!=0)
{
while(turn!=i);
if(times[i]>=1)
{
fgets(line,sizeof(line),fp);
output=parser(line);
printf("output of process%d of %s is %d\n",insno,ready_queue[i].process_name,output);
insno++;
sleep(1);
times[i]-=1;
}
StartNext(i);
}
if(times[i]==0)
{
time_t end=time(0);
timetaken=difftime(end, start);//time spend for arithmatic operations in parser file
waiting_time=difftime(start,ready_queue[i].arr_time_stamp);//waitingtime=time spent after arriving till going inside parser for execution
f = fopen("logs_rr","a");//generating logs file
fprintf(f ,"%s process's Arrival Time : %lf Waiting Time:%lf cpu_burst_time : %lf \n " ,ready_queue[i].process_name,ready_queue[i].arr_time,waiting_time,timetaken);
fclose(f);
printf("%s is completed\n",ready_queue[i].process_name);
}
pthread_exit(0);
}
void * threadfunc ( void * par )
{
struct pcb* pcb1 = (struct pcb *)par;
char arr[20];
strcpy(arr,pcb1->process_name);
if(choice==2)
{
checkthread[(pcb1->process_id)-1]=0;//11
pause();//11
}
FILE *fp;
int count=0;
char c;
fp=fopen(arr,"r");
int insno= 1;
char line[BUFSIZ];
int output;
double timetaken,waiting_time;//11
sleep(1);//for waiting time
time_t start=time(0);
//giving instruction to the parser file
while(fgets(line,sizeof(line),fp))
{
output=parser(line);
printf("output of process%d of %s is %d\n",insno,arr,output);
insno++;
sleep(1);//takes 1 sec to complete each instruction
}
time_t end=time(0);
fclose(fp);
timetaken=difftime(end, start);//time spend for arithmatic operations in parser file
waiting_time=difftime(start,pcb1->arr_time_stamp)-4;//waitingtime=time spent after arriving till going inside parser for execution
switch(choice)
{
case 1:
{
f = fopen("logs_fcfs","a");
fprintf(f , " %s process's Arrival Time : %lf Waiting Time : %lf cpu_burst_time : %lf \n " , arr , pcb1->arr_time,waiting_time,timetaken);
fclose(f);
pthread_mutex_unlock(&var); //applying unlock
printf("%s is completed\n",arr);
pthread_exit(NULL);
break;
}
case 3:
{
f = fopen("logs_sjf","a");
fprintf(f,"%s process's Arrival Time:%lf Waiting Time:%lf cpu_burst_time:%lf\n" ,arr,pcb1->arr_time,waiting_time,timetaken);
fclose(f);
pthread_mutex_unlock(&var); //applying unlock
printf("%s is completed\n",arr);
pthread_exit(NULL);
break;
}
case 4:
{
f = fopen("logs_priority","a");
fprintf(f , " %s process's Priority:%d Arrival Time : %lf Waiting Time : %lf cpu_burst_time: %lf \n " , arr ,pcb1->priority, pcb1->arr_time,waiting_time,timetaken);
fclose(f);
pthread_mutex_unlock(&var); //applying unlock
printf("%s is completed\n",arr);
pthread_exit(NULL);
break;
}
}
}
int main(){
printf("........................MENU..........................\n");
printf("......................1.FCFS..........................\n");
printf("...................2.ROUND ROBIN......................\n");
printf("................3.SHORTEST JOB FIRST..................\n");
printf("..................4.PRIORITY VISE.....................\n");
printf("ENTER YOUR CHOICE\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
creatingprocess();//creates pcb of each process from 'programs' folder
int maxthread = total_files;
pthread_t threads[maxthread];//array of threads
pthread_mutex_init(&var,NULL);//initialize mutex
struct pcb fcfs_array[10];//array of processes according to fcfs
double temp;
char tempname[20];
int pos;
int tempid;
int status;
//copying ready_queue to fcfs array for sorting
memcpy(&fcfs_array,&ready_queue,sizeof(ready_queue));
//sorting
for(int i=0;i<total_files;i++)
{
pos=i;
for(int j=i+1;j<total_files;j++)
{
if(fcfs_array[j].arr_time<fcfs_array[pos].arr_time)
pos=j;
}
temp=fcfs_array[i].arr_time;
fcfs_array[i].arr_time=fcfs_array[pos].arr_time;
fcfs_array[pos].arr_time=temp;
tempid=fcfs_array[i].process_id;
fcfs_array[i].process_id=fcfs_array[pos].process_id;
fcfs_array[pos].process_id=tempid;
strcpy(tempname,fcfs_array[i].process_name);
strcpy(fcfs_array[i].process_name,fcfs_array[pos].process_name);
strcpy(fcfs_array[pos].process_name,tempname);
}
//creating threads of each process taken from 'programs' folder
for(int i=0;i<maxthread;i++)
{
status = pthread_create(&threads[i], NULL, threadfunc, (void *) &fcfs_array[i] );
//error handling
if (status != 0)
{
printf("error in creating threads");
exit(1);
}
pthread_join(threads[i], NULL);
}
break;
}
case 2:
{
creatingprocess();
int ins[4]={0};
pthread_t threads[4];
int i, status;
char arr[50];
char line[BUFSIZ];
char *b;
for(i=0;i<4;i++)
{
strcpy(arr,ready_queue[i].process_name);
FILE *fp;
fp=fopen(arr,"r");
while(fgets(line,sizeof(line),fp))
{
ins[i]++;
}
fclose(fp);
}
for(i=0;i<4;i++)
{
times[i] =ins[i];
}
for(i = 0; i < 4; i++)
{
status = pthread_create(&threads[i], NULL, Run, (void *)i); //Create threads
if(status != 0)
{
printf("While creating thread %d, pthread_create returned error code %d\n", i, status);
exit(-1);
}
}
for(i=0;i<4;i++)
pthread_join(threads[i], 0);
break;
}
case 3:
{
creatingprocess();//creates pcb of each process from 'programs' folder
struct pcb sjf_array[10];//array of processes according to sjf
double temp;
char tempname[50];
int pos;
int status;
int maxthread = total_files;
pthread_mutex_init(&var,NULL);//initialize mutex
pthread_t threads[maxthread];//array of threads
memcpy(&sjf_array,&ready_queue,sizeof(ready_queue));
//sorting sjf array according to cpu_burst_time time
for(int i=0;i<total_files;i++)
{
pos=i;
for(int j=i+1;j<total_files;j++)
{
if(sjf_array[j].cpu_burst_time<sjf_array[pos].cpu_burst_time)
pos=j;
}
temp=sjf_array[i].cpu_burst_time;
sjf_array[i].cpu_burst_time=sjf_array[pos].cpu_burst_time;
sjf_array[pos].cpu_burst_time=temp;
temp=sjf_array[i].process_id;
sjf_array[i].process_id=sjf_array[pos].process_id;
sjf_array[pos].process_id=temp;
strcpy(tempname,sjf_array[i].process_name);
strcpy(sjf_array[i].process_name,sjf_array[pos].process_name);
strcpy(sjf_array[pos].process_name,tempname);
}
//creating threads of each process taken from 'programs' folder
for(int i=0;i<total_files;i++)
{
status = pthread_create(&threads[i], NULL, threadfunc, (void *) &sjf_array[i]);
//error handling
if (status != 0)
{
printf("error in creating thread");
exit(1);
}
pthread_join(threads[i], NULL);
}
break;
}
case 4:
{
creatingprocess();//creates pcb of each process from 'programs' folder
int maxthread = total_files;
pthread_t threads[maxthread];//array of threads
pthread_mutex_init(&var,NULL);//initialize mutex
struct pcb priority_array[10];//array of processes according to priority
int temp;
double temparrival;
int tempid;
char tempname[20];
int pos;
int status;
//copying ready_queue to priority array for sorting
memcpy(&priority_array,&ready_queue,sizeof(ready_queue));
//sorting
for(int i=0;i<total_files;i++)
{
pos=i;
for(int j=i+1;j<total_files;j++)
{
if(priority_array[j].priority<priority_array[pos].priority)
pos=j;
}
temp=priority_array[i].priority;
priority_array[i].priority=priority_array[pos].priority;
priority_array[pos].priority=temp;
temparrival=priority_array[i].arr_time;
priority_array[i].arr_time=priority_array[pos].arr_time;
priority_array[pos].arr_time=temparrival;
tempid=priority_array[i].process_id;
priority_array[i].process_id=priority_array[pos].process_id;
priority_array[pos].process_id=tempid;
strcpy(tempname,priority_array[i].process_name);
strcpy(priority_array[i].process_name,priority_array[pos].process_name);
strcpy(priority_array[pos].process_name,tempname);
}
//creating threads of each process taken from 'programs' folder
for(int i=0;i<maxthread;i++)
{
status = pthread_create(&threads[i], NULL, threadfunc, (void *) &priority_array[i] );
//error handling
if (status != 0)
{
printf("error in creating threads");
exit(1);
}
pthread_join(threads[i], NULL);
}
break;
}
}
return 0;
}