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 * Message queue variables
58 struct queue_message {
63 * Handler for signals.
65 void sig_handler(int signo)
67 fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
68 if (signo == SIGUSR1) {
69 fprintf(stderr, "[%s] > Notified!\n", "process3");
71 else if (signo == SIGTERM) {
73 struct queue_message msg;
76 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
85 pid_t pid = processes->pids[i];
86 fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, pid);
89 msgsnd(pid, &msg, sizeof(msg));
95 else if (signo == SIGTSTP) {
96 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
100 else if (signo == SIGCONT) {
101 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
102 file_descriptor = open(write_pipe, O_WRONLY);
107 * Program reads entire lines of text from the standard input and pass them
108 * to the process2 using created pipe.
111 /** Currently fetched from stdin character */
114 /** Current buffer length*/
115 int buffer_length = 0;
117 /** Index of the current character */
121 * Temporary buffer used as a proxy between
122 * checking memory allocation and copying data to real buffer
126 fprintf(stderr, "[%s] Init!\n", "process1");
129 * Register signals handled by process
131 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
132 fprintf(stderr, "can't catch SIGUSR1\n");
134 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
135 fprintf(stderr, "can't catch SIGTERM\n");
137 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
138 fprintf(stderr, "can't catch SIGTSTP\n");
140 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
141 fprintf(stderr, "can't catch SIGCONT\n");
145 * Register memory to share with other processes, and pass current
146 * process id to the array.
148 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
150 processes = (struct message *)shmat(shmid, NULL, 0);
151 processes->pids[0] = getpid();
153 fprintf(stderr, "[%s] Shared pid: %d\n", "process1", getpid());
157 * Register message queue to communicate with other processes
160 qid_input = msgget(qkey, IPC_CREAT | 0666);
162 qid_output1 = msgget(processes->pids[1], IPC_CREAT | 0666);
163 qid_output2 = msgget(processes->pids[2], IPC_CREAT | 0666);
165 mkfifo(write_pipe, 0666);
167 file_descriptor = open(write_pipe, O_WRONLY);
173 * Check if current index is bigger than current buffer size.
174 * If so increment buffer size. On error release memory, and set
177 if (i >= buffer_length) {
179 buffer_length += BUFFER_STEP;
180 tmp = (char*) realloc(buffer, buffer_length);
182 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
190 * If there were no errors or it was not just an empty newline:
193 if (c != EOF || ((i == 0) && (c == 10))) {
194 /* If newline has been found
195 * return entire string and release the memory
197 if (c == 10 && (i != 0)) {
199 write(file_descriptor, buffer, strlen(buffer));
200 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
210 * Normal character, add it to the buffer
216 /* Used only for debugging..*/
218 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
225 /* Release resources in normal program flow exit. */