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 * Structure holding array of process IDs.
39 * Message is shared by processes. Contains array of process IDs
45 struct message * processes = NULL;
48 * Message Queue variables
52 * Unique key of message queue.
62 * Structure holding queue message data.
63 * Parameter mtype describes process to whom message is sent.
64 * Parameter signo is a signal to raise after getting message.
66 struct queue_message {
71 void notify_other_processes(int signo) {
73 struct queue_message msg;
77 pid_t pid = processes->pids[i];
79 if (i != 2 && pid != 0) {
81 fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process3", msg.mtype, msg.signo[0]);
82 msgsnd(qid, &msg, sizeof(int), 0);
83 fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process3", strsignal(SIGUSR1), SIGUSR1, pid);
90 * Handler for signals.
92 void sig_handler(int signo)
94 fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
95 if (signo == SIGUSR1) {
96 fprintf(stderr, "[%s] > Notified!\n", "process3");
97 struct queue_message msg;
98 /* Check queues from both other processes */
99 if (msgrcv(qid, &msg, sizeof(int), 3, 0) > 0) {
100 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process3", strsignal(msg.signo[0]));
104 else if (signo == SIGTERM) {
105 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
106 processes->pids[2] = 0;
107 notify_other_processes(signo);
109 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
110 close(read_descriptor);
113 else if (signo == SIGTSTP) {
114 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
115 processes->pids[2] = 0;
116 notify_other_processes(signo);
118 processes->pids[2] = getpid();
120 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
121 close(read_descriptor);
124 else if (signo == SIGCONT) {
125 fprintf(stderr, "[%s] > Signalling other processes..\n", "process3");
126 processes->pids[2] = 0;
127 notify_other_processes(signo);
129 processes->pids[2] = getpid();
131 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
132 read_descriptor = open(read_pipe, O_RDONLY);
137 * Program grabs data (calculated number of characters) from process2 and prints
138 * grabbed data to the standard output.
141 /** Buffer used for storing data from input pipe */
144 /** Stores number of bytes read from input pipe in current iteration */
147 fprintf(stderr, "[%s] Init!\n", "process3");
150 * Register signals handled by process
152 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
153 fprintf(stderr, "can't catch SIGUSR1\n");
155 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
156 fprintf(stderr, "can't catch SIGTERM\n");
158 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
159 fprintf(stderr, "can't catch SIGTSTP\n");
161 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
162 fprintf(stderr, "can't catch SIGCONT\n");
166 * Register memory to share with other processes, and pass current
167 * process id to the array.
169 shmid = shmget(shmkey, sizeof(struct message), 0666);
171 processes = (struct message *)shmat(shmid, NULL, 0);
172 processes->pids[2] = getpid();
174 fprintf(stderr, "[%s] Shared pid: %d\n", "process3", getpid());
177 * Register message queue to communicate with other processes
179 qid = msgget(qkey, 0666);
181 /* Reading from process2 */
182 read_descriptor = open(read_pipe, O_RDONLY);
185 /* Read data from input pipe */
186 count = read(read_descriptor, &buffer, sizeof(int));
188 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
191 fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
198 /* Release resources in normal program flow exit. */
199 close(read_descriptor);