Added initial inter-process communication: Sharing pids between processes.
[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 /**
54  * Handler for signals.
55  */
56 void sig_handler(int signo)
57 {
58         fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
59         if (signo == SIGUSR1) {
60         }
61         else if (signo == SIGTERM) {
62                 int i = 0;
63                 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
64                 close(read_descriptor);
65                 close(write_descriptor);
66                 unlink(write_descriptor);
67                 for (; i < 3; i++) {
68                         fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, processes->pids[i]);
69                 }
70                 exit(0);
71         }
72         else if (signo == SIGTSTP) {
73                 fprintf(stderr, "[%s] > Closing pipes\n", "process2");
74                 close(read_descriptor);
75                 close(write_descriptor);
76                 raise (SIGSTOP);
77         }
78         else if (signo == SIGCONT) {
79                 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
80                 read_descriptor = open(read_pipe, O_RDONLY);
81                 write_descriptor = open(write_pipe, O_WRONLY);
82         }
83 }
84
85 /**
86  * Program grabs data from process1, calculates number of characters in each line
87  * and pass the value to process3.
88  */
89 int main(void) {
90         /**
91          * Buffer used for storing data from input pipe.
92          * Data is stored in chunks of BUFFER_STEP size.
93          * If data during reading is bigger than this value, then number of
94          * characters is saved, and buffer is cleared for reading another chunk.
95          */
96         char buffer[BUFFER_STEP];
97
98         /** Index used when iterating buffer */
99         int i = 0;
100
101         /** Stores number of bytes read from input pipe in current iteration */
102         ssize_t count = 0;
103
104         int number_of_characters = 0;
105
106         fprintf(stderr, "[%s] Init!\n", "process2");
107
108         /**
109          * Register signals handled by process
110          */
111         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
112                 fprintf(stderr, "can't catch SIGUSR1\n");
113         }
114         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
115                 fprintf(stderr, "can't catch SIGTERM\n");
116         }
117         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
118                 fprintf(stderr, "can't catch SIGTSTP\n");
119         }
120         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
121                 fprintf(stderr, "can't catch SIGCONT\n");
122         }
123
124         /*
125          * Register memory to share with other processes, and pass current
126          * process id to the array.
127          */
128         shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
129
130         processes = (struct message *)shmat(shmid, NULL, 0);
131         processes->pids[1] = getpid();
132
133         /* Reading from process1 */
134         read_descriptor = open(read_pipe, O_RDONLY);
135
136         /* Writing to process2 */
137         mkfifo(write_pipe, 0666);
138         write_descriptor = open(write_pipe, O_WRONLY);
139
140         while(1) {
141                 /* Read data from input pipe */
142                 count = read(read_descriptor, buffer, BUFFER_STEP);
143
144                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
145
146                 if (count > 0) {
147                         for (i = 0; i < count; i++, number_of_characters++) {
148                                 if (buffer[i] == '\n') {
149                                         fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
150                                         write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
151                                         write(write_descriptor, '\n', 1);
152                                         number_of_characters = 0;
153                                 }
154                         }
155                 }
156                 else {
157                         break;
158                 }
159         }
160
161         /* Release resources in normal program flow exit. */
162         close(read_descriptor);
163         close(write_descriptor);
164         unlink(write_descriptor);
165
166         return 0;
167 }