Added initial inter-process communication: Sharing pids between processes.
[wsti_so.git] / src / process3.c
1 #include <stdio.h>
2
3 /* exit.. */
4 #include <stdlib.h>
5
6 /* open/read/write/close */
7 #include <fcntl.h>
8
9 /* Signals handling.. */
10 #include <signal.h>
11
12 #include <sys/shm.h>
13
14 /** Named pipe used to communicate with process2 */
15 char * read_pipe = "/tmp/process2pipe";
16
17 /** Descriptor of input pipe */
18 int read_descriptor;
19
20
21 /**
22  * Shared memory variables
23  */
24 /**
25  * Memory key for processes. Must be same between all processes to properly
26  * communicate.
27  */
28 key_t shmkey = 18912;
29 /**
30  * Id of the shared memory
31  */
32 int shmid;
33
34 /**
35  * Message shared by processes. Contains array of process IDs
36  */
37 struct message {
38         pid_t pids[3];
39 };
40
41 struct message * processes = NULL;
42
43
44 /**
45  * Handler for signals.
46  */
47 void sig_handler(int signo)
48 {
49         fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
50         if (signo == SIGUSR1) {
51         }
52         else if (signo == SIGTERM) {
53                 int i = 0;
54                 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
55                 close(read_descriptor);
56                 for (; i < 3; i++) {
57                         fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, processes->pids[i]);
58                 }
59                 exit(0);
60         }
61         else if (signo == SIGTSTP) {
62                 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
63                 close(read_descriptor);
64                 raise (SIGSTOP);
65         }
66         else if (signo == SIGCONT) {
67                 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
68                 read_descriptor = open(read_pipe, O_RDONLY);
69         }
70 }
71
72 /**
73  * Program grabs data (calculated number of characters) from process2 and prints
74  * grabbed data to the standard output.
75  */
76 int main(void) {
77         /** Buffer used for storing data from input pipe */
78         int buffer = 0;
79         
80         /** Stores number of bytes read from input pipe in current iteration */
81         ssize_t count = 0;
82
83         fprintf(stderr, "[%s] Init!\n", "process3");
84
85         /**
86          * Register signals handled by process
87          */
88         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
89                 fprintf(stderr, "can't catch SIGUSR1\n");
90         }
91         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
92                 fprintf(stderr, "can't catch SIGTERM\n");
93         }
94         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
95                 fprintf(stderr, "can't catch SIGTSTP\n");
96         }
97         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
98                 fprintf(stderr, "can't catch SIGCONT\n");
99         }
100
101         /*
102          * Register memory to share with other processes, and pass current
103          * process id to the array.
104          */
105         shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
106
107         processes = (struct message *)shmat(shmid, NULL, 0);
108         processes->pids[2] = getpid();
109
110         /* Reading from process2 */
111         read_descriptor = open(read_pipe, O_RDONLY);
112
113         while(1) {
114                 /* Read data from input pipe */
115                 count = read(read_descriptor, &buffer, sizeof(int));
116
117                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
118
119                 if (count > 0) {
120                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
121                 }
122                 else {
123                         break;
124                 }
125         }
126
127         /* Release resources in normal program flow exit. */
128         close(read_descriptor);
129
130         return 0;
131 }