forked from ShariKing/FuzzyBunnies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserprocesses.c
125 lines (98 loc) · 3.61 KB
/
userprocesses.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
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include "rtx.h"
// *** PROCESS A *** PID 3
void ProcessA(){
// initialize return int, count
int num = 0;
// receive the message from the CCI
envA = receive_message();
// deallocate the message from Proc A's queue in its PCB
// FIX deallocate_message(envA);
while(1){
// request an envelope
tempEnv = request_msg_env();
// set the env type and text
strcpy(tempEnv->msg_type, "count_report");
tempEnv->msg_text[1] = num;
// send to ProcessB
int result = send_message(4, tempEnv);
num++;
release_processor();
}
}
// *** PROCESS B *** PID 4
void ProcessB(){
while(1){
// receive the message from Process A
envB = receive_message();
// send to ProcessB
int Z = send_message(5, envB);
if (Z ==0)
printf("Sending Failed from Proc B");
}
}
// *** PROCESS C *** PID 5
void ProcessC(){
// init the local queue
struct envQ* localQ = (struct envQ *) malloc (sizeof (struct envQ));
if (!localQ) {
printf("localQ in Proc C not created properly");
return;
} // can we use the queue on the proc pcb instead?
// init a PCB and a couple env
struct pcb* CPCB = (struct pcb *) malloc(sizeof (struct pcb));
struct msgenv* envC = (struct msgenv *) malloc (sizeof (struct msgenv));
struct msgenv* envSend = (struct msgenv *) malloc (sizeof (struct msgenv));
CPCB = convert_PID(5);
envSend = request_msg_env();
// infinite loop of normal activity
while (1){
// if theres nothing the localQ, receive a message and enqueue it
if (localQ->head == NULL){
envC = receive_message();
int H = env_ENQ(envC,localQ);
if (H ==0)
printf("Cannot enqueue on localQ");
}
// if there is something on the localQ, dequeue it
else
envC = env_DEQ(localQ);
// if the message type is count report, and the value in msg_text is evenly divisible by 20, display "Process C"
if (envC->msg_type == "count_report" && envC->msg_text % 20 == 0){
// send the display message
strcpy(envC->msg_text, "Process C");
int W = send_console_chars(envSend); // Returns confirmation of sending
if (W==1) {
// if it is the ack message request a delay of 10s, with wakeup code "wakeup10"
int R = request_delay(10, "wakeup10", envC); // request_delay returns an int
if (R==0)
printf("Error with request_delay");
// wait for wakeup message
envC = receive_message();
// if its not the wakeup message put it on the local Q
while (envC->msg_type != "wakeup10"){
envC = receive_message();
envC = env_ENQ(localQ);
}
}
else
printf("Error sending 'Process C' to screen in Proc C");
}
// deallocate envelopes
deallocate_message(envC);
// release processor
int R =release_processor();
}// end of infinite loop
}