Skip to content

Commit 7062c0f

Browse files
committed
Testing
1 parent 9cbdaf4 commit 7062c0f

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

cfs.c

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ struct process_params
6161
int pid;
6262
int outmode;
6363
};
64+
struct scheduler_params
65+
{
66+
int outmode;
67+
int allp;
68+
};
6469

65-
int isAllpFinished();
70+
int isAllpFinished(int size);
6671

6772
int main(int argc, char const *argv[])
6873
{
@@ -98,8 +103,6 @@ int main(int argc, char const *argv[])
98103
strcpy(distIAT, argv[8]); avgIAT = atoi(argv[9]); minIAT = atoi(argv[10]); maxIAT = atoi(argv[11]);
99104
rqLen = atoi(argv[12]); allp = atoi(argv[13]); outmode = atoi(argv[14]);
100105

101-
printf("Test1\n");
102-
103106
if ( argc == MIN_ARGS_C + 1 )
104107
{
105108
strcpy(outfile,argv[15]);
@@ -125,7 +128,6 @@ int main(int argc, char const *argv[])
125128
}
126129
}
127130

128-
printf("Test2\n");
129131
// Start Simulation
130132
gettimeofday(&start, NULL);
131133

@@ -159,17 +161,20 @@ int main(int argc, char const *argv[])
159161
params.minPrio = minPrio; params.maxPrio = maxPrio;
160162
params.allp = allp; params.outmode = outmode;
161163

162-
if (strcmp(prog_mode, "C"))
164+
if (strcmp(prog_mode, "C") == 0)
163165
params.mode = 0;
164166
else
165167
{
166168
params.mode = 1;
167169
strcpy(params.infile, infile);
168170
}
169-
170-
printf("Test3\n");
171+
172+
struct scheduler_params sParams;
173+
sParams.allp = allp;
174+
sParams.outmode = outmode;
175+
171176
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);
173178

174179
pthread_join(generator_tid, NULL);
175180
pthread_join(scheduler_tid, NULL);
@@ -208,13 +213,10 @@ void *generator(void *args)
208213

209214
pthread_t *thread_id_array = malloc(sizeof(pthread_t) * numOfProcesses);
210215

211-
printf("Test4\n");
212216
for (int i = 0; i < numOfProcesses; i++)
213217
{
214218
if (mode == 0)
215219
{
216-
printf("Test5\n");
217-
218220
process_length = generate_process_length(params->distPL, params->avgPL, params->minPL, params->maxPL);
219221
interarrival_time = generate_interarrival_time(params->distIAT, params->avgIAT, params->minIAT, params->maxIAT);
220222
priority = generate_priority(params->minPrio, params->maxPrio);
@@ -233,7 +235,6 @@ void *generator(void *args)
233235
fscanf(fp, "%d", &value);
234236
interarrival_time = value;
235237
}
236-
237238

238239
struct process_params p_params;
239240
p_params.priority = priority;
@@ -250,7 +251,7 @@ void *generator(void *args)
250251

251252
if (outmode == 3)
252253
{
253-
printf("New Process with pid %d created", p_params.pid);
254+
printf("New Process with pid %d created\n", p_params.pid);
254255
}
255256

256257
usleep(interarrival_time * 1000);
@@ -287,6 +288,10 @@ void *process(void *args)
287288

288289
// Critical Section
289290
insert_pcb(&runqueue, pcb);
291+
292+
pthread_mutex_unlock(&lock_runqueue);
293+
294+
pthread_cond_signal(&scheduler_cond_var);
290295

291296
if ( outmode == 3 )
292297
{
@@ -296,11 +301,10 @@ void *process(void *args)
296301
gettimeofday(&arrival, NULL);
297302
pcb.arrival_time = (arrival.tv_usec - start.tv_usec) / 1000;
298303

299-
pthread_mutex_unlock(&lock_runqueue);
304+
300305

301306
while ( states_array[pcb.pid - 1] == WAITING )
302307
{
303-
pthread_cond_signal(&scheduler_cond_var);
304308
pthread_cond_wait(&(cond_var_array[pcb.pid - 1]), &lock_runqueue);
305309

306310
// Running
@@ -372,34 +376,39 @@ void *process(void *args)
372376

373377
void *scheduler(void *args)
374378
{
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 )
377384
{
378385
pthread_cond_wait(&scheduler_cond_var, &lock_runqueue);
379386

380-
// Sceduler Woken Up
387+
// Scheduler Woken Up
381388
pthread_mutex_lock(&lock_runqueue);
382-
389+
printf("Entered\n");
383390
struct Process_Control_Block pcb = get_min_pcb(&runqueue);
384391
states_array[pcb.pid - 1] = RUNNING;
385392

393+
pthread_cond_signal(&(cond_var_array[pcb.pid - 1]));
394+
386395
pthread_mutex_unlock(&lock_runqueue);
387396

388-
if (*outmode == 3)
397+
if (outmode == 3)
389398
{
390399
printf("Process with pid %d is selected for CPU\n", pcb.pid);
391400
}
392401

393-
pthread_cond_signal(&(cond_var_array[pcb.pid - 1]));
402+
394403

395404
}
396405

397406
pthread_exit(0);
398407
}
399408

400-
int isAllpFinished()
409+
int isAllpFinished(int size)
401410
{
402-
for (int i = 0; i < runqueue.currentSize; i++)
411+
for (int i = 0; i < size; i++)
403412
{
404413
if ( states_array[i] != FINISHED )
405414
{

distribute.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// Declarations
1010
int generate_priority(int minPrio, int maxPrio);
1111

12-
int generate_process_length(char *distPL, int avgPL, int minPL, int maxPL);
12+
int generate_process_length(char distPL[STR_SIZE], int avgPL, int minPL, int maxPL);
1313

14-
int generate_interarrival_time(char *distIAT, int avgIAT, int minIAT, int maxIAT);
14+
int generate_interarrival_time(char distIAT[STR_SIZE], int avgIAT, int minIAT, int maxIAT);
1515

1616

1717
// Implementation

0 commit comments

Comments
 (0)