-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcb.c
454 lines (398 loc) · 12 KB
/
pcb.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
12/6/2017
Authors: Connor Lundberg, Jasmine Dacones, Jacob Ackerman
*/
#include "pcb.h"
int global_largest_PID = 0;
/*
* Helper function to iniialize PCB data.
*/
void initialize_data(/* in-out */ PCB pcb) {
pcb->pid = 0;
pcb->parent = -1;
PCB_assign_priority(pcb, 0);
pcb->size = 0;
pcb->channel_no = 0;
pcb->state = 0;
pcb->blocked_timer = -1;
pcb->mem = NULL;
pcb->context->pc = 0;
pcb->context->ir = 0;
pcb->context->r0 = 0;
pcb->context->r1 = 0;
pcb->context->r2 = 0;
pcb->context->r3 = 0;
pcb->context->r4 = 0;
pcb->context->r5 = 0;
pcb->context->r6 = 0;
pcb->context->r7 = 0;
pcb->max_pc = makeMaxPC();
pcb->creation = 0;
pcb->termination = 0;
pcb->terminate = rand() % MAX_TERM_COUNT;
if (pcb->terminate == 0) {
pcb->terminate++;
}
pcb->term_count = 0;
pcb->isProducer = 0;
pcb->isConsumer = 0;
}
/*
Chooses a role type for a PCB to be assigned to. The percentages work as such:
- 50% will be Computationally Intensive
- 25% will be I/O Trap
- 12.5% will be Producer/Consumer
- 12.5% will be Shared Resource
*/
enum pcb_type chooseRole () {
int num = rand() % ROLE_PERCENTAGE_MAX_RANGE;
enum pcb_type newRole;
if (num <= COMP_ROLE_MAX_RANGE) {
newRole = COMP;
} else if (num >= IO_ROLE_MIN_RANGE && num <= IO_ROLE_MAX_RANGE) {
newRole = IO;
} else if (num >= PAIR_ROLE_MIN_RANGE && num <= PAIR_ROLE_MAX_RANGE) {
newRole = PAIR;
} else {
newRole = SHARED;
}
return newRole;
}
/*
This will initialize the role type of the given PCB. If it is the first of the pair
that is created, then the role will be randomly chosen, otherwise the role will match
the first one created. From there, if the role is IO, then the PCBs I/O Trap locations
will be initialized. If the role is PAIR or SHARED, then the 1st and 2nd PCBs contained
within the given shared mutex will be set.
*/
void initialize_pcb_type (PCB pcb, int isFirst, Mutex sharedMutexR1, Mutex sharedMutexR2) {
int lock = 0, unlock = 0, signal = 0, wait = 0;
if (isFirst) {
pcb->role = chooseRole();
} else {
pcb->role = sharedMutexR1->pcb1->role;
}
switch(pcb->role) {
case COMP:
if (isFirst) {
sharedMutexR1->pcb1 = pcb;
sharedMutexR2->pcb1 = pcb;
} else {
sharedMutexR1->pcb2 = pcb;
sharedMutexR2->pcb2 = pcb;
}
break;
case IO:
populateIOTraps (pcb, 0); // populates io_1_traps
populateIOTraps (pcb, 1); // populates io_2_traps
if (isFirst) {
sharedMutexR1->pcb1 = pcb;
sharedMutexR2->pcb1 = pcb;
} else {
sharedMutexR1->pcb2 = pcb;
sharedMutexR2->pcb2 = pcb;
}
break;
case PAIR:
if (isFirst) {
if ((rand() % 100) > 49) { //this decides if it's producer or consumer
pcb->isProducer = 1;
} else {
pcb->isConsumer = 1;
}
sharedMutexR1->pcb1 = pcb;
sharedMutexR2->pcb1 = pcb;
} else {
if (sharedMutexR1->pcb1->isProducer) { //if the first PCB is producer, the second will be
pcb->isConsumer = 1; //Consumer, or vice versa
} else {
pcb->isProducer = 1;
}
sharedMutexR1->pcb2 = pcb;
sharedMutexR2->pcb2 = pcb;
}
pcb->mutex_R1_id = sharedMutexR1->mid;
pcb->mutex_R2_id = sharedMutexR2->mid;
break;
case SHARED:
if (isFirst) {
sharedMutexR1->pcb1 = pcb;
sharedMutexR2->pcb1 = pcb;
} else {
sharedMutexR1->pcb2 = pcb;
sharedMutexR2->pcb2 = pcb;
}
pcb->mutex_R1_id = sharedMutexR1->mid;
pcb->mutex_R2_id = sharedMutexR2->mid;
break;
}
}
/*
Displays the PC trap values for a given trap array.
*/
void printPCLocations (unsigned int pcLocs[]) {
for (int i = 0; i < TRAP_COUNT; i++) {
printf("%d ", pcLocs[i]);
}
printf("\r\n");
}
/*
A helper function to populate the IO Trap PC values for the pcb. Creates a random number
that is less than the pcb's max PC value. While the random number is contained within either
pcb trap lists, the random number is incremented by one. Once a new number is created that is
not within the two lists, it will be inserted into the next open position for the list specified
by the ioTrapType parameter.
*/
void populateIOTraps (PCB pcb, int ioTrapType) {
unsigned int newRand = 0;
for (int i = 0; i < TRAP_COUNT; i++) {
newRand = rand() % pcb->max_pc;
while (ioTrapContains(newRand, pcb->io_1_traps) || ioTrapContains(newRand, pcb->io_2_traps)) {
newRand++;
}
if (!ioTrapType) {
pcb->io_1_traps[i] = newRand;
} else {
pcb->io_2_traps[i] = newRand;
}
}
}
/*
Populates the mutex traps values for a PCB of type SHARED. Calling this function for both PCBs in a pair is
made for the purpose of a non-deadlock situation.
*/
void populateMutexTraps1221(PCB pcb, int step) {
memcpy(pcb->lockR1, ((unsigned int[TRAP_COUNT]) {1 * step, 5 * step, 9 * step, 13 * step}), 4 * sizeof(unsigned int));
memcpy(pcb->lockR2, ((unsigned int[TRAP_COUNT]) {2 * step, 6 * step, 10 * step, 14 * step}), 4 * sizeof(unsigned int));
memcpy(pcb->unlockR2, ((unsigned int[TRAP_COUNT]) {3 * step, 7 * step, 11 * step, 15 * step}), 4 * sizeof(unsigned int));
memcpy(pcb->unlockR1, ((unsigned int[TRAP_COUNT]) {4 * step, 8 * step, 12 * step, 16 * step}), 4 * sizeof(unsigned int));
}
/*
Populates the mutex traps values for a PCB of type SHARED. Calling this function for one PCB of a pair is made
for the purpose of a deadlock situation.
*/
void populateMutexTraps2112(PCB pcb, int step) {
memcpy(pcb->lockR2, ((unsigned int[TRAP_COUNT]) {1 * step, 5 * step, 9 * step, 13 * step}), 4 * sizeof(unsigned int));
memcpy(pcb->lockR1, ((unsigned int[TRAP_COUNT]) {2 * step, 6 * step, 10 * step, 14 * step}), 4 * sizeof(unsigned int));
memcpy(pcb->unlockR1, ((unsigned int[TRAP_COUNT]) {3 * step, 7 * step, 11 * step, 15 * step}), 4 * sizeof(unsigned int));
memcpy(pcb->unlockR2, ((unsigned int[TRAP_COUNT]) {4 * step, 8 * step, 12 * step, 16 * step}), 4 * sizeof(unsigned int));
}
/*
Populates the mutex traps values for a PCB of type PAIR.
*/
void populateProducerConsumerTraps(PCB pcb, int step, int isProducer) {
memcpy(pcb->lockR1, ((unsigned int[TRAP_COUNT]) {1 * step, 5 * step, 9 * step, 13 * step}), 4 * sizeof(unsigned int));
if (!isProducer) {
memcpy(pcb->wait_cond, ((unsigned int[TRAP_COUNT]) {2 * step, 6 * step, 10 * step, 14 * step}), 4 * sizeof(unsigned int));
} else {
memcpy(pcb->signal_cond,((unsigned int[TRAP_COUNT]) {3 * step, 7 * step, 11 * step, 15 * step}), 4 * sizeof(unsigned int));
}
memcpy(pcb->unlockR1, ((unsigned int[TRAP_COUNT]) {4 * step, 8 * step, 12 * step, 16 * step}), 4 * sizeof(unsigned int));
}
/*
Checks if the given random number is already within the given ioTraps array. If so,
return 1, otherwise 0.
*/
int ioTrapContains (unsigned int newRand, unsigned int ioTraps[]) {
unsigned int *ptr = ioTraps;
int isContained = 0;
while (*ptr) {
if (*ptr == newRand) {
isContained = 1;
break;
}
ptr++;
}
return isContained;
}
/*
Makes a random number between 0 and LARGEST_PC_POSSIBLE *found in pcb.h*.
If the number is less than SMALLEST_PC_POSSIBLE *found in pcb.h*, then maxPC will
be incremented by a new random number mod SMALLEST_PC_POSSIBLE then + SMALLEST_PC_POSSIBLE
and returned.
*/
unsigned int makeMaxPC () {
unsigned int maxPC = rand() % LARGEST_PC_POSSIBLE;
if (maxPC < SMALLEST_PC_POSSIBLE) maxPC += ((rand() % SMALLEST_PC_POSSIBLE) + SMALLEST_PC_POSSIBLE);
return maxPC;
}
/*
* Allocate a PCB and a context for that PCB.
*
* Return: NULL if context or PCB allocation failed, the new pointer otherwise.
*/
PCB PCB_create() {
PCB new_pcb = (PCB) malloc(sizeof(struct pcb));
if (new_pcb != NULL) {
new_pcb->context = (CPU_context_p) malloc(sizeof(struct cpu_context));
if (new_pcb->context != NULL) {
initialize_data(new_pcb);
PCB_assign_PID(new_pcb);
} else {
free(new_pcb);
new_pcb = NULL;
}
}
return new_pcb;
}
/*
* Frees a PCB and its context.
*
* Arguments: pcb: the pcb to free.
*/
void PCB_destroy(/* in-out */ PCB pcb) {
if (pcb) {
if (pcb->context) {
free(pcb->context);
}
free(pcb);
pcb = NULL;
}
}
/*
* Assigns intial process ID to the process.
*
* Arguments: pcb: the pcb to modify.
*/
void PCB_assign_PID(/* in */ PCB the_PCB) {
the_PCB->pid = global_largest_PID;
global_largest_PID++;
}
/*
* Sets the state of the process to the provided state.
*
* Arguments: pcb: the pcb to modify.
* state: the new state of the process.
*/
void PCB_assign_state(/* in-out */ PCB the_pcb, /* in */ enum state_type the_state) {
the_pcb->state = the_state;
}
/*
* Sets the parent of the given pcb to the provided pid.
*
* Arguments: pcb: the pcb to modify.
* pid: the parent PID for this process.
*/
void PCB_assign_parent(PCB the_pcb, int the_pid) {
the_pcb->parent = the_pid;
}
/*
* Sets the priority of the PCB to the provided value.
*
* Arguments: pcb: the pcb to modify.
* state: the new priority of the process.
*/
void PCB_assign_priority(/* in */ PCB the_pcb, /* in */ unsigned int the_priority) {
the_pcb->priority = the_priority;
if (the_priority > NUM_PRIORITIES) {
the_pcb->priority = NUM_PRIORITIES - 1;
}
}
/*
* Create and return a string representation of the provided PCB.
*
* Arguments: pcb: the pcb to create a string representation of.
* Return: a string representation of the provided PCB on success, NULL otherwise.
*/
void toStringPCB(PCB thisPCB, int showCpu) {
if (thisPCB) {
printf("contents: ");
printf("PID: %d, ", thisPCB->pid);
switch(thisPCB->state) {
case STATE_NEW:
printf("state: new, ");
break;
case STATE_READY:
printf("state: ready, ");
break;
case STATE_RUNNING:
printf("state: running, ");
break;
case STATE_INT:
printf("state: interrupted, ");
break;
case STATE_WAIT:
printf("state: waiting, ");
break;
case STATE_HALT:
printf("state: halted, ");
break;
}
switch(thisPCB->role) {
case COMP:
printf("role: comp, ");
break;
case IO:
printf("role: io, ");
break;
case PAIR:
printf("role: pair, "); //producer/consumer
break;
case SHARED:
printf("role: shared, ");
break;
}
printf("priority: %d, ", thisPCB->priority);
printf("PC: %d, ", thisPCB->context->pc);
if (thisPCB->role == PAIR) {
printf("isProducer: %d, ", thisPCB->isProducer);
}
//do it like IO if its PAIR or SHARED to show mutex positions
printf("\r\nMAX PC: %d\r\n", thisPCB->max_pc);
if (thisPCB->role == IO) {
printf("io_1 traps\r\n");
for (int i = 0; i < TRAP_COUNT; i++) {
printf("%d ", thisPCB->io_1_traps[i]);
}
printf("\r\nio_2 traps\r\n");
for (int i = 0; i < TRAP_COUNT; i++) {
printf("%d ", thisPCB->io_2_traps[i]);
}
printf("\r\n");
} else if (thisPCB->role == SHARED) {
printf("mutex_r1 locks\r\n");
printPCLocations(thisPCB->lockR1);
printf("mutex_r1 unlocks\r\n");
printPCLocations(thisPCB->unlockR1);
printf("mutex_r2 locks\r\n");
printPCLocations(thisPCB->lockR2);
printf("mutex_r2 unlocks\r\n");
printPCLocations(thisPCB->unlockR2);
} else if (thisPCB->role == PAIR) {
if (thisPCB->isProducer) {
printf("cond_var signals\r\n");
printPCLocations(thisPCB->signal_cond);
} else {
printf("cond_var waits\r\n");
printPCLocations(thisPCB->wait_cond);
}
}
printf("terminate: %d\r\n", thisPCB->terminate);
printf("term_count: %d\r\n", thisPCB->term_count);
printf("\r\n");
if (showCpu) {
printf("mem: 0x%04X, ", thisPCB->mem);
printf("size: %d, ", thisPCB->size);
printf("channel_no: %d ", thisPCB->channel_no);
toStringCPUContext(thisPCB->context);
}
} else {
printf("PCB is null\r\n");
}
}
/*
Prints the CPU context
*/
void toStringCPUContext(CPU_context_p context) {
printf(" CPU context values: ");
printf("ir: %d, ", context->ir);
printf("psr: %d, ", context->psr);
printf("r0: %d, ", context->r0);
printf("r1: %d, ", context->r1);
printf("r2: %d, ", context->r2);
printf("r3: %d, ", context->r3);
printf("r4: %d, ", context->r4);
printf("r5: %d, ", context->r5);
printf("r6: %d, ", context->r6);
printf("r7: %d\r\n", context->r7);
}