Document Message Queue variables.
[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  * Structure holding array of process IDs.
39  * Message is shared by processes. Contains array of process IDs
40  */
41 struct message {
42         pid_t pids[3];
43 };
44
45 struct message * processes = NULL;
46
47 /**
48  * Message Queue variables
49  */
50
51 /**
52  * Unique key of message queue.
53  */
54 key_t qkey = 12356;
55
56 /**
57  * Queue descriptor.
58  */
59 int qid;
60
61 /**
62  * Structure holding queue message data.
63  * Parameter mtype describes process to whom message is sent.
64  * Parameter signo is a signal to raise after getting message.
65  */
66 struct queue_message {
67         long mtype;
68         int signo[1];
69 };
70
71 void notify_other_processes(int signo) {
72         int i = 0;
73         struct queue_message msg;
74         msg.signo[0] = signo;
75
76         for (; i < 3; i++) {
77                 pid_t pid = processes->pids[i];
78                 // Bleh
79                 if (i != 2 && pid != 0) {
80                         msg.mtype = i+1;
81                         fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process3", msg.mtype, msg.signo[0]);
82                         msgsnd(qid, &msg, sizeof(int), 0);
83                         fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process3", strsignal(SIGUSR1), SIGUSR1, pid);
84                         kill(pid, SIGUSR1);
85                 }
86         }
87 }
88
89 /**
90  * Handler for signals.
91  */
92 void sig_handler(int signo)
93 {
94         fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
95         if (signo == SIGUSR1) {
96                 fprintf(stderr, "[%s] > Notified!\n", "process3");
97                 struct queue_message msg;
98                 /* Check queues from both other processes */
99                 if (msgrcv(qid, &msg, sizeof(int), 3, 0) > 0) {
100                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process3", strsignal(msg.signo[0]));
101                         raise(msg.signo[0]);
102                 }
103         }
104         else if (signo == SIGTERM) {
105                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
106                 processes->pids[2] = 0;
107                 notify_other_processes(signo);
108
109                 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
110                 close(read_descriptor);
111                 exit(0);
112         }
113         else if (signo == SIGTSTP) {
114                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
115                 notify_other_processes(signo);
116
117                 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
118                 close(read_descriptor);
119                 raise (SIGSTOP);
120         }
121         else if (signo == SIGCONT) {
122                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
123                 notify_other_processes(signo);
124
125                 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
126                 read_descriptor = open(read_pipe, O_RDONLY);
127         }
128 }
129
130 /**
131  * Program grabs data (calculated number of characters) from process2 and prints
132  * grabbed data to the standard output.
133  */
134 int main(void) {
135         /** Buffer used for storing data from input pipe */
136         int buffer = 0;
137         
138         /** Stores number of bytes read from input pipe in current iteration */
139         ssize_t count = 0;
140
141         fprintf(stderr, "[%s] Init!\n", "process3");
142
143         /**
144          * Register signals handled by process
145          */
146         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
147                 fprintf(stderr, "can't catch SIGUSR1\n");
148         }
149         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
150                 fprintf(stderr, "can't catch SIGTERM\n");
151         }
152         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
153                 fprintf(stderr, "can't catch SIGTSTP\n");
154         }
155         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
156                 fprintf(stderr, "can't catch SIGCONT\n");
157         }
158
159         /*
160          * Register memory to share with other processes, and pass current
161          * process id to the array.
162          */
163         shmid = shmget(shmkey, sizeof(struct message), 0666);
164
165         processes = (struct message *)shmat(shmid, NULL, 0);
166         processes->pids[2] = getpid();
167
168         fprintf(stderr, "[%s] Shared pid: %d\n", "process3", getpid());
169
170         /**
171          * Register message queue to communicate with other processes
172          */
173         qid = msgget(qkey, 0666);
174
175         /* Reading from process2 */
176         read_descriptor = open(read_pipe, O_RDONLY);
177
178         while(1) {
179                 /* Read data from input pipe */
180                 count = read(read_descriptor, &buffer, sizeof(int));
181
182                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
183
184                 if (count > 0) {
185                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
186                 }
187                 else {
188                         break;
189                 }
190         }
191
192         /* Release resources in normal program flow exit. */
193         close(read_descriptor);
194
195         return 0;
196 }