Using only one message queue.
[wsti_so.git] / src / process3.c
1 #include <stdio.h>
2
3 /* exit.. */
4 #include <stdlib.h>
5 /* strsignal... */
6 #include <string.h>
7
8 /* open/read/write/close */
9 #include <fcntl.h>
10
11 /* Signals handling.. */
12 #include <signal.h>
13
14 #include <sys/shm.h>
15 #include <sys/msg.h>
16
17 /** Named pipe used to communicate with process2 */
18 char * read_pipe = "/tmp/process2pipe";
19
20 /** Descriptor of input pipe */
21 int read_descriptor;
22
23
24 /**
25  * Shared memory variables
26  */
27 /**
28  * Memory key for processes. Must be same between all processes to properly
29  * communicate.
30  */
31 key_t shmkey = 18912;
32 /**
33  * Id of the shared memory
34  */
35 int shmid;
36
37 /**
38  * Message shared by processes. Contains array of process IDs
39  */
40 struct message {
41         pid_t pids[3];
42 };
43
44 struct message * processes = NULL;
45
46 /**
47  * Message queue variables
48  */
49 key_t qkey = 12356;
50 int qid;
51
52 struct queue_message {
53         long mtype;
54         int signo[1];
55 };
56
57 void notify_other_processes(int signo) {
58         int i = 0;
59         struct queue_message msg;
60         msg.signo[0] = signo;
61
62         for (; i < 3; i++) {
63                 pid_t pid = processes->pids[i];
64                 // Bleh
65                 if (i != 2 && pid != 0) {
66                         msg.mtype = i+1;
67                         fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process3", msg.mtype, msg.signo[0]);
68                         msgsnd(qid, &msg, sizeof(int), 0);
69                         fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process3", strsignal(SIGUSR1), SIGUSR1, pid);
70                         kill(pid, SIGUSR1);
71                 }
72         }
73 }
74
75 /**
76  * Handler for signals.
77  */
78 void sig_handler(int signo)
79 {
80         fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
81         if (signo == SIGUSR1) {
82                 fprintf(stderr, "[%s] > Notified!\n", "process3");
83                 struct queue_message msg;
84                 /* Check queues from both other processes */
85                 if (msgrcv(qid, &msg, sizeof(int), 3, 0) > 0) {
86                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process3", strsignal(msg.signo[0]));
87                         raise(msg.signo[0]);
88                 }
89                 else if (msgrcv(qid, &msg, sizeof(int), 3, 0) > 0) {
90                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process3", strsignal(msg.signo[0]));
91                         raise(msg.signo[0]);
92                 }
93         }
94         else if (signo == SIGTERM) {
95                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
96                 processes->pids[2] = 0;
97                 notify_other_processes(signo);
98
99                 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
100                 close(read_descriptor);
101                 exit(0);
102         }
103         else if (signo == SIGTSTP) {
104                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
105                 notify_other_processes(signo);
106
107                 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
108                 close(read_descriptor);
109                 raise (SIGSTOP);
110         }
111         else if (signo == SIGCONT) {
112                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
113                 notify_other_processes(signo);
114
115                 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
116                 read_descriptor = open(read_pipe, O_RDONLY);
117         }
118 }
119
120 /**
121  * Program grabs data (calculated number of characters) from process2 and prints
122  * grabbed data to the standard output.
123  */
124 int main(void) {
125         /** Buffer used for storing data from input pipe */
126         int buffer = 0;
127         
128         /** Stores number of bytes read from input pipe in current iteration */
129         ssize_t count = 0;
130
131         fprintf(stderr, "[%s] Init!\n", "process3");
132
133         /**
134          * Register signals handled by process
135          */
136         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
137                 fprintf(stderr, "can't catch SIGUSR1\n");
138         }
139         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
140                 fprintf(stderr, "can't catch SIGTERM\n");
141         }
142         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
143                 fprintf(stderr, "can't catch SIGTSTP\n");
144         }
145         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
146                 fprintf(stderr, "can't catch SIGCONT\n");
147         }
148
149         /*
150          * Register memory to share with other processes, and pass current
151          * process id to the array.
152          */
153         shmid = shmget(shmkey, sizeof(struct message), 0666);
154
155         processes = (struct message *)shmat(shmid, NULL, 0);
156         processes->pids[2] = getpid();
157
158         fprintf(stderr, "[%s] Shared pid: %d\n", "process3", getpid());
159
160         /**
161          * Register message queue to communicate with other processes
162          */
163         qid = msgget(qkey, 0666);
164
165         /* Reading from process2 */
166         read_descriptor = open(read_pipe, O_RDONLY);
167
168         while(1) {
169                 /* Read data from input pipe */
170                 count = read(read_descriptor, &buffer, sizeof(int));
171
172                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
173
174                 if (count > 0) {
175                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
176                 }
177                 else {
178                         break;
179                 }
180         }
181
182         /* Release resources in normal program flow exit. */
183         close(read_descriptor);
184
185         return 0;
186 }