Added signal handlers for all processes.
[wsti_so.git] / src / process1.c
index d514b03e38daa000c379cc21e9c43212576eb2c6..b5004830f5c9c43e4aebc60df07a0cf1828e83ea 100644 (file)
@@ -5,9 +5,31 @@
 /* open/read/write/close */
 #include <fcntl.h>
 
+/* Signals handling.. */
+#include <signal.h>
+
 /** If buffer is too small to hold entire string, it is incremented by this value */
 #define BUFFER_STEP 16
 
+/**
+ * Handler for signals.
+ */
+void sig_handler(int signo)
+{
+       if (signo == SIGUSR1) {
+               fprintf(stderr, "[%s] SIGUSR1!\n", "process1");
+       }
+       else if (signo == SIGUSR2) {
+               fprintf(stderr, "[%s] SIGUSR2!\n", "process1");
+       }
+       else if (signo == SIGINT) {
+               fprintf(stderr, "[%s] SIGINT!\n", "process1");
+       }
+       else if (signo == SIGCONT) {
+               fprintf(stderr, "[%s] SIGCONT!\n", "process1");
+       }
+}
+
 /**
  * Program reads entire lines of text from the standard input and pass them
  * to the process2 using created pipe.
@@ -37,12 +59,28 @@ int main(void) {
        /** File descriptor of pipe */
        int file_descriptor;
 
+       fprintf(stderr, "[%s] Init!\n", "process1");
+
+       /**
+        * Register signals handled by process
+        */
+       if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGUSR1\n");
+       }
+       if (signal(SIGUSR2, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGUSR2\n");
+       }
+       if (signal(SIGINT, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGINT\n");
+       }
+       if (signal(SIGCONT, sig_handler) == SIG_ERR) {
+               fprintf(stderr, "can't catch SIGCONT\n");
+       }
+
        mkfifo(write_pipe, 0666);
 
        file_descriptor = open(write_pipe, O_WRONLY);
 
-       fprintf(stderr, "[%s] Init!\n", "process1");
-
        do {
                c = fgetc(stdin);