Added signal handlers for all processes.
[wsti_so.git] / src / process3.c
1 #include <stdio.h>
2
3 /* open/read/write/close */
4 #include <fcntl.h>
5
6 /* Signals handling.. */
7 #include <signal.h>
8
9 /**
10  * Handler for signals.
11  */
12 void sig_handler(int signo)
13 {
14         if (signo == SIGUSR1) {
15                 fprintf(stderr, "[%s] SIGUSR1!\n", "process1");
16         }
17         else if (signo == SIGUSR2) {
18                 fprintf(stderr, "[%s] SIGUSR2!\n", "process1");
19         }
20         else if (signo == SIGINT) {
21                 fprintf(stderr, "[%s] SIGINT!\n", "process1");
22         }
23         else if (signo == SIGCONT) {
24                 fprintf(stderr, "[%s] SIGCONT!\n", "process1");
25         }
26 }
27
28 /**
29  * Program grabs data (calculated number of characters) from process2 and prints
30  * grabbed data to the standard output.
31  */
32 int main(void) {
33         /** Named pipe used to communicate with process2 */
34         char * read_pipe = "/tmp/process2pipe";
35
36         /** Descriptor of input pipe */
37         int read_descriptor;
38
39         /** Buffer used for storing data from input pipe */
40         int buffer = 0;
41         
42         /** Stores number of bytes read from input pipe in current iteration */
43         ssize_t count = 0;
44
45         fprintf(stderr, "[%s] Init!\n", "process3");
46
47         /**
48          * Register signals handled by process
49          */
50         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
51                 fprintf(stderr, "can't catch SIGUSR1\n");
52         }
53         if (signal(SIGUSR2, sig_handler) == SIG_ERR) {
54                 fprintf(stderr, "can't catch SIGUSR2\n");
55         }
56         if (signal(SIGINT, sig_handler) == SIG_ERR) {
57                 fprintf(stderr, "can't catch SIGINT\n");
58         }
59         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
60                 fprintf(stderr, "can't catch SIGCONT\n");
61         }
62
63         /* Reading from process2 */
64         read_descriptor = open(read_pipe, O_RDONLY);
65
66         while(1) {
67                 /* Read data from input pipe */
68                 count = read(read_descriptor, &buffer, sizeof(int));
69
70                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
71
72                 if (count > 0) {
73                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
74                 }
75                 else {
76                         break;
77                 }
78         }
79
80         close(read_descriptor);
81
82         return 0;
83 }