5 /* open/read/write/close */
8 /* Signals handling.. */
15 /** If buffer is too small to hold entire string, it is incremented by this value */
16 #define BUFFER_STEP 16
18 /** Buffer used to store line of characters */
21 /** Named pipe used to communnicate with process2 */
22 char * write_pipe = "/tmp/process1pipe";
24 /** File descriptor of pipe */
29 * Shared memory variables
32 * Memory key for processes. Must be same between all processes to properly
37 * Id of the shared memory
42 * Message shared by processes. Contains array of process IDs
48 struct message * processes = NULL;
51 * Message Queue variables
55 * Unique key of message queue.
65 * Structure holding queue message data.
66 * Parameter mtype describes process to whom message is sent.
67 * Parameter signo is a signal to raise after getting message.
69 struct queue_message {
74 void notify_other_processes(int signo) {
76 struct queue_message msg;
80 pid_t pid = processes->pids[i];
82 if (i != 0 && pid != 0) {
84 fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
85 msgsnd(qid, &msg, sizeof(int), 0);
86 fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process1", strsignal(SIGUSR1), SIGUSR1, pid);
93 * Handler for signals.
95 void sig_handler(int signo)
97 fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
98 if (signo == SIGUSR1) {
99 fprintf(stderr, "[%s] > Notified!\n", "process1");
100 struct queue_message msg;
101 /* Check queues from both other processes */
102 if (msgrcv(qid, &msg, sizeof(int), 1, 0) > 0) {
103 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process1", strsignal(msg.signo[0]));
107 else if (signo == SIGTERM) {
108 fprintf(stderr, "[%s] > Signalling other processes..\n", "process1");
109 processes->pids[1] = 0;
110 notify_other_processes(signo);
112 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
122 else if (signo == SIGTSTP) {
123 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
127 else if (signo == SIGCONT) {
128 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
129 file_descriptor = open(write_pipe, O_WRONLY);
134 * Program reads entire lines of text from the standard input and pass them
135 * to the process2 using created pipe.
138 /** Currently fetched from stdin character */
141 /** Current buffer length*/
142 int buffer_length = 0;
144 /** Index of the current character */
148 * Temporary buffer used as a proxy between
149 * checking memory allocation and copying data to real buffer
153 fprintf(stderr, "[%s] Init!\n", "process1");
156 * Register signals handled by process
158 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
159 fprintf(stderr, "can't catch SIGUSR1\n");
161 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
162 fprintf(stderr, "can't catch SIGTERM\n");
164 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
165 fprintf(stderr, "can't catch SIGTSTP\n");
167 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
168 fprintf(stderr, "can't catch SIGCONT\n");
172 * Register memory to share with other processes, and pass current
173 * process id to the array.
175 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
177 processes = (struct message *)shmat(shmid, NULL, 0);
178 processes->pids[0] = getpid();
180 fprintf(stderr, "[%s] Shared pid: %d\n", "process1", getpid());
183 * Register message queue to communicate with other processes
185 qid = msgget(qkey, IPC_CREAT | 0666);
187 mkfifo(write_pipe, 0666);
189 file_descriptor = open(write_pipe, O_WRONLY);
195 * Check if current index is bigger than current buffer size.
196 * If so increment buffer size. On error release memory, and set
199 if (i >= buffer_length) {
201 buffer_length += BUFFER_STEP;
202 tmp = (char*) realloc(buffer, buffer_length);
204 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
212 * If there were no errors or it was not just an empty newline:
215 if (c != EOF || ((i == 0) && (c == 10))) {
216 /* If newline has been found
217 * return entire string and release the memory
219 if (c == 10 && (i != 0)) {
221 write(file_descriptor, buffer, strlen(buffer));
222 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
232 * Normal character, add it to the buffer
238 /* Used only for debugging..*/
240 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
247 /* Release resources in normal program flow exit. */