7 /* open/read/write/close */
10 /* Signals handling.. */
15 /** If buffer is too small to hold entire string, it is incremented by this value */
16 #define BUFFER_STEP 16
18 /** Named pipe used to communicate with process1 */
19 char * read_pipe = "/tmp/process1pipe";
21 /** Named pipe used to communicate with process3 */
22 char * write_pipe = "/tmp/process2pipe";
24 /** Descriptor of input pipe */
27 /** Descriptor of output pipe */
32 * Shared memory variables
36 * Memory key for processes. Must be same between all processes to properly
41 * Id of the shared memory
46 * Message shared by processes. Contains array of process IDs
52 struct message * processes = NULL;
55 * Message Queue variables
59 * Unique key of message queue.
69 * Structure holding queue message data.
70 * Parameter mtype describes process to whom message is sent.
71 * Parameter signo is a signal to raise after getting message.
73 struct queue_message {
78 void notify_other_processes(int signo) {
80 struct queue_message msg;
84 pid_t pid = processes->pids[i];
86 if (i != 1 && pid != 0) {
88 fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
89 msgsnd(qid, &msg, sizeof(msg), 0);
90 fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process2", strsignal(SIGUSR1), SIGUSR1, pid);
97 * Handler for signals.
99 void sig_handler(int signo)
101 fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
102 if (signo == SIGUSR1) {
103 fprintf(stderr, "[%s] > Notified!\n", "process2");
104 struct queue_message msg;
105 /* Check queues from both other processes */
106 if (msgrcv(qid, &msg, sizeof(int), 2, 0) > 0) {
107 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process2", strsignal(msg.signo[0]));
111 else if (signo == SIGTERM) {
112 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
113 processes->pids[1] = 0;
114 notify_other_processes(signo);
116 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
117 close(read_descriptor);
118 close(write_descriptor);
119 unlink(write_descriptor);
122 else if (signo == SIGTSTP) {
123 fprintf(stderr, "[%s] > Close reading pipe\n", "process2");
124 close(read_descriptor);
125 processes->pids[1] = 0;
126 notify_other_processes(signo);
128 processes->pids[1] = getpid();
130 fprintf(stderr, "[%s] > Close writing pipe\n", "process2");
131 close(write_descriptor);
134 else if (signo == SIGCONT) {
135 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
136 processes->pids[1] = 0;
137 notify_other_processes(signo);
139 processes->pids[1] = getpid();
141 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
142 write_descriptor = open(write_pipe, O_WRONLY);
143 read_descriptor = open(read_pipe, O_RDONLY);
148 * Program grabs data from process1, calculates number of characters in each line
149 * and pass the value to process3.
153 * Buffer used for storing data from input pipe.
154 * Data is stored in chunks of BUFFER_STEP size.
155 * If data during reading is bigger than this value, then number of
156 * characters is saved, and buffer is cleared for reading another chunk.
158 char buffer[BUFFER_STEP];
160 /** Index used when iterating buffer */
163 /** Stores number of bytes read from input pipe in current iteration */
166 int number_of_characters = 0;
168 fprintf(stderr, "[%s] Init!\n", "process2");
171 * Register signals handled by process
173 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
174 fprintf(stderr, "can't catch SIGUSR1\n");
176 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
177 fprintf(stderr, "can't catch SIGTERM\n");
179 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
180 fprintf(stderr, "can't catch SIGTSTP\n");
182 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
183 fprintf(stderr, "can't catch SIGCONT\n");
187 * Register memory to share with other processes, and pass current
188 * process id to the array.
190 shmid = shmget(shmkey, sizeof(struct message), 0666);
192 processes = (struct message *)shmat(shmid, NULL, 0);
193 processes->pids[1] = getpid();
195 fprintf(stderr, "[%s] Shared pid: %d\n", "process2", getpid());
198 * Register message queue to communicate with other processes
200 qid = msgget(qkey, 0666);
202 /* Reading from process1 */
203 read_descriptor = open(read_pipe, O_RDONLY);
205 /* Writing to process2 */
206 mkfifo(write_pipe, 0666);
207 write_descriptor = open(write_pipe, O_WRONLY);
210 /* Read data from input pipe */
211 count = read(read_descriptor, buffer, BUFFER_STEP);
213 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
216 for (i = 0; i < count; i++, number_of_characters++) {
217 if (buffer[i] == '\n') {
218 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
219 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
220 write(write_descriptor, '\n', 1);
221 number_of_characters = 0;
230 /* Release resources in normal program flow exit. */
231 close(read_descriptor);
232 close(write_descriptor);
233 unlink(write_descriptor);