2492ebecfe874f5ed4b4bae357b8e05aa56122f0
[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 key_t qkey;
54 int qid_input;
55
56 int qid_output1;
57 int qid_output2;
58
59 struct queue_message {
60         long mtype;
61         int signo[1];
62 };
63
64 void notify_other_processes(int signo) {
65         int i = 0;
66         struct queue_message msg;
67         msg.signo[0] = signo;
68
69         for (; i < 3; i++) {
70                 pid_t pid = processes->pids[i];
71                 // Bleh
72                 if (i != 0 && pid != 0) {
73                         msg.mtype = i+1;
74                         fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
75                         msgsnd(pid, &msg, sizeof(int), 0);
76                         fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process1", strsignal(SIGUSR1), SIGUSR1, pid);
77                         kill(pid, SIGUSR1);
78                 }
79         }
80 }
81
82 /**
83  * Handler for signals.
84  */
85 void sig_handler(int signo)
86 {
87         fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
88
89         if (signo == SIGUSR1) {
90                 fprintf(stderr, "[%s] > Notified!\n", "process1");
91                 struct queue_message msg;
92                 /* Check queues from both other processes */
93                 if (msgrcv(processes->pids[1], &msg, sizeof(int), 1, 0) > 0) {
94                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process1", strsignal(msg.signo[0]));
95                         raise(msg.signo[0]);
96                         break;
97                 }
98                 else if (msgrcv(processes->pids[2], &msg, sizeof(int), 1, 0) > 0) {
99                         fprintf(stderr, "[%s] > Notified with value: %s!\n", "process1", strsignal(msg.signo[0]));
100                         raise(msg.signo[0]);
101                         break;
102                 }
103         }
104         else if (signo == SIGTERM) {
105                 fprintf(stderr, "[%s] > Signalling other processes..\n", "process1");
106                 processes->pids[1] = 0;
107                 notify_other_processes(signo);
108
109                 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
110                 close(write_pipe);
111                 unlink(write_pipe);
112
113                 if (buffer) {
114                         free(buffer);
115                         buffer = NULL;
116                 }
117                 exit(0);
118         }
119         else if (signo == SIGTSTP) {
120                 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
121                 close(write_pipe);
122                 raise (SIGSTOP);
123         }
124         else if (signo == SIGCONT) {
125                 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
126                 file_descriptor = open(write_pipe, O_WRONLY);
127         }
128 }
129
130 /**
131  * Program reads entire lines of text from the standard input and pass them
132  * to the process2 using created pipe.
133  */
134 int main(void) {
135         /** Currently fetched from stdin character */
136         int c;
137
138         /** Current buffer length*/
139         int buffer_length = 0;
140
141         /** Index of the current character */
142         int i = 0;
143
144         /**
145          * Temporary buffer used as a proxy between
146          * checking memory allocation and copying data to real buffer 
147          */
148         char * tmp = NULL;
149
150         fprintf(stderr, "[%s] Init!\n", "process1");
151
152         /**
153          * Register signals handled by process
154          */
155         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
156                 fprintf(stderr, "can't catch SIGUSR1\n");
157         }
158         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
159                 fprintf(stderr, "can't catch SIGTERM\n");
160         }
161         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
162                 fprintf(stderr, "can't catch SIGTSTP\n");
163         }
164         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
165                 fprintf(stderr, "can't catch SIGCONT\n");
166         }
167
168         /*
169          * Register memory to share with other processes, and pass current
170          * process id to the array.
171          */
172         shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
173
174         processes = (struct message *)shmat(shmid, NULL, 0);
175         processes->pids[0] = getpid();
176
177         fprintf(stderr, "[%s] Shared pid: %d\n", "process1", getpid());
178
179         /**
180          * Register message queue to communicate with other processes
181          */
182         qkey = getpid();
183         qid_input = msgget(qkey, IPC_CREAT | 0666);
184         
185         qid_output1 = msgget(processes->pids[1], IPC_CREAT | 0666);
186         qid_output2 = msgget(processes->pids[2], IPC_CREAT | 0666);
187         
188         mkfifo(write_pipe, 0666);
189
190         file_descriptor = open(write_pipe, O_WRONLY);
191
192         do {
193                 c = fgetc(stdin);
194
195                 /*
196                  * Check if current index is bigger than current buffer size.
197                  * If so increment buffer size. On error release memory, and set
198                  * appropriate flags.
199                  */
200                 if (i >= buffer_length) {
201                         tmp = NULL;
202                         buffer_length += BUFFER_STEP;
203                         tmp = (char*) realloc(buffer, buffer_length);
204                         if (tmp == NULL) {
205                                 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
206                                 free(buffer);
207                                 buffer = NULL;
208                                 c = EOF;
209                         }
210                 }
211
212                 /*
213                  * If there were no errors or it was not just an empty newline:
214                  * parse data.
215                  */
216                 if (c != EOF || ((i == 0) && (c == 10))) {
217                         /* If newline has been found
218                          * return entire string and release the memory
219                          */
220                         if (c == 10 && (i != 0)) {
221                                 buffer[i] = '\n';
222                                 write(file_descriptor, buffer, strlen(buffer));
223                                 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
224
225                                 buffer_length = 0;
226                                 i = 0;
227
228                                 free(buffer);
229                                 buffer = NULL;
230                                 tmp = NULL;
231                         }
232                         /*
233                          * Normal character, add it to the buffer
234                          */
235                         else {
236                                 buffer = tmp;
237                                 buffer[i] = c;
238
239                                 /* Used only for debugging..*/
240                                 /*
241                                 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
242                                 */
243                                 i++;
244                         }
245                 }
246         } while(c != EOF);
247
248         /* Release resources in normal program flow exit. */
249         close(write_pipe);
250         unlink(write_pipe);
251
252         if (buffer) {
253                 free(buffer);
254                 buffer = NULL;
255         }
256
257         return 0;
258 }