3 /* open/read/write/close */
6 /* Signals handling.. */
9 /** If buffer is too small to hold entire string, it is incremented by this value */
10 #define BUFFER_STEP 16
13 * Handler for signals.
15 void sig_handler(int signo)
17 if (signo == SIGUSR1) {
18 fprintf(stderr, "[%s] SIGUSR1!\n", "process1");
20 else if (signo == SIGUSR2) {
21 fprintf(stderr, "[%s] SIGUSR2!\n", "process1");
23 else if (signo == SIGINT) {
24 fprintf(stderr, "[%s] SIGINT!\n", "process1");
26 else if (signo == SIGCONT) {
27 fprintf(stderr, "[%s] SIGCONT!\n", "process1");
32 * Program grabs data from process1, calculates number of characters in each line
33 * and pass the value to process3.
36 /** Named pipe used to communicate with process1 */
37 char * read_pipe = "/tmp/process1pipe";
39 /** Named pipe used to communicate with process3 */
40 char * write_pipe = "/tmp/process2pipe";
42 /** Descriptor of input pipe */
45 /** Descriptor of output pipe */
49 * Buffer used for storing data from input pipe.
50 * Data is stored in chunks of BUFFER_STEP size.
51 * If data during reading is bigger than this value, then number of
52 * characters is saved, and buffer is cleared for reading another chunk.
54 char buffer[BUFFER_STEP];
56 /** Index used when iterating buffer */
59 /** Stores number of bytes read from input pipe in current iteration */
62 /* Reading from process1 */
63 read_descriptor = open(read_pipe, O_RDONLY);
65 /* Writing to process2 */
66 mkfifo(write_pipe, 0666);
67 write_descriptor = open(write_pipe, O_WRONLY);
69 int number_of_characters = 0;
71 fprintf(stderr, "[%s] Init!\n", "process2");
74 * Register signals handled by process
76 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
77 fprintf(stderr, "can't catch SIGUSR1\n");
79 if (signal(SIGUSR2, sig_handler) == SIG_ERR) {
80 fprintf(stderr, "can't catch SIGUSR2\n");
82 if (signal(SIGINT, sig_handler) == SIG_ERR) {
83 fprintf(stderr, "can't catch SIGINT\n");
85 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
86 fprintf(stderr, "can't catch SIGCONT\n");
90 /* Read data from input pipe */
91 count = read(read_descriptor, buffer, BUFFER_STEP);
93 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
96 for (i = 0; i < count; i++, number_of_characters++) {
97 if (buffer[i] == '\n') {
98 fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
99 write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
100 write(write_descriptor, '\n', 1);
101 number_of_characters = 0;
110 close(read_descriptor);
111 close(write_descriptor);
112 unlink(write_descriptor);