54bb6587370a53648f0aecbc63a6e2b299ec5176
[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  * Message queue variables
45  */
46 key_t qkey;
47 int qid_input;
48
49 int qid_output1;
50 int qid_output2;
51
52 struct queue_message {
53         int signo;
54 };
55
56 /**
57  * Handler for signals.
58  */
59 void sig_handler(int signo)
60 {
61         fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
62         if (signo == SIGUSR1) {
63                 fprintf(stderr, "[%s] > Notified!\n", "process3");
64         }
65         else if (signo == SIGTERM) {
66                 int i = 0;
67                 struct queue_message msg;
68                 msg.signo = signo;
69
70                 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
71                 close(read_descriptor);
72                 for (; i < 3; i++) {
73                         pid_t pid = processes->pids[i];
74                         fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, pid);
75                         // Bleh
76                         if (i != 2) {
77                                 msgsnd(pid, &msg, sizeof(msg));
78                                 kill(pid, SIGUSR1);
79                         }
80                 }
81                 exit(0);
82         }
83         else if (signo == SIGTSTP) {
84                 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
85                 close(read_descriptor);
86                 raise (SIGSTOP);
87         }
88         else if (signo == SIGCONT) {
89                 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
90                 read_descriptor = open(read_pipe, O_RDONLY);
91         }
92 }
93
94 /**
95  * Program grabs data (calculated number of characters) from process2 and prints
96  * grabbed data to the standard output.
97  */
98 int main(void) {
99         /** Buffer used for storing data from input pipe */
100         int buffer = 0;
101         
102         /** Stores number of bytes read from input pipe in current iteration */
103         ssize_t count = 0;
104
105         fprintf(stderr, "[%s] Init!\n", "process3");
106
107         /**
108          * Register signals handled by process
109          */
110         if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
111                 fprintf(stderr, "can't catch SIGUSR1\n");
112         }
113         if (signal(SIGTERM, sig_handler) == SIG_ERR) {
114                 fprintf(stderr, "can't catch SIGTERM\n");
115         }
116         if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
117                 fprintf(stderr, "can't catch SIGTSTP\n");
118         }
119         if (signal(SIGCONT, sig_handler) == SIG_ERR) {
120                 fprintf(stderr, "can't catch SIGCONT\n");
121         }
122
123         /*
124          * Register memory to share with other processes, and pass current
125          * process id to the array.
126          */
127         shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
128
129         processes = (struct message *)shmat(shmid, NULL, 0);
130         processes->pids[2] = getpid();
131
132         fprintf(stderr, "[%s] Shared pid: %d\n", "process3", getpid());
133
134         sleep(1);
135         /**
136          * Register message queue to communicate with other processes
137          */
138         qkey = getpid();
139         qid_input = msgget(qkey, IPC_CREAT | 0666);
140         
141         qid_output1 = msgget(processes->pids[1], IPC_CREAT | 0666);
142         qid_output2 = msgget(processes->pids[2], IPC_CREAT | 0666);
143
144         /* Reading from process2 */
145         read_descriptor = open(read_pipe, O_RDONLY);
146
147         while(1) {
148                 /* Read data from input pipe */
149                 count = read(read_descriptor, &buffer, sizeof(int));
150
151                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
152
153                 if (count > 0) {
154                         fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
155                 }
156                 else {
157                         break;
158                 }
159         }
160
161         /* Release resources in normal program flow exit. */
162         close(read_descriptor);
163
164         return 0;
165 }