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
60 struct queue_message {
65 void notify_other_processes(int signo) {
67 struct queue_message msg;
71 pid_t pid = processes->pids[i];
73 if (i != 1 && pid != 0) {
75 fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
76 msgsnd(qid, &msg, sizeof(msg), 0);
77 fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process2", strsignal(SIGUSR1), SIGUSR1, pid);
84 * Handler for signals.
86 void sig_handler(int signo)
88 fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
89 if (signo == SIGUSR1) {
90 fprintf(stderr, "[%s] > Notified!\n", "process2");
91 struct queue_message msg;
92 /* Check queues from both other processes */
93 if (msgrcv(qid, &msg, sizeof(int), 2, 0) > 0) {
94 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process2", strsignal(msg.signo[0]));
98 else if (signo == SIGTERM) {
99 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
100 processes->pids[1] = 0;
101 notify_other_processes(signo);
103 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
104 close(read_descriptor);
105 close(write_descriptor);
106 unlink(write_descriptor);
109 else if (signo == SIGTSTP) {
110 fprintf(stderr, "[%s] > Close reading pipe\n", "process2");
111 close(read_descriptor);
112 notify_other_processes(signo);
113 fprintf(stderr, "[%s] > Close writing pipe\n", "process2");
114 close(write_descriptor);
117 else if (signo == SIGCONT) {
118 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
119 notify_other_processes(signo);
121 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
122 write_descriptor = open(write_pipe, O_WRONLY);
123 read_descriptor = open(read_pipe, O_RDONLY);
128 * Program grabs data from process1, calculates number of characters in each line
129 * and pass the value to process3.
133 * Buffer used for storing data from input pipe.
134 * Data is stored in chunks of BUFFER_STEP size.
135 * If data during reading is bigger than this value, then number of
136 * characters is saved, and buffer is cleared for reading another chunk.
138 char buffer[BUFFER_STEP];
140 /** Index used when iterating buffer */
143 /** Stores number of bytes read from input pipe in current iteration */
146 int number_of_characters = 0;
148 fprintf(stderr, "[%s] Init!\n", "process2");
151 * Register signals handled by process
153 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
154 fprintf(stderr, "can't catch SIGUSR1\n");
156 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
157 fprintf(stderr, "can't catch SIGTERM\n");
159 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
160 fprintf(stderr, "can't catch SIGTSTP\n");
162 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
163 fprintf(stderr, "can't catch SIGCONT\n");
167 * Register memory to share with other processes, and pass current
168 * process id to the array.
170 shmid = shmget(shmkey, sizeof(struct message), 0666);
172 processes = (struct message *)shmat(shmid, NULL, 0);
173 processes->pids[1] = getpid();
175 fprintf(stderr, "[%s] Shared pid: %d\n", "process2", getpid());
178 * Register message queue to communicate with other processes
180 qid = msgget(qkey, 0666);
182 /* Reading from process1 */
183 read_descriptor = open(read_pipe, O_RDONLY);
185 /* Writing to process2 */
186 mkfifo(write_pipe, 0666);
187 write_descriptor = open(write_pipe, O_WRONLY);
190 /* Read data from input pipe */
191 count = read(read_descriptor, buffer, BUFFER_STEP);
193 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
196 for (i = 0; i < count; i++, number_of_characters++) {
197 if (buffer[i] == '\n') {
198 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
199 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
200 write(write_descriptor, '\n', 1);
201 number_of_characters = 0;
210 /* Release resources in normal program flow exit. */
211 close(read_descriptor);
212 close(write_descriptor);
213 unlink(write_descriptor);