Turn On/Off pids on notifying other processes.
[wsti_so.git] / src / process1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.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 #include <sys/msg.h>
13
14
15 /** If buffer is too small to hold entire string, it is incremented by this value */
16 #define BUFFER_STEP 16
17
18 /** Buffer used to store line of characters */
19 char * buffer = NULL;
20
21 /** Named pipe used to communnicate with process2 */
22 char * write_pipe = "/tmp/process1pipe";
23
24 /** File descriptor of pipe */
25 int file_descriptor;
26
27
28 /**
29  * Shared memory variables
30  */
31 /**
32  * Memory key for processes. Must be same between all processes to properly
33  * communicate.
34  */
35 key_t shmkey = 18912;
36 /**
37  * Id of the shared memory
38  */
39 int shmid;
40
41 /**
42  * Message shared by processes. Contains array of process IDs
43  */
44 struct message {
45         pid_t pids[3];
46 };
47
48 struct message * processes = NULL;
49
50 /**
51  * Message Queue variables
52  */
53
54 /**
55  * Unique key of message queue.
56  */
57 key_t qkey = 12356;
58
59 /**
60  * Queue descriptor.
61  */
62 int qid;
63
64 /**
65  * Structure holding queue message data.
66  * Parameter mtype describes process to whom message is sent.
67  * Parameter signo is a signal to raise after getting message.
68  */
69 struct queue_message {
70         long mtype;
71         int signo[1];
72 };
73
74 void notify_other_processes(int signo) {
75         int i = 0;
76         struct queue_message msg;
77         msg.signo[0] = signo;
78
79         for (; i < 3; i++) {
80                 pid_t pid = processes->pids[i];
81                 // Bleh
82                 if (i != 0 && pid != 0) {
83                         msg.mtype = i+1;
84                         fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
85                         msgsnd(qid, &msg, sizeof(int), 0);
86                         fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process1", strsignal(SIGUSR1), SIGUSR1, pid);
87                         kill(pid, SIGUSR1);
88                 }
89         }
90 }
91
92 /**
93  * Handler for signals.
94  */
95 void sig_handler(int signo)
96 {
97         fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
98         if (signo == SIGUSR1) {
99                 fprintf(stderr, "[%s] > Notified!\n", "process1");
100                 struct queue_message msg;
101                 /* Check queues from both other processes */
102                 if (msgrcv(qid, &msg, sizeof(int), 1, 0) > 0) {
103                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process1", strsignal(msg.signo[0]));
104                         raise(msg.signo[0]);
105                 }
106         }
107         else if (signo == SIGTERM) {
108                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process1");
109                 processes->pids[0] = 0;
110                 notify_other_processes(signo);
111
112                 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
113                 close(write_pipe);
114                 unlink(write_pipe);
115
116                 if (buffer) {
117                         free(buffer);
118                         buffer = NULL;
119                 }
120                 exit(0);
121         }
122         else if (signo == SIGTSTP) {
123                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process1");
124                 processes->pids[0] = 0;
125                 notify_other_processes(signo);
126 //              sleep(1);
127                 processes->pids[0] = getpid();
128
129                 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
130                 close(write_pipe);
131                 raise (SIGSTOP);
132         }
133         else if (signo == SIGCONT) {
134                 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
135                 file_descriptor = open(write_pipe, O_WRONLY);
136                 processes->pids[0] = 0;
137                 notify_other_processes(signo);
138 //              sleep(1);
139                 processes->pids[0] = getpid();
140         }
141 }
142
143 /**
144  * Program reads entire lines of text from the standard input and pass them
145  * to the process2 using created pipe.
146  */
147 int main(void) {
148         /** Currently fetched from stdin character */
149         int c;
150
151         /** Current buffer length*/
152         int buffer_length = 0;
153
154         /** Index of the current character */
155         int i = 0;
156
157         /**
158          * Temporary buffer used as a proxy between
159          * checking memory allocation and copying data to real buffer 
160          */
161         char * tmp = NULL;
162
163         fprintf(stderr, "[%s] Init!\n", "process1");
164
165         /**
166          * Register signals handled by process
167          */
168         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
169                 fprintf(stderr, "can't catch SIGUSR1\n");
170         }
171         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
172                 fprintf(stderr, "can't catch SIGTERM\n");
173         }
174         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
175                 fprintf(stderr, "can't catch SIGTSTP\n");
176         }
177         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
178                 fprintf(stderr, "can't catch SIGCONT\n");
179         }
180
181         /*
182          * Register memory to share with other processes, and pass current
183          * process id to the array.
184          */
185         shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
186
187         processes = (struct message *)shmat(shmid, NULL, 0);
188         processes->pids[0] = getpid();
189
190         fprintf(stderr, "[%s] Shared pid: %d\n", "process1", getpid());
191
192         /**
193          * Register message queue to communicate with other processes
194          */
195         qid = msgget(qkey, IPC_CREAT | 0666);
196         
197         mkfifo(write_pipe, 0666);
198
199         file_descriptor = open(write_pipe, O_WRONLY);
200
201         do {
202                 c = fgetc(stdin);
203
204                 /*
205                  * Check if current index is bigger than current buffer size.
206                  * If so increment buffer size. On error release memory, and set
207                  * appropriate flags.
208                  */
209                 if (i >= buffer_length) {
210                         tmp = NULL;
211                         buffer_length += BUFFER_STEP;
212                         tmp = (char*) realloc(buffer, buffer_length);
213                         if (tmp == NULL) {
214                                 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
215                                 free(buffer);
216                                 buffer = NULL;
217                                 c = EOF;
218                         }
219                 }
220
221                 /*
222                  * If there were no errors or it was not just an empty newline:
223                  * parse data.
224                  */
225                 if (c != EOF || ((i == 0) && (c == 10))) {
226                         /* If newline has been found
227                          * return entire string and release the memory
228                          */
229                         if (c == 10 && (i != 0)) {
230                                 buffer[i] = '\n';
231                                 write(file_descriptor, buffer, strlen(buffer));
232                                 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
233
234                                 buffer_length = 0;
235                                 i = 0;
236
237                                 free(buffer);
238                                 buffer = NULL;
239                                 tmp = NULL;
240                         }
241                         /*
242                          * Normal character, add it to the buffer
243                          */
244                         else {
245                                 buffer = tmp;
246                                 buffer[i] = c;
247
248                                 /* Used only for debugging..*/
249                                 /*
250                                 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
251                                 */
252                                 i++;
253                         }
254                 }
255         } while(c != EOF);
256
257         /* Release resources in normal program flow exit. */
258         close(write_pipe);
259         unlink(write_pipe);
260
261         if (buffer) {
262                 free(buffer);
263                 buffer = NULL;
264         }
265
266         return 0;
267 }