@@ -61,8 +61,13 @@ struct process_params
61
61
int pid ;
62
62
int outmode ;
63
63
};
64
+ struct scheduler_params
65
+ {
66
+ int outmode ;
67
+ int allp ;
68
+ };
64
69
65
- int isAllpFinished ();
70
+ int isAllpFinished (int size );
66
71
67
72
int main (int argc , char const * argv [])
68
73
{
@@ -98,8 +103,6 @@ int main(int argc, char const *argv[])
98
103
strcpy (distIAT , argv [8 ]); avgIAT = atoi (argv [9 ]); minIAT = atoi (argv [10 ]); maxIAT = atoi (argv [11 ]);
99
104
rqLen = atoi (argv [12 ]); allp = atoi (argv [13 ]); outmode = atoi (argv [14 ]);
100
105
101
- printf ("Test1\n" );
102
-
103
106
if ( argc == MIN_ARGS_C + 1 )
104
107
{
105
108
strcpy (outfile ,argv [15 ]);
@@ -125,7 +128,6 @@ int main(int argc, char const *argv[])
125
128
}
126
129
}
127
130
128
- printf ("Test2\n" );
129
131
// Start Simulation
130
132
gettimeofday (& start , NULL );
131
133
@@ -159,17 +161,20 @@ int main(int argc, char const *argv[])
159
161
params .minPrio = minPrio ; params .maxPrio = maxPrio ;
160
162
params .allp = allp ; params .outmode = outmode ;
161
163
162
- if (strcmp (prog_mode , "C" ))
164
+ if (strcmp (prog_mode , "C" ) == 0 )
163
165
params .mode = 0 ;
164
166
else
165
167
{
166
168
params .mode = 1 ;
167
169
strcpy (params .infile , infile );
168
170
}
169
-
170
- printf ("Test3\n" );
171
+
172
+ struct scheduler_params sParams ;
173
+ sParams .allp = allp ;
174
+ sParams .outmode = outmode ;
175
+
171
176
pthread_create (& generator_tid , NULL , generator , (void * ) & params );
172
- pthread_create (& scheduler_tid , NULL , scheduler , (void * ) & outmode );
177
+ pthread_create (& scheduler_tid , NULL , scheduler , (void * ) & sParams );
173
178
174
179
pthread_join (generator_tid , NULL );
175
180
pthread_join (scheduler_tid , NULL );
@@ -208,13 +213,10 @@ void *generator(void *args)
208
213
209
214
pthread_t * thread_id_array = malloc (sizeof (pthread_t ) * numOfProcesses );
210
215
211
- printf ("Test4\n" );
212
216
for (int i = 0 ; i < numOfProcesses ; i ++ )
213
217
{
214
218
if (mode == 0 )
215
219
{
216
- printf ("Test5\n" );
217
-
218
220
process_length = generate_process_length (params -> distPL , params -> avgPL , params -> minPL , params -> maxPL );
219
221
interarrival_time = generate_interarrival_time (params -> distIAT , params -> avgIAT , params -> minIAT , params -> maxIAT );
220
222
priority = generate_priority (params -> minPrio , params -> maxPrio );
@@ -233,7 +235,6 @@ void *generator(void *args)
233
235
fscanf (fp , "%d" , & value );
234
236
interarrival_time = value ;
235
237
}
236
-
237
238
238
239
struct process_params p_params ;
239
240
p_params .priority = priority ;
@@ -250,7 +251,7 @@ void *generator(void *args)
250
251
251
252
if (outmode == 3 )
252
253
{
253
- printf ("New Process with pid %d created" , p_params .pid );
254
+ printf ("New Process with pid %d created\n " , p_params .pid );
254
255
}
255
256
256
257
usleep (interarrival_time * 1000 );
@@ -287,6 +288,10 @@ void *process(void *args)
287
288
288
289
// Critical Section
289
290
insert_pcb (& runqueue , pcb );
291
+
292
+ pthread_mutex_unlock (& lock_runqueue );
293
+
294
+ pthread_cond_signal (& scheduler_cond_var );
290
295
291
296
if ( outmode == 3 )
292
297
{
@@ -296,11 +301,10 @@ void *process(void *args)
296
301
gettimeofday (& arrival , NULL );
297
302
pcb .arrival_time = (arrival .tv_usec - start .tv_usec ) / 1000 ;
298
303
299
- pthread_mutex_unlock ( & lock_runqueue );
304
+
300
305
301
306
while ( states_array [pcb .pid - 1 ] == WAITING )
302
307
{
303
- pthread_cond_signal (& scheduler_cond_var );
304
308
pthread_cond_wait (& (cond_var_array [pcb .pid - 1 ]), & lock_runqueue );
305
309
306
310
// Running
@@ -372,34 +376,39 @@ void *process(void *args)
372
376
373
377
void * scheduler (void * args )
374
378
{
375
- int * outmode = (int * ) args ;
376
- while ( isAllpFinished () == 0 )
379
+ struct scheduler_params * sParams = (struct scheduler_params * ) args ;
380
+ int outmode = sParams -> outmode ;
381
+ int allp = sParams -> allp ;
382
+ printf ("%d" , isAllpFinished (allp ));
383
+ while ( isAllpFinished (allp ) == 0 )
377
384
{
378
385
pthread_cond_wait (& scheduler_cond_var , & lock_runqueue );
379
386
380
- // Sceduler Woken Up
387
+ // Scheduler Woken Up
381
388
pthread_mutex_lock (& lock_runqueue );
382
-
389
+ printf ( "Entered\n" );
383
390
struct Process_Control_Block pcb = get_min_pcb (& runqueue );
384
391
states_array [pcb .pid - 1 ] = RUNNING ;
385
392
393
+ pthread_cond_signal (& (cond_var_array [pcb .pid - 1 ]));
394
+
386
395
pthread_mutex_unlock (& lock_runqueue );
387
396
388
- if (* outmode == 3 )
397
+ if (outmode == 3 )
389
398
{
390
399
printf ("Process with pid %d is selected for CPU\n" , pcb .pid );
391
400
}
392
401
393
- pthread_cond_signal ( & ( cond_var_array [ pcb . pid - 1 ]));
402
+
394
403
395
404
}
396
405
397
406
pthread_exit (0 );
398
407
}
399
408
400
- int isAllpFinished ()
409
+ int isAllpFinished (int size )
401
410
{
402
- for (int i = 0 ; i < runqueue . currentSize ; i ++ )
411
+ for (int i = 0 ; i < size ; i ++ )
403
412
{
404
413
if ( states_array [i ] != FINISHED )
405
414
{
0 commit comments