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;
54 * Handler for signals.
56 void sig_handler(int signo)
58 fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
59 if (signo == SIGUSR1) {
61 else if (signo == SIGTERM) {
63 fprintf(stderr, "[%s] > Releasing resources\n", "process2");
64 close(read_descriptor);
65 close(write_descriptor);
66 unlink(write_descriptor);
68 fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, processes->pids[i]);
72 else if (signo == SIGTSTP) {
73 fprintf(stderr, "[%s] > Closing pipes\n", "process2");
74 close(read_descriptor);
75 close(write_descriptor);
78 else if (signo == SIGCONT) {
79 fprintf(stderr, "[%s] > Opening pipes\n", "process2");
80 read_descriptor = open(read_pipe, O_RDONLY);
81 write_descriptor = open(write_pipe, O_WRONLY);
86 * Program grabs data from process1, calculates number of characters in each line
87 * and pass the value to process3.
91 * Buffer used for storing data from input pipe.
92 * Data is stored in chunks of BUFFER_STEP size.
93 * If data during reading is bigger than this value, then number of
94 * characters is saved, and buffer is cleared for reading another chunk.
96 char buffer[BUFFER_STEP];
98 /** Index used when iterating buffer */
101 /** Stores number of bytes read from input pipe in current iteration */
104 int number_of_characters = 0;
106 fprintf(stderr, "[%s] Init!\n", "process2");
109 * Register signals handled by process
111 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
112 fprintf(stderr, "can't catch SIGUSR1\n");
114 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
115 fprintf(stderr, "can't catch SIGTERM\n");
117 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
118 fprintf(stderr, "can't catch SIGTSTP\n");
120 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
121 fprintf(stderr, "can't catch SIGCONT\n");
125 * Register memory to share with other processes, and pass current
126 * process id to the array.
128 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
130 processes = (struct message *)shmat(shmid, NULL, 0);
131 processes->pids[1] = getpid();
133 /* Reading from process1 */
134 read_descriptor = open(read_pipe, O_RDONLY);
136 /* Writing to process2 */
137 mkfifo(write_pipe, 0666);
138 write_descriptor = open(write_pipe, O_WRONLY);
141 /* Read data from input pipe */
142 count = read(read_descriptor, buffer, BUFFER_STEP);
144 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
147 for (i = 0; i < count; i++, number_of_characters++) {
148 if (buffer[i] == '\n') {
149 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
150 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
151 write(write_descriptor, '\n', 1);
152 number_of_characters = 0;
161 /* Release resources in normal program flow exit. */
162 close(read_descriptor);
163 close(write_descriptor);
164 unlink(write_descriptor);