6 /* open/read/write/close */
9 /* Signals handling.. */
14 /** Named pipe used to communicate with process2 */
15 char * read_pipe = "/tmp/process2pipe";
17 /** Descriptor of input pipe */
22 * Shared memory variables
25 * Memory key for processes. Must be same between all processes to properly
30 * Id of the shared memory
35 * Message shared by processes. Contains array of process IDs
41 struct message * processes = NULL;
44 * Message queue variables
52 struct queue_message {
57 * Handler for signals.
59 void sig_handler(int signo)
61 fprintf(stderr, "[%s] Received %s!\n", "process3", strsignal(signo));
62 if (signo == SIGUSR1) {
63 fprintf(stderr, "[%s] > Notified!\n", "process3");
65 else if (signo == SIGTERM) {
67 struct queue_message msg;
70 fprintf(stderr, "[%s] > Releasing resources\n", "process3");
71 close(read_descriptor);
73 pid_t pid = processes->pids[i];
74 fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, pid);
77 msgsnd(pid, &msg, sizeof(msg));
83 else if (signo == SIGTSTP) {
84 fprintf(stderr, "[%s] > Closing pipe\n", "process3");
85 close(read_descriptor);
88 else if (signo == SIGCONT) {
89 fprintf(stderr, "[%s] > Opening pipe\n", "process3");
90 read_descriptor = open(read_pipe, O_RDONLY);
95 * Program grabs data (calculated number of characters) from process2 and prints
96 * grabbed data to the standard output.
99 /** Buffer used for storing data from input pipe */
102 /** Stores number of bytes read from input pipe in current iteration */
105 fprintf(stderr, "[%s] Init!\n", "process3");
108 * Register signals handled by process
110 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
111 fprintf(stderr, "can't catch SIGUSR1\n");
113 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
114 fprintf(stderr, "can't catch SIGTERM\n");
116 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
117 fprintf(stderr, "can't catch SIGTSTP\n");
119 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
120 fprintf(stderr, "can't catch SIGCONT\n");
124 * Register memory to share with other processes, and pass current
125 * process id to the array.
127 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
129 processes = (struct message *)shmat(shmid, NULL, 0);
130 processes->pids[2] = getpid();
132 fprintf(stderr, "[%s] Shared pid: %d\n", "process3", getpid());
136 * Register message queue to communicate with other processes
139 qid_input = msgget(qkey, IPC_CREAT | 0666);
141 qid_output1 = msgget(processes->pids[1], IPC_CREAT | 0666);
142 qid_output2 = msgget(processes->pids[2], IPC_CREAT | 0666);
144 /* Reading from process2 */
145 read_descriptor = open(read_pipe, O_RDONLY);
148 /* Read data from input pipe */
149 count = read(read_descriptor, &buffer, sizeof(int));
151 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process3", count);
154 fprintf(stderr, "[%s] Process2 send: %d\n", "process3", buffer);
161 /* Release resources in normal program flow exit. */
162 close(read_descriptor);