5 /* open/read/write/close */
8 /* Signals handling.. */
13 /** If buffer is too small to hold entire string, it is incremented by this value */
14 #define BUFFER_STEP 16
16 /** Named pipe used to communicate with process1 */
17 char * read_pipe = "/tmp/process1pipe";
19 /** Named pipe used to communicate with process3 */
20 char * write_pipe = "/tmp/process2pipe";
22 /** Descriptor of input pipe */
25 /** Descriptor of output pipe */
30 * Shared memory variables
34 * Memory key for processes. Must be same between all processes to properly
39 * Id of the shared memory
44 * Message shared by processes. Contains array of process IDs
50 struct message * processes = NULL;
53 * Message queue variables
61 struct queue_message {
66 * Handler for signals.
68 void sig_handler(int signo)
70 fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
71 if (signo == SIGUSR1) {
72 fprintf(stderr, "[%s] > Notified!\n", "process2");
74 else if (signo == SIGTERM) {
76 struct queue_message msg;
79 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
80 close(read_descriptor);
81 close(write_descriptor);
82 unlink(write_descriptor);
84 pid_t pid = processes->pids[i];
85 fprintf(stderr, "[%s] Process %d, PID: %d\n", "process2", i, pid);
88 msgsnd(pid, &msg, sizeof(msg));
94 else if (signo == SIGTSTP) {
95 fprintf(stderr, "[%s] > Closing pipes\n", "process2");
96 close(read_descriptor);
97 close(write_descriptor);
100 else if (signo == SIGCONT) {
101 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
102 read_descriptor = open(read_pipe, O_RDONLY);
103 write_descriptor = open(write_pipe, O_WRONLY);
108 * Program grabs data from process1, calculates number of characters in each line
109 * and pass the value to process3.
113 * Buffer used for storing data from input pipe.
114 * Data is stored in chunks of BUFFER_STEP size.
115 * If data during reading is bigger than this value, then number of
116 * characters is saved, and buffer is cleared for reading another chunk.
118 char buffer[BUFFER_STEP];
120 /** Index used when iterating buffer */
123 /** Stores number of bytes read from input pipe in current iteration */
126 int number_of_characters = 0;
128 fprintf(stderr, "[%s] Init!\n", "process2");
131 * Register signals handled by process
133 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
134 fprintf(stderr, "can't catch SIGUSR1\n");
136 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
137 fprintf(stderr, "can't catch SIGTERM\n");
139 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
140 fprintf(stderr, "can't catch SIGTSTP\n");
142 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
143 fprintf(stderr, "can't catch SIGCONT\n");
147 * Register memory to share with other processes, and pass current
148 * process id to the array.
150 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
152 processes = (struct message *)shmat(shmid, NULL, 0);
153 processes->pids[1] = getpid();
155 fprintf(stderr, "[%s] Shared pid: %d\n", "process2", getpid());
159 * Register message queue to communicate with other processes
162 qid_input = msgget(qkey, IPC_CREAT | 0666);
164 qid_output1 = msgget(processes->pids[1], IPC_CREAT | 0666);
165 qid_output2 = msgget(processes->pids[2], IPC_CREAT | 0666);
167 /* Reading from process1 */
168 read_descriptor = open(read_pipe, O_RDONLY);
170 /* Writing to process2 */
171 mkfifo(write_pipe, 0666);
172 write_descriptor = open(write_pipe, O_WRONLY);
175 /* Read data from input pipe */
176 count = read(read_descriptor, buffer, BUFFER_STEP);
178 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
181 for (i = 0; i < count; i++, number_of_characters++) {
182 if (buffer[i] == '\n') {
183 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
184 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
185 write(write_descriptor, '\n', 1);
186 number_of_characters = 0;
195 /* Release resources in normal program flow exit. */
196 close(read_descriptor);
197 close(write_descriptor);
198 unlink(write_descriptor);