5 /* open/read/write/close */
8 /* Signals handling.. */
11 /** If buffer is too small to hold entire string, it is incremented by this value */
12 #define BUFFER_STEP 16
15 * Handler for signals.
17 void sig_handler(int signo)
19 if (signo == SIGUSR1) {
20 fprintf(stderr, "[%s] SIGUSR1!\n", "process1");
22 else if (signo == SIGUSR2) {
23 fprintf(stderr, "[%s] SIGUSR2!\n", "process1");
25 else if (signo == SIGINT) {
26 fprintf(stderr, "[%s] SIGINT!\n", "process1");
28 else if (signo == SIGCONT) {
29 fprintf(stderr, "[%s] SIGCONT!\n", "process1");
34 * Program reads entire lines of text from the standard input and pass them
35 * to the process2 using created pipe.
38 /** Currently fetched from stdin character */
41 /** Buffer used to store line of characters */
44 /** Current buffer length*/
45 int buffer_length = 0;
47 /** Index of the current character */
51 * Temporary buffer used as a proxy between
52 * checking memory allocation and copying data to real buffer
56 /** Named pipe used to communnicate with process2 */
57 char * write_pipe = "/tmp/process1pipe";
59 /** File descriptor of pipe */
62 fprintf(stderr, "[%s] Init!\n", "process1");
65 * Register signals handled by process
67 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
68 fprintf(stderr, "can't catch SIGUSR1\n");
70 if (signal(SIGUSR2, sig_handler) == SIG_ERR) {
71 fprintf(stderr, "can't catch SIGUSR2\n");
73 if (signal(SIGINT, sig_handler) == SIG_ERR) {
74 fprintf(stderr, "can't catch SIGINT\n");
76 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
77 fprintf(stderr, "can't catch SIGCONT\n");
80 mkfifo(write_pipe, 0666);
82 file_descriptor = open(write_pipe, O_WRONLY);
88 * Check if current index is bigger than current buffer size.
89 * If so increment buffer size. On error release memory, and set
92 if (i >= buffer_length) {
94 buffer_length += BUFFER_STEP;
95 tmp = (char*) realloc(buffer, buffer_length);
97 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
105 * If there were no errors or it was not just an empty newline:
108 if (c != EOF || ((i == 0) && (c == 10))) {
109 /* If newline has been found
110 * return entire string and release the memory
112 if (c == 10 && (i != 0)) {
114 write(file_descriptor, buffer, strlen(buffer));
115 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
125 * Normal character, add it to the buffer
131 /* Used only for debugging..*/
133 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);