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
14 /** Buffer used to store line of characters */
17 /** Named pipe used to communnicate with process2 */
18 char * write_pipe = "/tmp/process1pipe";
20 /** File descriptor of pipe */
24 * Handler for signals.
26 void sig_handler(int signo)
28 fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
29 if (signo == SIGUSR1) {
31 else if (signo == SIGTERM) {
32 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
42 else if (signo == SIGTSTP) {
43 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
46 else if (signo == SIGCONT) {
47 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
48 file_descriptor = open(write_pipe, O_WRONLY);
53 * Program reads entire lines of text from the standard input and pass them
54 * to the process2 using created pipe.
57 /** Currently fetched from stdin character */
60 /** Current buffer length*/
61 int buffer_length = 0;
63 /** Index of the current character */
67 * Temporary buffer used as a proxy between
68 * checking memory allocation and copying data to real buffer
72 fprintf(stderr, "[%s] Init!\n", "process1");
75 * Register signals handled by process
77 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
78 fprintf(stderr, "can't catch SIGUSR1\n");
80 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
81 fprintf(stderr, "can't catch SIGTERM\n");
83 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
84 fprintf(stderr, "can't catch SIGTSTP\n");
86 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
87 fprintf(stderr, "can't catch SIGCONT\n");
90 mkfifo(write_pipe, 0666);
92 file_descriptor = open(write_pipe, O_WRONLY);
98 * Check if current index is bigger than current buffer size.
99 * If so increment buffer size. On error release memory, and set
102 if (i >= buffer_length) {
104 buffer_length += BUFFER_STEP;
105 tmp = (char*) realloc(buffer, buffer_length);
107 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
115 * If there were no errors or it was not just an empty newline:
118 if (c != EOF || ((i == 0) && (c == 10))) {
119 /* If newline has been found
120 * return entire string and release the memory
122 if (c == 10 && (i != 0)) {
124 write(file_descriptor, buffer, strlen(buffer));
125 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
135 * Normal character, add it to the buffer
141 /* Used only for debugging..*/
143 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
150 /* Release resources in normal program flow exit. */