@@ -30,7 +30,7 @@ pthread_cond_t *cond_var_array;
30
30
31
31
int * states_array ;
32
32
33
- struct timeval start , arrival , finish ;
33
+ struct timeval start , arrival , finish , running ;
34
34
35
35
struct Process_Control_Block * pcb_array ;
36
36
int pcb_array_currentSize ;
@@ -51,13 +51,15 @@ struct generator_params
51
51
int allp ;
52
52
int mode ;
53
53
char infile [STR_SIZE ];
54
+ int outmode ;
54
55
};
55
56
56
57
struct process_params
57
58
{
58
59
int process_length ;
59
60
int priority ;
60
61
int pid ;
62
+ int outmode ;
61
63
};
62
64
63
65
int isAllpFinished ();
@@ -152,7 +154,7 @@ int main(int argc, char const *argv[])
152
154
params .minPL = minPL ; params .minIAT = minIAT ;
153
155
params .maxPL = maxPL ; params .maxIAT = maxIAT ;
154
156
params .minPrio = minPrio ; params .maxPrio = maxPrio ;
155
- params .allp = allp ;
157
+ params .allp = allp ; params . outmode = outmode ;
156
158
157
159
if (strcmp (prog_mode , "C" ))
158
160
params .mode = 0 ;
@@ -164,7 +166,7 @@ int main(int argc, char const *argv[])
164
166
165
167
166
168
pthread_create (& generator_tid , NULL , generator , (void * ) & params );
167
- pthread_create (& scheduler_tid , NULL , scheduler , NULL );
169
+ pthread_create (& scheduler_tid , NULL , scheduler , ( void * ) & outmode );
168
170
169
171
pthread_join (generator_tid , NULL );
170
172
pthread_join (scheduler_tid , NULL );
@@ -193,6 +195,7 @@ void *generator(void *args)
193
195
struct generator_params * params = (struct generator_params * ) args ;
194
196
int numOfProcesses = params -> allp ;
195
197
int mode = params -> mode ;
198
+ int outmode = params -> outmode ;
196
199
197
200
int process_length , interarrival_time , priority ;
198
201
FILE * fp ;
@@ -230,13 +233,19 @@ void *generator(void *args)
230
233
p_params .priority = priority ;
231
234
p_params .process_length = process_length ;
232
235
p_params .pid = i + 1 ;
236
+ p_params .outmode = outmode ;
233
237
234
238
while ( isFull (& runqueue ) )
235
239
usleep ( interarrival_time * 1000 );
236
240
237
241
// Create thread
238
242
pthread_create (& thread_id_array [i ], NULL , process , (void * ) & p_params );
239
243
244
+ if (outmode == 3 )
245
+ {
246
+ printf ("New Process with pid %d created" , p_params .pid );
247
+ }
248
+
240
249
usleep (interarrival_time * 1000 );
241
250
242
251
}
@@ -266,11 +275,18 @@ void *process(void *args)
266
275
pcb .virtual_runtime = 0.0 ;
267
276
pcb .total_time_spent = 0 ; //?
268
277
278
+ int outmode = params -> outmode ;
279
+
269
280
pthread_mutex_lock (& lock_runqueue );
270
281
271
282
// Critical Section
272
283
insert_pcb (& runqueue , pcb );
273
284
285
+ if ( outmode == 3 )
286
+ {
287
+ printf ("The process with pid %d added to the runqueue\n" , pcb .pid );
288
+ }
289
+
274
290
gettimeofday (& arrival , NULL );
275
291
pcb .arrival_time = (arrival .tv_usec - start .tv_usec ) / 1000 ;
276
292
@@ -284,6 +300,17 @@ void *process(void *args)
284
300
// Running
285
301
pthread_mutex_lock (& lock_cpu );
286
302
303
+ if (outmode == 2 )
304
+ {
305
+ gettimeofday (& running , NULL );
306
+ int time = (running .tv_usec - start .tv_usec ) / 1000 ;
307
+ printf ("%d %d RUNNING\n" , time , pcb .pid );
308
+ }
309
+ else if (outmode == 3 )
310
+ {
311
+ printf ("Process with pid %d is running in CPU\n" , pcb .pid );
312
+ }
313
+
287
314
int timeslice = calculate_timeslice (& runqueue , pcb .priority );
288
315
289
316
int actualruntime ;
@@ -295,6 +322,10 @@ void *process(void *args)
295
322
else
296
323
{
297
324
actualruntime = timeslice ;
325
+ if (outmode == 3 )
326
+ {
327
+ printf ("Process with pid %d expired timeslice\n" , pcb .pid );
328
+ }
298
329
}
299
330
300
331
usleep (actualruntime * 1000 );
@@ -314,6 +345,10 @@ void *process(void *args)
314
345
else
315
346
{
316
347
states_array [pcb .pid - 1 ] = FINISHED ;
348
+ if (outmode == 3 )
349
+ {
350
+ printf ("Process with pid %d finished\n" , pcb .pid );
351
+ }
317
352
gettimeofday (& finish , NULL );
318
353
pcb_array [pcb_array_currentSize ] = pcb ;
319
354
pcb_array_currentSize ++ ;
@@ -331,6 +366,7 @@ void *process(void *args)
331
366
332
367
void * scheduler (void * args )
333
368
{
369
+ int * outmode = (int * ) args ;
334
370
while ( isAllpFinished () == 0 )
335
371
{
336
372
pthread_cond_wait (& scheduler_cond_var , & lock_runqueue );
@@ -343,6 +379,11 @@ void *scheduler(void *args)
343
379
344
380
pthread_mutex_unlock (& lock_runqueue );
345
381
382
+ if (* outmode == 3 )
383
+ {
384
+ printf ("Process with pid %d is selected for CPU\n" , pcb .pid );
385
+ }
386
+
346
387
pthread_cond_signal (& (cond_var_array [pcb .pid - 1 ]));
347
388
348
389
}
0 commit comments