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