-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcb.c
1215 lines (1092 loc) · 29 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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "pcb.h"
#include "list.h"
// Comments for functions are in pcb.h
List* zero;
List* one;
List* two;
List* send;
List* receive;
List* semaphores;
pcb* ipcb;
pcb* current;
int id = 1;
int total = 0;
// Placeholder for returning an id
int getId() {
int num = id;
id++;
return num;
}
_Bool idCompare(void* compare, void* input) {
return ( *((int*)input) == ((pcb*)(compare))->id );
}
_Bool msgCompare(void* compare, void* input) {
return ( *((int*)input) == ((message*)(compare))->senderPid );
}
_Bool sidCompare(void* compare, void* input) {
sem* cmp = (sem*)compare;
int in = *((int*)input);
int result = (in == cmp->id);
return result;
}
void messageFree(void* msg) {
if (msg) {
free(msg);
}
}
void PCB_free(void* proc) {
if (proc && List_count(((pcb*)proc)->inbox) > 0) {
List_first(((pcb*)proc)->inbox);
for (int i = 0; i < List_count(((pcb*)proc)->inbox); i++) {
messageFree( ((message*)(List_curr(((pcb*)proc)->inbox)))->text);
List_next(((pcb*)proc)->inbox);
}
free(proc);
}
else if (proc) {
free(proc);
}
return;
}
void SEM_free() {
sem* semaphore;
if (List_count(semaphores) <= 0) {
return;
}
List_first(semaphores);
for (int i = 0; i < List_count(semaphores); i++) {
semaphore = List_curr(semaphores);
List_first(semaphore->waitList);
List_free(semaphore->waitList, PCB_free);
List_remove(semaphores);
free(semaphore);
}
}
pcb* PCB_init(int priority) {
pcb* block = (pcb*)malloc(sizeof(pcb));
if (!block) {
printf("ERROR: Process could not be allocated on heap\n");
return NULL;
}
block->priority = priority;
block->inbox = List_create();
if (block->inbox == NULL) {
printf("ERROR: process inbox could not be created\n");
free(block);
return NULL;
}
block->time = 0;
block->id = getId();
block->reply = NULL;
block->state = READY;
if (priority == 1) {
block->time = 2;
}
total++;
return block;
}
void IPCB_init() {
pcb* block = (pcb*)malloc(sizeof(pcb));
if (!block) {
printf("CRITICAL FAILURE: Init could not be created\n");
exit(1);
}
block->priority = 2;
block->inbox = List_create();
if (!block->inbox) {
printf("CRITICAL FAILURE: Init inbox could not be created\n");
exit(1);
}
block->id = 0;
block->reply = NULL;
block->state = RUNNING;
// Initialize the queues
zero = List_create();
one = List_create();
two = List_create();
send = List_create();
receive = List_create();
semaphores = List_create();
if (!zero || !one || !two || !send || !receive) {
printf("CRITICAL FAILURE: queues could not be created\n");
exit(1);
}
ipcb = block;
}
// For when quantum passes
// Will only switch to a process of the same level of priority as the currently running process
// or higher priority level. If there is only one running process in any level this will
// switch to the init process
void PCB_next(bool flag) {
List* currQueue;
pcb* old = current;
printf("\n");
// Flag indicates "Switch to any running process, from level 0, to 2"
// What it really means is for PCB_next to not try to orient itself
// in any ready queue, i.e., not try to find what process is "next"
// in a queue, because the current process was removed and there are
// no other processes in its priority queue or for some other reason
if (flag) {
if (List_count(zero) > 0) {
if (current) {
current->state = READY;
current = List_first(zero);
current->state = RUNNING;
printf("Switched to pcb %d in level 0\n\n", current->id);
return;
}
}
if (List_count(one) > 0) {
if (current) {
current->state = READY;
current = List_first(one);
current->state = RUNNING;
printf("Switched to pcb %d in level 1\n\n", current->id);
return;
}
}
if (List_count(two) > 0) {
if (current) {
current->state = READY;
current = List_first(two);
current->state = RUNNING;
printf("Switched to pcb %d in level 2\n\n", current->id);
return;
}
}
if (current) {
current->state = READY;
}
current = ipcb;
current->state = RUNNING;
printf("Running init process\n\n");
return;
}
if (current->id == 0 && total > 1 ) {
PCB_next(1);
return;
}
if (total == 0) {
printf("No unblocked processes to switch to\n\n");
current = ipcb;
current->state = RUNNING;
return;
}
if (current->priority == 0) {
currQueue = zero;
}
else if (current->priority == 1) {
currQueue = one;
}
else if (current->priority == 2) {
currQueue = two;
}
else {
printf("NEXT ERROR: Priority of current process could not be determined\n\n");
return;
}
// init process doesn't get time advanced because it's never on a priority queue
current->time++;
// Level 0 has processing time limit of 2 ns
// Level 1 has processing time limit of 5 ns
// Level 2 has no processing time limit
if (List_count(zero) > 0) {
// If current is in level 0
if (currQueue == zero) {
if (List_count(zero) > 1) {
current->state = READY;
// Orient the list
List_first(zero);
List_search(zero, idCompare, ¤t->id);
if (List_next(zero) == NULL) {
current = List_first(zero);
}
else {
// Checking List_next advances the list's current item
// so just set current process to List_curr
current = List_curr(zero);
}
current->state = RUNNING;
printf("Switched to pcb %d in level 0\n", current->id);
}
else {
printf("Not switching\n");
}
if (old->time >= 2) {
printf("pcb %d has been demoted\n", old->id);
List_first(zero);
List_search(zero, idCompare, &old->id);
List_remove(zero);
List_append(one, old);
old->priority = 1;
printf("Moved pcb %d to level 1\n", old->id);
}
printf("\n");
return;
}
// If current is not in level 0 but there are pcbs in level 0
else {
current->state = READY;
current = List_first(zero);
current->state = RUNNING;
printf("Switched to pcb %d in level 0\n", current->id);
printf("\n");
return;
}
}
else if (List_count(one) > 0) {
// If current is in level 0
if (currQueue == one) {
if (List_count(one) > 1) {
current->state = READY;
// Orient the list
List_first(one);
List_search(one, idCompare, ¤t->id);
if (List_next(one) == NULL) {
current = List_first(one);
}
else {
// Checking List_next advances the list's current item
// so just set current process to List_curr
current = List_curr(one);
}
current->state = RUNNING;
printf("Switched to pcb %d in level 1\n", current->id);
}
else {
printf("Not switching\n");
}
if (old->time >= 7) {
printf("pcb %d has been demoted\n", old->id);
List_first(one);
List_search(one, idCompare, &old->id);
List_remove(one);
List_append(two, old);
old->priority = 2;
printf("Moved pcb %d to level 2\n", old->id);
}
printf("\n");
return;
}
// If current is not in level 1 but there are pcbs in level 1
else {
current->state = READY;
current = List_first(one);
current->state = RUNNING;
printf("Switched to pcb %d in level 1\n", current->id);
printf("\n");
return;
}
}
else if (List_count(two) > 0) {
// If current is in level 0
if (currQueue == two) {
if (List_count(two) > 1) {
current->state = READY;
// Orient the list
List_first(two);
List_search(two, idCompare, ¤t->id);
if (List_next(two) == NULL) {
current = List_first(two);
}
else {
// Checking List_next advances the list's current item
// so just set current process to List_curr
current = List_curr(two);
}
current->state = RUNNING;
printf("Switched to pcb %d in level 2\n\n", current->id);
return;
}
else {
printf("Not switching\n\n");
return;
}
}
// If current is not in level 2 but there are pcbs in level 2
// Would never reach here, but just in case
else {
current->state = READY;
current = List_first(two);
current->state = RUNNING;
printf("Switched to pcb %d in level 2\n\n", current->id);
return;
}
}
printf("NEXT ERROR: Something went wrong\n\n");
return;
}
// Displays all the information of every process running or not
// Does not print the messages in any of the processes, only the number in inbox
void PCB_displayAll() {
pcb* printPCB;
printf("\n###################### SYSTEM STATE ######################\n\n");
printf("%d Total processes\n\n", total);
printf("Level 0:\n%d\n", List_count(zero));
if (List_count(zero) <= 0) {
printf("\tNo processes in level 0\n\n");
}
else {
List_first(zero);
for (int i = 0; i < List_count(zero); i++) {
printPCB = List_curr(zero);
printf("\tid: %d\n", printPCB->id);
printf("\tpriority: %d\n", printPCB->priority);
printf("\tstate: %s\n", printPCB->state);
printf("\tmessages in inbox: %d\n", List_count(printPCB->inbox));
printf("\n");
List_next(zero);
}
}
printf("Level 1:\n%d\n\n", List_count(one));
if (List_count(one) <= 0) {
printf("\tNo processes in level 1\n\n");
}
else {
List_first(one);
for (int i = 0; i < List_count(one); i++) {
printPCB = List_curr(one);
printf("\tid: %d\n", printPCB->id);
printf("\tpriority: %d\n", printPCB->priority);
printf("\tstate: %s\n", printPCB->state);
printf("\tmessages in inbox: %d\n", List_count(printPCB->inbox));
printf("\n");
List_next(one);
}
}
printf("Level 2:\n%d\n\n", List_count(two));
if (List_count(two) <= 0) {
printf("\tNo processes in level 2\n\n");
}
else {
List_first(two);
for (int i = 0; i < List_count(two); i++) {
printPCB = List_curr(two);
printf("\tid: %d\n", printPCB->id);
printf("\tpriority: %d\n", printPCB->priority);
printf("\tstate: %s\n", printPCB->state);
printf("\tmessages in inbox: %d\n", List_count(printPCB->inbox));
printf("\n");
List_next(two);
}
}
printf("Send queue:\n%d\n\n", List_count(send));
if (List_count(send) <= 0) {
printf("\tNo processes in send queue\n\n");
}
else {
List_first(send);
for (int i = 0; i < List_count(send); i++) {
printPCB = List_curr(send);
printf("\tid: %d\n", printPCB->id);
printf("\tpriority: %d\n", printPCB->priority);
printf("\tstate: %s\n", printPCB->state);
printf("\tmessages in inbox: %d\n", List_count(printPCB->inbox));
printf("\n");
List_next(send);
}
}
printf("Receive queue:\n%d\n\n", List_count(receive));
if (List_count(receive) <= 0) {
printf("\tNo processes in receive queue\n\n");
}
else {
List_first(receive);
for (int i = 0; i < List_count(receive); i++) {
printPCB = List_curr(receive);
printf("\tid: %d\n", printPCB->id);
printf("\tpriority: %d\n", printPCB->priority);
printf("\tstate: %s\n", printPCB->state);
printf("\tmessages in inbox: %d\n", List_count(printPCB->inbox));
printf("\n");
List_next(receive);
}
}
if (List_count(semaphores) > 0) {
printf("Semaphores\n%d\n\n", List_count(semaphores));
List_first(semaphores);
for (int i = 0; i < List_count(semaphores); i++) {
printf("\tsem id: %d\n", ((sem*)List_curr(semaphores))->id);
printf("\tsem current value: %d\n", ((sem*)List_curr(semaphores))->val);
printf("\tpcbs in waitlist: %d\n\n", List_count(((sem*)List_curr(semaphores))->waitList));
List_next(semaphores);
}
}
else {
printf("No semaphores\n\n");
}
printf("##########################################################\n\n");
}
void PCB_display(pcb* process) {
if (process == NULL) {
printf("ERROR: Process to be displayed is NULL\n");
return;
}
printf("\nid: %d\n", process->id);
printf("priority: %d\n", process->priority);
printf("state: %s\n", process->state);
printf("messages in inbox: %d\n", List_count(process->inbox));
printf("\n");
}
void PCB_fork(pcb* process) {
printf("\n");
if (process == NULL) {
printf("ERROR: Cannot fork process, for it is NULL\n\n");
return;
}
else if (process->id == 0) {
printf("ERROR: Cannot fork init process\n\n");
return;
}
pcb* copy = (pcb*)malloc(sizeof(pcb));
if (copy == NULL) {
printf("ERROR: Copy could not be allocated\n\n");
return;
}
copy->id = getId();
// Doesn't need to copy contents of the inbox
copy->inbox = List_create();
copy->priority = process->priority;
copy->state = READY;
copy->time = 0;
switch(copy->priority) {
case 0:
List_append(zero, copy);
break;
case 1:
List_append(one, copy);
break;
case 2:
List_append(two, copy);
break;
default:
printf("ERROR: Something went wrong in fork process, aborting\n\n");
free(copy);
return;
}
printf("Process successfuly forked\n");
printf("Forked process id: %d\n", copy->id);
total++;
printf("\n");
return;
}
void PCB_kill(int id) {
pcb* destroy;
List* currQueue; // The queue the current process is in
List* destQueue; // The queue the to-be-ended process is in
sem* destSem; // The semaphore the queue could be in
printf("\n");
if (id == 0 && total > 0) {
printf("ERROR: Cannot end init process while other processes are in system\n\n");
return;
}
else if (id == 0 && total <= 0) {
printf("Exiting os-sim\nGoodbye\n");
List_free(ipcb->inbox, messageFree);
free(ipcb);
SEM_free();
exit(1);
}
// Need to find the queue the current process belongs to
// in case we are killing the current process
List_first(send);
List_first(receive);
List_first(semaphores);
if (current->priority == 0) {
currQueue = zero;
} else if (current->priority == 1) {
currQueue = one;
} else if (current->priority == 2) {
currQueue = two;
} else {
printf("KILL ERROR: Something went wrong, check if current is in a ready queue\n\n");
return;
}
// Case where we are ending the current process
if (current->id == id) {
destroy = current;
// Case where the current to-be-destroyed process
// is the only one in its ready queue
if (List_count(currQueue) == 1) {
List_first(currQueue);
List_search(currQueue, idCompare, &destroy->id);
if (List_remove(currQueue) == NULL) {
printf("LIST ERROR: Item could not be removed, try again\n");
return;
}
List_free(destroy->inbox, messageFree);
if (destroy->reply) {
free(destroy->reply);
}
free(destroy);
total--;
PCB_next(1);
printf("Ended current process\n\n");
return;
}
// Case where the current to-be-destroyed process
// is NOT the only one in its ready queue
// PCB_next(NULL) tells PCB_next to orient itself
// in regard to whre the current process is in its ready
// queue and to properly transfer to the next process in that queue
else {
PCB_next(NULL);
List_first(currQueue);
List_search(currQueue, idCompare, &destroy->id);
if (List_remove(currQueue) == NULL) {
printf("LIST ERROR: Item could not be removed, try again\n");
return;
}
List_free(destroy->inbox, messageFree);
if (destroy->reply) {
free(destroy->reply);
}
free(destroy);
total--;
printf("Ended current process\n\n");
return;
}
}
// Searching for the process to be ended
// List is kind of crappy so all lists have
// to be set to first to actually search entire list
List_first(zero);
List_first(one);
List_first(two);
List_first(send);
List_first(receive);
List_first(semaphores);
if (List_search(zero, idCompare, &id)) {
destQueue = zero;
}
else if (List_search(one, idCompare, &id)) {
destQueue = one;
}
else if (List_search(two, idCompare, &id)) {
destQueue = two;
}
else if (List_search(send, idCompare, &id)) {
destQueue = send;
}
else if (List_search(receive, idCompare, &id)) {
destQueue = receive;
}
// Searching for a pcb with id in all semaphores
else if (List_count(semaphores) > 0 ) {
for (int i = 0; i < List_count(semaphores); i++) {
if (List_count( ((sem*)List_curr(semaphores))->waitList) > 0) {
List_first( ((sem*)List_curr(semaphores))->waitList);
if (List_search(((sem*)List_curr(semaphores))->waitList, idCompare, &id) ) {
destQueue = ((sem*)List_curr(semaphores))->waitList;
destSem = ((sem*)List_curr(semaphores));
break;
}
}
List_next(semaphores);
}
}
else {
printf("A process with that id could not be found anywhere\n\n");
return;
}
destroy = List_curr(destQueue);
if (List_remove(destQueue) == NULL) {
printf("LIST ERROR: Item could not be removed, try again\n");
return;
}
if (destSem && destQueue == destSem->waitList) {
destSem->val++;
printf("Found process removed from a semaphore\n");
printf("Semaphore value increased\n");
}
List_free(destroy->inbox, messageFree);
free(destroy);
total--;
printf("Successfuly found and ended process\n\n");
return;
}
// A deeper send function with more messy arguments
int mail(List* fromQueue, List* toQueue, int pid, message* msg) {
pcb* target;
pcb* sender = current;
List* returnQueue; // Queue to return recipient process to if it's in receive queue
printf("In mail\n");
if (fromQueue == NULL || pid < 0 || msg == NULL) {
printf("SEND ERROR: Something went wrong sending message\n\n");
return 1;
}
if (pid == current->id) {
List_append(current->inbox, msg);
printf("You sent a message to yourself\n\n");
return 0;
}
// If toQueue is passed in as NULL that means send to ipcb, the init process
if (!toQueue) {
target = ipcb;
} else {
List_first(toQueue);
target = List_search(toQueue, idCompare, &pid);
}
if (target) {
if (toQueue && target->priority == 0) {
returnQueue = zero;
}
else if (toQueue && target->priority == 1) {
returnQueue = one;
}
else if (toQueue && target->priority == 2) {
returnQueue = two;
}
List_first(fromQueue);
List_append(target->inbox, msg);
if (toQueue == receive) {
List_remove(toQueue);
List_append(returnQueue, target);
target->state = READY;
printf("Recipient now unblocked\n\n");
}
if (List_count(fromQueue) <= 1) {
List_first(fromQueue);
List_search(fromQueue, idCompare, &sender->id);
List_remove(fromQueue);
PCB_next(1);
} else {
PCB_next(NULL);
List_first(fromQueue);
List_search(fromQueue, idCompare, &sender->id);
List_remove(fromQueue);
}
List_append(send, sender);
sender->state = BLOCKED;
return 0;
}
printf("ERROR: Something went wrong sending message\n\n");
return 1;
}
// Function called from main
// Clean interface, the function does some detective work to search
// for the target, etc., then hands off the actual sending to the mail function
int PCB_send(int pid, message* msg) {
List* currQueue; // The queue the current process is in
List* destQueue;
pcb* sender = current;
int result;
printf("\n");
if (pid == ipcb->id && current->id == 0) {
printf("You sent a message to yourself\n\n");
List_append(ipcb->inbox, msg);
return 0;
}
// Processes that are in a ready queue are only allowed
// to send messages, so it is assumed current is in levels 0, 1, or 2
if (sender->priority == 0) {
currQueue = zero;
} else if (sender->priority == 1) {
currQueue = one;
} else if (sender->priority == 2) {
currQueue = two;
} else {
printf("ERROR: Something went wrong sending message\n\n");
return -1;
}
List_first(zero);
List_first(one);
List_first(two);
List_first(send);
List_first(receive);
List_first(semaphores);
if (pid == 0) {
result = mail(currQueue, NULL, pid, msg);
}
else if (List_search(zero, idCompare, &pid)) {
result = mail(currQueue, zero, pid, msg);
}
else if (List_search(one, idCompare, &pid)) {
result = mail(currQueue, one, pid, msg);
}
else if (List_search(two, idCompare, &pid)) {
result = mail(currQueue, two, pid, msg);
}
else if (List_search(send, idCompare, &pid)) {
result = mail(currQueue, send, pid, msg);
}
else if (List_search(receive, idCompare, &pid)) {
result = mail(currQueue, receive, pid, msg);
}
// Searching all semaphores for a pcb with recipient id
// The target pcb being blocked on a semaphore works the same
// way as if the target were blocked on the send queue,
// getting a message won't unblock the target pcb
else if (List_count(semaphores) > 0 ) {
int semCount = List_count(semaphores);
for (int i = 0; i < semCount; i++) {
sem* currSem = List_curr(semaphores);
int waitCount = List_count(currSem->waitList);
if (waitCount > 0) {
List_first(currSem->waitList);
if (List_search(currSem->waitList, idCompare, &pid) ) {
destQueue = currSem->waitList;
result = mail(currQueue, destQueue, pid, msg);
break;
}
}
List_next(semaphores);
}
}
else {
printf("Process with that id could not be found\n\n");
return -1;
}
if (!result) {
return 0;
} else {
printf("Something went wrong\n\n");
return -1;
}
}
void PCB_receive() {
List* currQueue;
pcb* recipient = current;
printf("\n");
if (List_count(current->inbox) > 0) {
printf("Total messages in inbox: %d\n", List_count(current->inbox));
printf("Oldest message in inbox:\n\n");
printf("\t%s\n\n", ((message*)(List_first(current->inbox)))->text);
printf("From process %d\n\n", ((message*)(List_first(current->inbox)))->senderPid);
return;
}
printf("There are no messages in inbox\n");
if (recipient->priority == 0) {
currQueue = zero;
} else if (recipient->priority == 1) {
currQueue = one;
} else if (recipient->priority == 2) {
currQueue = two;
} else {
printf("ERROR: Something went wrong, check current process is in a ready queue\n");
return;
}
if (List_count(currQueue) <= 1 && current->id != 0) {
List_first(currQueue);
List_search(currQueue, idCompare, &recipient->id);
List_remove(currQueue);
PCB_next(1);
List_append(receive, recipient);
recipient->state = BLOCKED;
printf("Current process has been blocked and placed on receive queue\n");
printf("Process will become unblocked when it receives a message\n\n");
return;
}
else if (List_count(currQueue) > 1 && current->id != 0) {
PCB_next(NULL);
List_first(currQueue);
List_search(currQueue, idCompare, &recipient->id);
List_remove(currQueue);
List_append(receive, recipient);
recipient->state = BLOCKED;
printf("Current process has been blocked and placed on receive queue\n");
printf("Process will become unblocked when it receives a message\n\n");
return;
}
else if (current->id == 0) {
printf("Running init, process not blocked\n\n");
}
else {
printf("RECEIVE ERROR: Something went wrong\n\n");
return;
}
}
int PCB_reply(int senderPid, char* reply) {
List* returnQueue;
pcb* sender;
message* msg;
if (List_count(current->inbox) <= 0) {
printf("No messages in inbox to reply to\n\n");
free(reply);
return -1;
}
List_first(current->inbox);
if (List_search(current->inbox, msgCompare, &senderPid) == NULL) {
printf("A message from given pid is not in inbox\n\n");
free(reply);
return -1;
}
if (senderPid == current->id) {
printf("Message was from yourself\n");
msg = (message*)List_curr(current->inbox);
List_remove(current->inbox);
free(msg);
// Reply is not needed
free(reply);
printf("Message freed\n");
return 0;
}
if (senderPid == 0) {
msg = (message*)List_curr(current->inbox);
List_remove(current->inbox);
free(msg);
ipcb->reply = reply;
printf("Reply sent\n\n");
return 0;
}
List_first(send);
if (List_search(send, idCompare, &senderPid)) {
sender = List_curr(send);
List_remove(send);
if (sender->priority == 0) {
returnQueue = zero;
}
else if (sender->priority == 1) {
returnQueue = one;
}
else if (sender->priority == 2) {
returnQueue = two;
}
List_append(returnQueue, sender);
sender->state = READY;
sender->reply = reply;
printf("Sender %d unblocked from send queue\n\n", sender->id);
msg = (message*)List_curr(current->inbox);
List_remove(current->inbox);
free(msg);
return 0;
}
else {
printf("Sender could not be found in send queue\n\n");
free(reply);
return -1;
}
}
void PCB_dump(int id) {
pcb* process;
List_first(zero);
List_first(one);
List_first(two);
List_first(send);
List_first(receive);
if (id == 0) {
process = ipcb;
}
else if (List_search(zero, idCompare, &id)) {
process = List_curr(zero);
}
else if (List_search(one, idCompare, &id)) {
process = List_curr(one);
}
else if (List_search(two, idCompare, &id)) {
process = List_curr(one);
}
else if (List_search(send, idCompare, &id)) {
process = List_curr(one);
}
else if (List_search(receive, idCompare, &id)) {
process = List_curr(one);
}
else if (List_count(semaphores) > 0 ) {
for (int i = 0; i < List_count(semaphores); i++) {
if (List_count( ((sem*)List_curr(semaphores))->waitList) > 0) {
List_first( ((sem*)List_curr(semaphores))->waitList);
if (List_search(((sem*)List_curr(semaphores))->waitList, idCompare, &id) ) {
process = List_curr(((sem*)List_curr(semaphores))->waitList);
break;
}
}
List_next(semaphores);
}
}
else {
printf("A process with that id could not be found\n");
return;
}