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