5 /* open/read/write/close */
8 /* Signals handling.. */
14 /** If buffer is too small to hold entire string, it is incremented by this value */
15 #define BUFFER_STEP 16
17 /** Buffer used to store line of characters */
20 /** Named pipe used to communnicate with process2 */
21 char * write_pipe = "/tmp/process1pipe";
23 /** File descriptor of pipe */
28 * Shared memory variables
31 * Memory key for processes. Must be same between all processes to properly
36 * Id of the shared memory
41 * Message shared by processes. Contains array of process IDs
47 struct message * processes = NULL;
50 * Handler for signals.
52 void sig_handler(int signo)
54 fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
55 if (signo == SIGUSR1) {
57 else if (signo == SIGTERM) {
59 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
68 fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, processes->pids[i]);
72 else if (signo == SIGTSTP) {
73 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
77 else if (signo == SIGCONT) {
78 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
79 file_descriptor = open(write_pipe, O_WRONLY);
84 * Program reads entire lines of text from the standard input and pass them
85 * to the process2 using created pipe.
88 /** Currently fetched from stdin character */
91 /** Current buffer length*/
92 int buffer_length = 0;
94 /** Index of the current character */
98 * Temporary buffer used as a proxy between
99 * checking memory allocation and copying data to real buffer
103 fprintf(stderr, "[%s] Init!\n", "process1");
106 * Register signals handled by process
108 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
109 fprintf(stderr, "can't catch SIGUSR1\n");
111 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
112 fprintf(stderr, "can't catch SIGTERM\n");
114 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
115 fprintf(stderr, "can't catch SIGTSTP\n");
117 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
118 fprintf(stderr, "can't catch SIGCONT\n");
122 * Register memory to share with other processes, and pass current
123 * process id to the array.
125 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
127 processes = (struct message *)shmat(shmid, NULL, 0);
128 processes->pids[0] = getpid();
130 mkfifo(write_pipe, 0666);
132 file_descriptor = open(write_pipe, O_WRONLY);
138 * Check if current index is bigger than current buffer size.
139 * If so increment buffer size. On error release memory, and set
142 if (i >= buffer_length) {
144 buffer_length += BUFFER_STEP;
145 tmp = (char*) realloc(buffer, buffer_length);
147 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
155 * If there were no errors or it was not just an empty newline:
158 if (c != EOF || ((i == 0) && (c == 10))) {
159 /* If newline has been found
160 * return entire string and release the memory
162 if (c == 10 && (i != 0)) {
164 write(file_descriptor, buffer, strlen(buffer));
165 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
175 * Normal character, add it to the buffer
181 /* Used only for debugging..*/
183 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
190 /* Release resources in normal program flow exit. */