03aea04af4ae7233f54d1ee3fcb70d469358add5
[wsti_so.git] / src / process2.c
1 #include <stdio.h>
2 /* exit.. */
3 #include <stdlib.h>
4 /* strsignal... */
5 #include <string.h>
6
7 /* open/read/write/close */
8 #include <fcntl.h>
9
10 /* Signals handling.. */
11 #include <signal.h>
12
13 #include <sys/shm.h>
14
15 /** If buffer is too small to hold entire string, it is incremented by this value */
16 #define BUFFER_STEP 16
17
18 /** Named pipe used to communicate with process1 */
19 char * read_pipe = "/tmp/process1pipe";
20
21 /** Named pipe used to communicate with process3 */
22 char * write_pipe = "/tmp/process2pipe";
23
24 /** Descriptor of input pipe */
25 int read_descriptor;
26
27 /** Descriptor of output pipe */
28 int write_descriptor;
29
30
31 /**
32  * Shared memory variables
33  */
34
35 /**
36  * Memory key for processes. Must be same between all processes to properly
37  * communicate.
38  */
39 key_t shmkey = 18912;
40 /**
41  * Id of the shared memory
42  */
43 int shmid;
44
45 /**
46  * Message shared by processes. Contains array of process IDs
47  */
48 struct message {
49         pid_t pids[3];
50 };
51
52 struct message * processes = NULL;
53
54 /**
55  * Message Queue variables
56  */
57
58 /**
59  * Unique key of message queue.
60  */
61 key_t qkey = 12356;
62
63 /**
64  * Queue descriptor.
65  */
66 int qid;
67
68 /**
69  * Structure holding queue message data.
70  * Parameter mtype describes process to whom message is sent.
71  * Parameter signo is a signal to raise after getting message.
72  */
73 struct queue_message {
74         long mtype;
75         int signo[1];
76 };
77
78 void notify_other_processes(int signo) {
79         int i = 0;
80         struct queue_message msg;
81         msg.signo[0] = signo;
82
83         for (; i < 3; i++) {
84                 pid_t pid = processes->pids[i];
85                 // Bleh
86                 if (i != 1 && pid != 0) {
87                         msg.mtype = i+1;
88                         fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
89                         msgsnd(qid, &msg, sizeof(msg), 0);
90                         fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process2", strsignal(SIGUSR1), SIGUSR1, pid);
91                         kill(pid, SIGUSR1);
92                 }
93         }
94 }
95
96 /**
97  * Handler for signals.
98  */
99 void sig_handler(int signo)
100 {
101         fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
102         if (signo == SIGUSR1) {
103                 fprintf(stderr, "[%s] > Notified!\n", "process2");
104                 struct queue_message msg;
105                 /* Check queues from both other processes */
106                 if (msgrcv(qid, &msg, sizeof(int), 2, 0) > 0) {
107                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process2", strsignal(msg.signo[0]));
108                         raise(msg.signo[0]);
109                 }
110         }
111         else if (signo == SIGTERM) {
112                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
113                 processes->pids[1] = 0;
114                 notify_other_processes(signo);
115
116                 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
117                 close(read_descriptor);
118                 close(write_descriptor);
119                 unlink(write_descriptor);
120                 exit(0);
121         }
122         else if (signo == SIGTSTP) {
123                 fprintf(stderr, "[%s] > Close reading pipe\n", "process2");
124                 close(read_descriptor);
125                 notify_other_processes(signo);
126                 fprintf(stderr, "[%s] > Close writing pipe\n", "process2");
127                 close(write_descriptor);
128                 raise (SIGSTOP);
129         }
130         else if (signo == SIGCONT) {
131                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
132                 notify_other_processes(signo);
133
134                 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
135                 write_descriptor = open(write_pipe, O_WRONLY);
136                 read_descriptor = open(read_pipe, O_RDONLY);
137         }
138 }
139
140 /**
141  * Program grabs data from process1, calculates number of characters in each line
142  * and pass the value to process3.
143  */
144 int main(void) {
145         /**
146          * Buffer used for storing data from input pipe.
147          * Data is stored in chunks of BUFFER_STEP size.
148          * If data during reading is bigger than this value, then number of
149          * characters is saved, and buffer is cleared for reading another chunk.
150          */
151         char buffer[BUFFER_STEP];
152
153         /** Index used when iterating buffer */
154         int i = 0;
155
156         /** Stores number of bytes read from input pipe in current iteration */
157         ssize_t count = 0;
158
159         int number_of_characters = 0;
160
161         fprintf(stderr, "[%s] Init!\n", "process2");
162
163         /**
164          * Register signals handled by process
165          */
166         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
167                 fprintf(stderr, "can't catch SIGUSR1\n");
168         }
169         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
170                 fprintf(stderr, "can't catch SIGTERM\n");
171         }
172         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
173                 fprintf(stderr, "can't catch SIGTSTP\n");
174         }
175         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
176                 fprintf(stderr, "can't catch SIGCONT\n");
177         }
178
179         /*
180          * Register memory to share with other processes, and pass current
181          * process id to the array.
182          */
183         shmid = shmget(shmkey, sizeof(struct message), 0666);
184
185         processes = (struct message *)shmat(shmid, NULL, 0);
186         processes->pids[1] = getpid();
187
188         fprintf(stderr, "[%s] Shared pid: %d\n", "process2", getpid());
189
190         /**
191          * Register message queue to communicate with other processes
192          */
193         qid = msgget(qkey, 0666);
194
195         /* Reading from process1 */
196         read_descriptor = open(read_pipe, O_RDONLY);
197
198         /* Writing to process2 */
199         mkfifo(write_pipe, 0666);
200         write_descriptor = open(write_pipe, O_WRONLY);
201
202         while(1) {
203                 /* Read data from input pipe */
204                 count = read(read_descriptor, buffer, BUFFER_STEP);
205
206                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
207
208                 if (count > 0) {
209                         for (i = 0; i < count; i++, number_of_characters++) {
210                                 if (buffer[i] == '\n') {
211                                         fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
212                                         write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
213                                         write(write_descriptor, '\n', 1);
214                                         number_of_characters = 0;
215                                 }
216                         }
217                 }
218                 else {
219                         break;
220                 }
221         }
222
223         /* Release resources in normal program flow exit. */
224         close(read_descriptor);
225         close(write_descriptor);
226         unlink(write_descriptor);
227
228         return 0;
229 }