-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPCB.c
More file actions
556 lines (502 loc) · 16.4 KB
/
PCB.c
File metadata and controls
556 lines (502 loc) · 16.4 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Problem 3 - Discontinuities
* TCSS 422 A Spring 2016
* Bun Kak, Chris Ottersen, Mark Peters, Paul Zander
*/
#include "PCB.h"
//struct and error defines originally here
char *STATE[] = {"created", "ready", "running", "waiting", "interrupted",
"blocked", "terminated", "nostate"};
char *TYPE[] = {"regular", "producer", "mutual_A", "consumer", "mutual_B",
"undefined"};
word CODE_TYPE[(LAST_PAIR * 2) + 1][CALL_NUMBER] = {
{0, 0, 0, 0, 0, 0},
/* producer */
{CODE_LOCK + 0, CODE_WAIT_F + 0, CODE_WRITE + 0, CODE_FLAG + 0,
CODE_SIGNAL +
0,
CODE_UNLOCK + 0},
/* consumer */
{CODE_LOCK + 0, CODE_WAIT_T + 0, CODE_READ + 0, CODE_FLAG +
0, CODE_SIGNAL +
0,
CODE_UNLOCK + 0},
/* mutual_a */
{CODE_LOCK + 0, CODE_LOCK + 1, CODE_WRITE + 0, CODE_WRITE +
1, CODE_UNLOCK +
1,
CODE_UNLOCK + 0},
/* mutual_b */
{CODE_LOCK + 1, CODE_LOCK + 0, CODE_WRITE + 0, CODE_WRITE +
1, CODE_UNLOCK +
0,
CODE_UNLOCK + 1}
};
static int PRIORITIES[] = {PRIORITY_0_CHANCE, PRIORITY_1_CHANCE,
PRIORITY_2_CHANCE, PRIORITY_3_CHANCE,
PRIORITY_OTHER_CHANCE};
static int MAX_TYPES[] = {CPU_ONLY_MAX + IO_ONLY_MAX, PROCON_PAIR_MAX,
MUTUAL_PAIR_MAX};
static int MAX_IOCPU[] = {CPU_ONLY_MAX, IO_ONLY_MAX};
static int type_count[] = {0, 0, 0, 0, 0};
static int io_count[] = {0, 0};
static bool procon = false;
static bool mutual = false;
/**
* Constructs and initializes a pcb. Use unless you need to delay or avoid initialization.
* @param ptr_error
* @return
*/
PCB_p PCB_construct_init(int *ptr_error)
{
PCB_p pcb = PCB_construct(ptr_error);
sprintf(pcb->ispcb, "pcb");
*ptr_error += PCB_init(pcb);
return pcb;
}
/**
* Returns a pcb pointer to heap allocation.
*
* @return a pointer to a new PCB object
*/
PCB_p PCB_construct(int *ptr_error)
{
PCB_p this = (PCB_p) malloc(sizeof(struct PCB));
this->regs = (REG_p) malloc(sizeof(union regfile));
int error = ((!this) * PCB_INIT_ERROR);
int r;
this->buddies = THREADQ_construct(NULL);
this->pid = -1;
this->io = false;
this->type = undefined;
this->priority = -1;
this->state = nostate;
this->timeCreate = -1;
this->timeTerminate = DEFAULT_TIME_TERMINATE;
this->terminate = false;
this->lastClock = -1;
this->group = 0;
for (r = 0; r < REGNUM; r++)
this->regs->gpu[r] = -1;
if (ptr_error != NULL) {
*ptr_error += error;
}
return (this);
}
/**
* Deallocates pcb from the heap.
* @param this
*/
int PCB_destruct(PCB_p this)
{
int error = (this == NULL) *
PCB_NULL_ERROR; // sets error code to 1 if `this` is NULL
this->terminate = true;
while (!THREADQ_is_empty(this->buddies, NULL)) {
thread_type buddy = THREADQ_dequeue(this->buddies, NULL);
}
THREADQ_destruct(this->buddies, NULL);
if (!error) {
if (this->regs != NULL)
free(this->regs);
free(this);
this = NULL;
}
return error;
}
/**
* Sets default values for member data.
* @param this
* @return
*/
int PCB_init(PCB_p this)
{
static word pidCounter = 0;
static int firstCall = 1;
if (!firstCall) {
srand((uint32_t) time(NULL) << 1);
firstCall = 0;
}
int error = (this == NULL) * PCB_NULL_ERROR;
int t;
if (!error) {
int chance, percent, priority, type;
this->pid = ++pidCounter;
this->attentionCount = 0;
this->promoted = false;
//IO and type determination
int type_chance[LAST_PAIR + 1];
percent = 0;
for (type = LAST_PAIR; type >= regular; type--) {
type_chance[type] = (MAX_TYPES[type] != type_count[type]);
if (type_chance[type])
percent++;
}
if (PCB_DEBUG)
printf("\t\tPER R: %d P: %d M: %d\n", type_chance[0],
type_chance[1], type_chance[2]);
if (PCB_DEBUG)
printf("\t\tpercent: %d\n", percent);
if (percent || procon || mutual) {
if (procon || mutual) {
this->type = (procon ? consumer : mutual_B);
if (PCB_DEBUG)
printf("\t\tkilled it %s\n", TYPE[this->type]);
}
else {
for (type = LAST_PAIR; type >= regular; type--)
type_chance[type] *= 100 / percent;
type_chance[regular] *= 1.5;
if (PCB_DEBUG)
printf("\t\tCHA R: %d P: %d M: %d\n", type_chance[0],
type_chance[1], type_chance[2]);
chance = rand() % 100;
percent = 0;
for (type = regular; type <= LAST_PAIR; type++) {
percent += type_chance[type];
if (chance < percent) {
this->type = type;
break;
}
}
}
if (this->type == regular && ((MAX_IOCPU[0] == io_count[0]) ||
(MAX_IOCPU[1] == io_count[1]))) {
this->io = (MAX_IOCPU[0] == io_count[0]);
if (PCB_DEBUG)
printf("\t\treg max detected io now %d\n", this->io);
}
else
this->io = rand() % 2;
procon = (type == producer);
mutual = (type == mutual_A);
if (PCB_DEBUG && (procon || mutual))
printf("\t\twe got a live one %s\n", TYPE[this->type]);
type_count[this->type]++;
io_count[this->io] += (type == regular);
}
if (PCB_DEBUG)
printf(
"\t\tCUR R: %d {C: %d I:%d} P: %d A: %d C: %d B: %d\n",
type_count[0], io_count[0], io_count[1], type_count[1],
type_count[2], type_count[3], type_count[4]);
if (PCB_DEBUG)
printf(
"\t\tMAX R: %d {C: %d I:%d} P: %d A: %d C: %d B: %d\n",
MAX_TYPES[0], MAX_IOCPU[0], MAX_IOCPU[1], MAX_TYPES[1],
MAX_TYPES[2], MAX_TYPES[1], MAX_TYPES[2]);
//Priority determination
chance = rand() % 100;
percent = 0;
for (priority = 0; priority < PRIORITIES_TOTAL; priority++) {
percent += PRIORITIES[min(priority, PRIORITY_UNIQUE_UPTO)];
if (chance < percent) {
this->priority = priority;
this->orig_priority = priority;
break;
}
//printf("\npid: %lu chance: %d percent %d priority: %d\n", this->pid, chance, percent, priority);
}
if (this->priority < 0)
error += PCB_PRIORITY_ERROR;
this->state = DEFAULT_STATE;
//this->timeCreate = 0;
this->timeTerminate = DEFAULT_TIME_TERMINATE;
REG_init(this->regs, &error);
int c;
if (this->type != regular && this->type <= LAST_PAIR * 2)
for (c = 0; c < CALL_NUMBER; c++)
this->regs->reg.CODES[c] = CODE_TYPE[this->type][c];
}
return error;
}
int PCBs_available()
{
int type_chance[LAST_PAIR + 1];
int type;
int available = 0;
for (type = LAST_PAIR; type >= regular; type--) {
type_chance[type] = (MAX_TYPES[type] != type_count[type]);
if (type_chance[type])
available++;
}
return available + procon + mutual;
}
/**
* Initializes registers. Must have PCB metadata correct for optimal values.
* @param this
* @param ptr_error
* @return
*/
REG_p REG_init(REG_p this, int *ptr_error)
{
if (this == NULL)
*ptr_error += PCB_NULL_ERROR;
else {
int t;
int j;
int thread_step;
this->reg.pc = DEFAULT_PC;
this->reg.MAX_PC =
rand() % (MAX_PC_RANGE + 1 - MAX_PC_MIN) + MAX_PC_MIN;
this->reg.sw = DEFAULT_SW;
this->reg.term_count = 0; //set to 0 for infinite process
this->reg.TERMINATE = ((int) (rand() % 100 >= TERM_INFINITE_CHANCE)) *
(rand() % TERM_RANGE + 1);
for (t = 0; t < IO_NUMBER * IO_CALLS; t++)
this->reg.IO_TRAPS[(int) (t / IO_CALLS)][t % IO_CALLS] =
MIN_IO_CALL + (rand() % (this->reg.MAX_PC - MIN_IO_CALL));
for (t = 0; t < IO_NUMBER * IO_CALLS; t++)
if (!(this->reg.IO_TRAPS[(int) (t / IO_CALLS)][t % IO_CALLS] %
THREAD_CALL))
this->reg.IO_TRAPS[(int) (t / IO_CALLS)][t %
IO_CALLS] = -1; //dup
else
for (j = 0; j < t; j++)
if (this->reg.IO_TRAPS[(int) (j / IO_CALLS)][j %
IO_CALLS] ==
this->reg.IO_TRAPS[(int) (t / IO_CALLS)][t % IO_CALLS])
this->reg.IO_TRAPS[(int) (j / IO_CALLS)][j %
IO_CALLS] = -1; //duplicate values erased
thread_step = THREAD_CALL * (int) (
((this->reg.MAX_PC - MIN_THREAD_CALL) / CALL_NUMBER) / THREAD_CALL);
for (t = 0; t < CALL_NUMBER; t++) {
this->reg.CALLS[t] = MIN_THREAD_CALL + t * thread_step;
this->reg.CODES[t] = 0;
}
}
return this;
}
/**
* Sets the pid of the process.
*
* @param this
* @param pid the new pid value
* @return
*/
int PCB_setPid(PCB_p this, word pid)
{
// verify pid does not already exist
int error = (this == NULL) * PCB_NULL_ERROR;
if (!error) {
this->pid = pid;
}
return error; // TODO: write
}
/**
* Returns the pid of the process.
*
* @param this
* @return the pid of the process
*/
word PCB_getPid(PCB_p this, int *ptr_error)
{
int error = (this == NULL) * PCB_NULL_ERROR;
if (ptr_error != NULL) {
*ptr_error += error;
}
return error ? ~0 : this->pid; // TODO: write
}
/**
* Sets the state of the process.
*
* @param this
* @param state the new state value
* @return error
*/
int PCB_setState(PCB_p this, enum state_type state)
{
int error = 0;
if (this == NULL) {
error |= PCB_NULL_ERROR;
}
if (state < created || state > terminated) {
error |= PCB_OTHER_ERROR;
}
if (!error) {
this->state = state;
if (state == terminated) {
type_count[this->type]--;
io_count[this->io] -= (this->type == regular);
if (PCB_DEBUG)
printf("\t\tprocess terminated, freed up slot:\n");
if (PCB_DEBUG)
printf(
"\t\tCUR R: %d {C: %d I:%d} P: %d A: %d C: %d B: %d\n",
type_count[0], io_count[0], io_count[1], type_count[1],
type_count[2], type_count[3], type_count[4]);
if (PCB_DEBUG)
printf(
"\t\tMAX R: %d {C: %d I:%d} P: %d A: %d C: %d B: %d\n",
MAX_TYPES[0], MAX_IOCPU[0], MAX_IOCPU[1], MAX_TYPES[1],
MAX_TYPES[2], MAX_TYPES[1], MAX_TYPES[2]);
}
}
return error;
}
/**
* Returns the current state of the process.
*
* @param this
* @return the state of the process
*/
enum state_type PCB_getState(PCB_p this, int *ptr_error)
{
int error = (this == NULL) * PCB_NULL_ERROR;
if (ptr_error != NULL) {
*ptr_error += error;
}
return error ? ~0 : this->state;
}
/**
* Sets the pid of the process.
*
* @param this
* @param pid the new pid value
* @return
*/
int PCB_setPriority(PCB_p this, unsigned short priority)
{
int error = 0;
if (this == NULL) {
error |= PCB_NULL_ERROR;
}
if (priority > LOWEST_PRIORITY) {
error |= PCB_OTHER_ERROR;
}
if (!error) {
this->priority = priority;
}
return error;
}
/**
* Returns the current state of the process.
*
* @param this
* @return the pid of the process
*/
unsigned short PCB_getPriority(PCB_p this, int *ptr_error)
{
int error = (this == NULL) * PCB_NULL_ERROR;
if (ptr_error != NULL) {
*ptr_error += error;
}
return error ? ~0 : this->priority; // TODO: write
}
/**
* Sets the pid of the process.
*
* @param this
* @param pid the new pid value
* @return
*/
int PCB_setPc(PCB_p this, word pc)
{
int error = (this == NULL) * PCB_NULL_ERROR;
if (!error) {
this->regs->reg.pc = pc;
}
return error; // TODO: write
}
/**
* Returns the pc value state of the process.
*
* @param this
* @return the address where the program will resume
*/
word PCB_getPc(PCB_p this, int *ptr_error)
{
int error = (this == NULL) * PCB_NULL_ERROR;
if (ptr_error != NULL) {
*ptr_error += error;
}
return error ? -1 : this->regs->reg.pc; // TODO: write
}
/**
* Sets the pid of the process.
*
* @param this
* @param pid the new pid value
* @return
*/
int PCB_setSw(PCB_p this, word sw)
{
int error = (this == NULL) * PCB_NULL_ERROR;
if (!error) {
this->regs->reg.sw = sw;
}
return error; // TODO: write
}
/**
* Returns the pc value state of the process.
*
* @param this
* @return the address where the program will resume
*/
word PCB_getSw(PCB_p this, int *ptr_error)
{
int error = (this == NULL) * PCB_NULL_ERROR;
if (ptr_error != NULL) {
*ptr_error += error;
}
return error ? ~0 : this->regs->reg.sw; // TODO: write
}
/**
* Returns a string representing the contents of the pcb.
* <br /><em><strong>Note:</strong> parameter string must be 80 chars or more.</em>
* @param this
* @param str
* @param int
* @return string representing the contents of the pc.
*/
char *PCB_toString(PCB_p this, char *str, int *ptr_error)
{
int error = (this == NULL || str == NULL) * PCB_NULL_ERROR;
if (!error) {
str[0] = '\0';
char regString[PCB_TOSTRING_LEN - 1];
if (this->timeTerminate == DEFAULT_TIME_TERMINATE) {
const char *format = "PID: 0x%04lx PC: 0x%05lx State: %s Priority: 0x%x Intensity: %s Type: %s Group: %lu %s TimeCreate: %lu";
snprintf(str, (size_t) PCB_TOSTRING_LEN - 1, format, this->pid,
this->regs->reg.pc,
STATE[this->state], this->orig_priority,
this->io ? "IO" : "CPU", TYPE[this->type], this->group,
Reg_File_toString(this->regs, regString, ptr_error),
this->timeCreate);
} else {
const char *format = "PID: 0x%04lx PC: 0x%05lx State: %s Priority: 0x%x Intensity: %s Type: %s Group: %lu %s TimeCreate: %lu TimeTerminate: %lu";
snprintf(str, (size_t) PCB_TOSTRING_LEN - 1, format, this->pid,
this->regs->reg.pc,
STATE[this->state], this->orig_priority,
this->io ? "IO" : "CPU", TYPE[this->type], this->group,
Reg_File_toString(this->regs, regString, ptr_error),
this->timeCreate, this->timeTerminate);
}
}
if (ptr_error != NULL) {
*ptr_error += error;
}
return str;
}
/**
* Returns a string representing the contents of the regfile of the pcb.
* Note: parameter string must be 80 chars or more.
* @param this
* @param str
* @param int
* @return string representing the contents of the regfile.
*/
char *Reg_File_toString(REG_p this, char *str, int *ptr_error)
{
int error = (this == NULL || str == NULL) * PCB_NULL_ERROR;
if (!error) {
str[0] = '\0';
const char *format = "MaxPC: 0x%05lx Rollover: 0x%05lx Terminate: 0x%05lx";
snprintf(str, (size_t) PCB_TOSTRING_LEN - 1, format, this->reg.MAX_PC,
this->reg.term_count, this->reg.TERMINATE);
}
if (ptr_error != NULL) {
*ptr_error += error;
}
return str;
}