Skip to content

Commit 0afdd75

Browse files
committed
makefile and output
1 parent e7462b2 commit 0afdd75

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

cfs.c

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pthread_cond_t *cond_var_array;
3030

3131
int *states_array;
3232

33-
struct timeval start, arrival, finish;
33+
struct timeval start, arrival, finish, running;
3434

3535
struct Process_Control_Block *pcb_array;
3636
int pcb_array_currentSize;
@@ -51,13 +51,15 @@ struct generator_params
5151
int allp;
5252
int mode;
5353
char infile[STR_SIZE];
54+
int outmode;
5455
};
5556

5657
struct process_params
5758
{
5859
int process_length;
5960
int priority;
6061
int pid;
62+
int outmode;
6163
};
6264

6365
int isAllpFinished();
@@ -152,7 +154,7 @@ int main(int argc, char const *argv[])
152154
params.minPL = minPL; params.minIAT = minIAT;
153155
params.maxPL = maxPL; params.maxIAT = maxIAT;
154156
params.minPrio = minPrio; params.maxPrio = maxPrio;
155-
params.allp = allp;
157+
params.allp = allp; params.outmode = outmode;
156158

157159
if (strcmp(prog_mode, "C"))
158160
params.mode = 0;
@@ -164,7 +166,7 @@ int main(int argc, char const *argv[])
164166

165167

166168
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);
168170

169171
pthread_join(generator_tid, NULL);
170172
pthread_join(scheduler_tid, NULL);
@@ -193,6 +195,7 @@ void *generator(void *args)
193195
struct generator_params *params = (struct generator_params*) args;
194196
int numOfProcesses = params->allp;
195197
int mode = params->mode;
198+
int outmode = params->outmode;
196199

197200
int process_length, interarrival_time, priority;
198201
FILE *fp;
@@ -230,13 +233,19 @@ void *generator(void *args)
230233
p_params.priority = priority;
231234
p_params.process_length = process_length;
232235
p_params.pid = i + 1;
236+
p_params.outmode = outmode;
233237

234238
while ( isFull(&runqueue) )
235239
usleep( interarrival_time * 1000 );
236240

237241
// Create thread
238242
pthread_create(&thread_id_array[i], NULL, process, (void *) &p_params);
239243

244+
if (outmode == 3)
245+
{
246+
printf("New Process with pid %d created", p_params.pid);
247+
}
248+
240249
usleep(interarrival_time * 1000);
241250

242251
}
@@ -266,11 +275,18 @@ void *process(void *args)
266275
pcb.virtual_runtime = 0.0;
267276
pcb.total_time_spent = 0; //?
268277

278+
int outmode = params->outmode;
279+
269280
pthread_mutex_lock(&lock_runqueue);
270281

271282
// Critical Section
272283
insert_pcb(&runqueue, pcb);
273284

285+
if ( outmode == 3 )
286+
{
287+
printf("The process with pid %d added to the runqueue\n", pcb.pid);
288+
}
289+
274290
gettimeofday(&arrival, NULL);
275291
pcb.arrival_time = (arrival.tv_usec - start.tv_usec) / 1000;
276292

@@ -284,6 +300,17 @@ void *process(void *args)
284300
// Running
285301
pthread_mutex_lock(&lock_cpu);
286302

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+
287314
int timeslice = calculate_timeslice(&runqueue, pcb.priority);
288315

289316
int actualruntime;
@@ -295,6 +322,10 @@ void *process(void *args)
295322
else
296323
{
297324
actualruntime = timeslice;
325+
if (outmode == 3)
326+
{
327+
printf("Process with pid %d expired timeslice\n", pcb.pid);
328+
}
298329
}
299330

300331
usleep(actualruntime * 1000);
@@ -314,6 +345,10 @@ void *process(void *args)
314345
else
315346
{
316347
states_array[pcb.pid - 1] = FINISHED;
348+
if (outmode == 3)
349+
{
350+
printf("Process with pid %d finished\n", pcb.pid);
351+
}
317352
gettimeofday(&finish, NULL);
318353
pcb_array[pcb_array_currentSize] = pcb;
319354
pcb_array_currentSize++;
@@ -331,6 +366,7 @@ void *process(void *args)
331366

332367
void *scheduler(void *args)
333368
{
369+
int *outmode = (int *) args;
334370
while ( isAllpFinished() == 0 )
335371
{
336372
pthread_cond_wait(&scheduler_cond_var, &lock_runqueue);
@@ -343,6 +379,11 @@ void *scheduler(void *args)
343379

344380
pthread_mutex_unlock(&lock_runqueue);
345381

382+
if (*outmode == 3)
383+
{
384+
printf("Process with pid %d is selected for CPU\n", pcb.pid);
385+
}
386+
346387
pthread_cond_signal(&(cond_var_array[pcb.pid - 1]));
347388

348389
}

makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
compile:
2+
gcc -w cfs.c -lm -lpthread -o cfs
3+
clean:
4+
rm cfs

0 commit comments

Comments
 (0)