8 /* open/read/write/close */
11 /* Signals handling.. */
17 /** Named pipe used to communicate with process2 */
18 char * read_pipe = "/tmp/process2pipe";
20 /** Descriptor of input pipe */
25 * Shared memory variables
28 * Memory key for processes. Must be same between all processes to properly
33 * Id of the shared memory
38 * Message shared by processes. Contains array of process IDs
44 struct message * processes = NULL;
47 * Message queue variables
52 struct queue_message {
57 void notify_other_processes(int signo) {
59 struct queue_message msg;
63 pid_t pid = processes->pids[i];
65 if (i != 2 && pid != 0) {
67 fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process3", msg.mtype, msg.signo[0]);
68 msgsnd(qid, &msg, sizeof(int), 0);
69 fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process3", strsignal(SIGUSR1), SIGUSR1, pid);
76 * Handler for signals.
78 void sig_handler(int signo)
80 fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
81 if (signo == SIGUSR1) {
82 fprintf(stderr, "[%s] > Notified!\n", "process3");
83 struct queue_message msg;
84 /* Check queues from both other processes */
85 if (msgrcv(qid, &msg, sizeof(int), 3, 0) > 0) {
86 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process3", strsignal(msg.signo[0]));
90 else if (signo == SIGTERM) {
91 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
92 processes->pids[2] = 0;
93 notify_other_processes(signo);
95 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
96 close(read_descriptor);
99 else if (signo == SIGTSTP) {
100 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
101 notify_other_processes(signo);
103 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
104 close(read_descriptor);
107 else if (signo == SIGCONT) {
108 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
109 notify_other_processes(signo);
111 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
112 read_descriptor = open(read_pipe, O_RDONLY);
117 * Program grabs data (calculated number of characters) from process2 and prints
118 * grabbed data to the standard output.
121 /** Buffer used for storing data from input pipe */
124 /** Stores number of bytes read from input pipe in current iteration */
127 fprintf(stderr, "[%s] Init!\n", "process3");
130 * Register signals handled by process
132 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
133 fprintf(stderr, "can't catch SIGUSR1\n");
135 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
136 fprintf(stderr, "can't catch SIGTERM\n");
138 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
139 fprintf(stderr, "can't catch SIGTSTP\n");
141 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
142 fprintf(stderr, "can't catch SIGCONT\n");
146 * Register memory to share with other processes, and pass current
147 * process id to the array.
149 shmid = shmget(shmkey, sizeof(struct message), 0666);
151 processes = (struct message *)shmat(shmid, NULL, 0);
152 processes->pids[2] = getpid();
154 fprintf(stderr, "[%s] Shared pid: %d\n", "process3", getpid());
157 * Register message queue to communicate with other processes
159 qid = msgget(qkey, 0666);
161 /* Reading from process2 */
162 read_descriptor = open(read_pipe, O_RDONLY);
165 /* Read data from input pipe */
166 count = read(read_descriptor, &buffer, sizeof(int));
168 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
171 fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
178 /* Release resources in normal program flow exit. */
179 close(read_descriptor);