Added initial inter-process communication: Sharing pids between processes.
[wsti_so.git] / src / process2.c
index 07a24222f6fac9fe9255c5ceed9f9c218892e76a..2417fe88638fd27bdc543a5d764bca6847540b87 100644 (file)
 #include <stdio.h>
+/* exit.. */
 #include <stdlib.h>
-#include <string.h>
 
 /* open/read/write/close */
 #include <fcntl.h>
 
+/* Signals handling.. */
+#include <signal.h>
+
+#include <sys/shm.h>
+
 /** If buffer is too small to hold entire string, it is incremented by this value */
 #define BUFFER_STEP 16
 
+/** Named pipe used to communicate with process1 */
+char * read_pipe = "/tmp/process1pipe";
+
+/** Named pipe used to communicate with process3 */
+char * write_pipe = "/tmp/process2pipe";
+
+/** Descriptor of input pipe */
+int read_descriptor;
+
+/** Descriptor of output pipe */
+int write_descriptor;
+
+
+/**
+ * Shared memory variables
+ */
+
+/**
+ * Memory key for processes. Must be same between all processes to properly
+ * communicate.
+ */
+key_t shmkey = 18912;
+/**
+ * Id of the shared memory
+ */
+int shmid;
+
+/**
+ * Message shared by processes. Contains array of process IDs
+ */
+struct message {
+       pid_t pids[3];
+};
+
+struct message * processes = NULL;
+
+
+/**
+ * Handler for signals.
+ */
+void sig_handler(int signo)
+{
+       fprintf(stderr, "[%s] Received %s!\n", "process2", strsignal(signo));
+       if (signo == SIGUSR1) {
+       }
+       else if (signo == SIGTERM) {
+               int i = 0;
+               fprintf(stderr, "[%s] > Releasing resources\n", "process2");
+               close(read_descriptor);
+               close(write_descriptor);
+               unlink(write_descriptor);
+               for (; i < 3; i++) {
+                       fprintf(stderr, "[%s] Process %d, PID: %d\n", "process3", i, processes->pids[i]);
+               }
+               exit(0);
+       }
+       else if (signo == SIGTSTP) {
+               fprintf(stderr, "[%s] > Closing pipes\n", "process2");
+               close(read_descriptor);
+               close(write_descriptor);
+               raise (SIGSTOP);
+       }
+       else if (signo == SIGCONT) {
+               fprintf(stderr, "[%s] > Opening pipes\n", "process2");
+               read_descriptor = open(read_pipe, O_RDONLY);
+               write_descriptor = open(write_pipe, O_WRONLY);
+       }
+}
+
 /**
  * Program grabs data from process1, calculates number of characters in each line
  * and pass the value to process3.
  */
 int main(void) {
-       /** Named pipe used to communicate with process1 */
-       char * read_pipe = "/tmp/process1pipe";
-
-       /** Named pipe used to communicate with process3 */
-       char * write_pipe = "/tmp/process2pipe";
-
-       int read_descriptor;
-       int write_descriptor;
-       
+       /**
+        * Buffer used for storing data from input pipe.
+        * Data is stored in chunks of BUFFER_STEP size.
+        * If data during reading is bigger than this value, then number of
+        * characters is saved, and buffer is cleared for reading another chunk.
+        */
        char buffer[BUFFER_STEP];
-       
+
+       /** Index used when iterating buffer */
        int i = 0;
+
+       /** Stores number of bytes read from input pipe in current iteration */
        ssize_t count = 0;
 
+       int number_of_characters = 0;
+
+       fprintf(stderr, "[%s] Init!\n", "process2");
+
+       /**
+        * Register signals handled by process
+        */
+       if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGUSR1\n");
+       }
+       if (signal(SIGTERM, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGTERM\n");
+       }
+       if (signal(SIGTSTP, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGTSTP\n");
+       }
+       if (signal(SIGCONT, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGCONT\n");
+       }
+
+       /*
+        * Register memory to share with other processes, and pass current
+        * process id to the array.
+        */
+       shmid = shmget(shmkey, sizeof(struct message), IPC_CREAT | 0666);
+
+       processes = (struct message *)shmat(shmid, NULL, 0);
+       processes->pids[1] = getpid();
+
        /* Reading from process1 */
        read_descriptor = open(read_pipe, O_RDONLY);
 
@@ -34,8 +137,6 @@ int main(void) {
        mkfifo(write_pipe, 0666);
        write_descriptor = open(write_pipe, O_WRONLY);
 
-       int number_of_characters = 0;
-       
        while(1) {
                /* Read data from input pipe */
                count = read(read_descriptor, buffer, BUFFER_STEP);
@@ -57,6 +158,7 @@ int main(void) {
                }
        }
 
+       /* Release resources in normal program flow exit. */
        close(read_descriptor);
        close(write_descriptor);
        unlink(write_descriptor);