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
63 struct queue_message {
68 void notify_other_processes(int signo) {
70 struct queue_message msg;
74 pid_t pid = processes->pids[i];
76 if (i != 1 && pid != 0) {
78 fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
79 msgsnd(pid, &msg, sizeof(msg), 0);
80 fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process2", strsignal(SIGUSR1), SIGUSR1, pid);
87 * Handler for signals.
89 void sig_handler(int signo)
91 fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
92 if (signo == SIGUSR1) {
93 fprintf(stderr, "[%s] > Notified!\n", "process2");
94 struct queue_message msg;
95 /* Check queues from both other processes */
96 if (msgrcv(processes->pids[0], &msg, sizeof(int), 2, 0) > 0) {
97 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process2", strsignal(msg.signo[0]));
101 else if (msgrcv(processes->pids[2], &msg, sizeof(int), 2, 0) > 0) {
102 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process2", strsignal(msg.signo[0]));
107 else if (signo == SIGTERM) {
108 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
109 processes->pids[1] = 0;
110 notify_other_processes(signo);
112 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
113 close(read_descriptor);
114 close(write_descriptor);
115 unlink(write_descriptor);
118 else if (signo == SIGTSTP) {
119 fprintf(stderr, "[%s] > Close reading pipe\n", "process2");
120 close(read_descriptor);
121 notify_other_processes(signo);
122 fprintf(stderr, "[%s] > Close writing pipe\n", "process2");
123 close(write_descriptor);
126 else if (signo == SIGCONT) {
127 fprintf(stderr, "[%s] > Signalling other processes..\n", "process2");
128 notify_other_processes(signo);
130 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
131 write_descriptor = open(write_pipe, O_WRONLY);
132 read_descriptor = open(read_pipe, O_RDONLY);
137 * Program grabs data from process1, calculates number of characters in each line
138 * and pass the value to process3.
142 * Buffer used for storing data from input pipe.
143 * Data is stored in chunks of BUFFER_STEP size.
144 * If data during reading is bigger than this value, then number of
145 * characters is saved, and buffer is cleared for reading another chunk.
147 char buffer[BUFFER_STEP];
149 /** Index used when iterating buffer */
152 /** Stores number of bytes read from input pipe in current iteration */
155 int number_of_characters = 0;
157 fprintf(stderr, "[%s] Init!\n", "process2");
160 * Register signals handled by process
162 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
163 fprintf(stderr, "can't catch SIGUSR1\n");
165 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
166 fprintf(stderr, "can't catch SIGTERM\n");
168 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
169 fprintf(stderr, "can't catch SIGTSTP\n");
171 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
172 fprintf(stderr, "can't catch SIGCONT\n");
176 * Register memory to share with other processes, and pass current
177 * process id to the array.
179 shmid = shmget(shmkey, sizeof(struct message), 0666);
181 processes = (struct message *)shmat(shmid, NULL, 0);
182 processes->pids[1] = getpid();
184 fprintf(stderr, "[%s] Shared pid: %d\n", "process2", getpid());
187 * Register message queue to communicate with other processes
190 qid_input = msgget(qkey, IPC_CREAT | 0666);
192 qid_output1 = msgget(processes->pids[1], IPC_CREAT | 0666);
193 qid_output2 = msgget(processes->pids[2], IPC_CREAT | 0666);
195 /* Reading from process1 */
196 read_descriptor = open(read_pipe, O_RDONLY);
198 /* Writing to process2 */
199 mkfifo(write_pipe, 0666);
200 write_descriptor = open(write_pipe, O_WRONLY);
203 /* Read data from input pipe */
204 count = read(read_descriptor, buffer, BUFFER_STEP);
206 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
209 for (i = 0; i < count; i++, number_of_characters++) {
210 if (buffer[i] == '\n') {
211 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
212 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
213 write(write_descriptor, '\n', 1);
214 number_of_characters = 0;
223 /* Release resources in normal program flow exit. */
224 close(read_descriptor);
225 close(write_descriptor);
226 unlink(write_descriptor);