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
56 struct queue_message {
61 void notify_other_processes(int signo) {
63 struct queue_message msg;
67 pid_t pid = processes->pids[i];
69 if (i != 0 && pid != 0) {
71 fprintf(stderr, "[%s] Sending message of type (%d) with value %d\n", "process1", msg.mtype, msg.signo[0]);
72 msgsnd(qid, &msg, sizeof(int), 0);
73 fprintf(stderr, "[%s] Sending signal %s (%d) to PID: %d\n", "process1", strsignal(SIGUSR1), SIGUSR1, pid);
80 * Handler for signals.
82 void sig_handler(int signo)
84 fprintf(stderr, "[%s] Received %s!\n", "process1", strsignal(signo));
86 if (signo == SIGUSR1) {
87 fprintf(stderr, "[%s] > Notified!\n", "process1");
88 struct queue_message msg;
89 /* Check queues from both other processes */
90 if (msgrcv(qid, &msg, sizeof(int), 1, 0) > 0) {
91 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process1", strsignal(msg.signo[0]));
94 else if (msgrcv(qid, &msg, sizeof(int), 1, 0) > 0) {
95 fprintf(stderr, "[%s] > Notified with value: %s!\n", "process1", strsignal(msg.signo[0]));
99 else if (signo == SIGTERM) {
100 fprintf(stderr, "[%s] > Signalling other processes..\n", "process1");
101 processes->pids[1] = 0;
102 notify_other_processes(signo);
104 fprintf(stderr, "[%s] > Releasing resources\n", "process1");
114 else if (signo == SIGTSTP) {
115 fprintf(stderr, "[%s] > Closing pipe\n", "process1");
119 else if (signo == SIGCONT) {
120 fprintf(stderr, "[%s] > Opening pipe\n", "process1");
121 file_descriptor = open(write_pipe, O_WRONLY);
126 * Program reads entire lines of text from the standard input and pass them
127 * to the process2 using created pipe.
130 /** Currently fetched from stdin character */
133 /** Current buffer length*/
134 int buffer_length = 0;
136 /** Index of the current character */
140 * Temporary buffer used as a proxy between
141 * checking memory allocation and copying data to real buffer
145 fprintf(stderr, "[%s] Init!\n", "process1");
148 * Register signals handled by process
150 if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
151 fprintf(stderr, "can't catch SIGUSR1\n");
153 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
154 fprintf(stderr, "can't catch SIGTERM\n");
156 if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
157 fprintf(stderr, "can't catch SIGTSTP\n");
159 if (signal(SIGCONT, sig_handler) == SIG_ERR) {
160 fprintf(stderr, "can't catch SIGCONT\n");
164 * Register memory to share with other processes, and pass current
165 * process id to the array.
167 shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
169 processes = (struct message *)shmat(shmid, NULL, 0);
170 processes->pids[0] = getpid();
172 fprintf(stderr, "[%s] Shared pid: %d\n", "process1", getpid());
175 * Register message queue to communicate with other processes
177 qid = msgget(qkey, IPC_CREAT | 0666);
179 mkfifo(write_pipe, 0666);
181 file_descriptor = open(write_pipe, O_WRONLY);
187 * Check if current index is bigger than current buffer size.
188 * If so increment buffer size. On error release memory, and set
191 if (i >= buffer_length) {
193 buffer_length += BUFFER_STEP;
194 tmp = (char*) realloc(buffer, buffer_length);
196 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
204 * If there were no errors or it was not just an empty newline:
207 if (c != EOF || ((i == 0) && (c == 10))) {
208 /* If newline has been found
209 * return entire string and release the memory
211 if (c == 10 && (i != 0)) {
213 write(file_descriptor, buffer, strlen(buffer));
214 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
224 * Normal character, add it to the buffer
230 /* Used only for debugging..*/
232 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
239 /* Release resources in normal program flow exit. */