From c0975e5a6af7b478563193dac7c3484b4430d3a3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Sat, 21 Jun 2014 01:33:38 +0200 Subject: [PATCH] Added signal handlers for all processes. --- src/process1.c | 42 ++++++++++++++++++++++++++++++++++++++++-- src/process2.c | 38 ++++++++++++++++++++++++++++++++++++++ src/process3.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/src/process1.c b/src/process1.c index d514b03..b500483 100644 --- a/src/process1.c +++ b/src/process1.c @@ -5,9 +5,31 @@ /* open/read/write/close */ #include +/* Signals handling.. */ +#include + /** 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); diff --git a/src/process2.c b/src/process2.c index b6dff4b..7ae70ff 100644 --- a/src/process2.c +++ b/src/process2.c @@ -3,9 +3,31 @@ /* open/read/write/close */ #include +/* Signals handling.. */ +#include + /** 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 grabs data from process1, calculates number of characters in each line * and pass the value to process3. @@ -48,6 +70,22 @@ int main(void) { 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(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"); + } + while(1) { /* Read data from input pipe */ count = read(read_descriptor, buffer, BUFFER_STEP); diff --git a/src/process3.c b/src/process3.c index 4568d97..8b4f826 100644 --- a/src/process3.c +++ b/src/process3.c @@ -3,6 +3,28 @@ /* open/read/write/close */ #include +/* Signals handling.. */ +#include + +/** + * 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 grabs data (calculated number of characters) from process2 and prints * grabbed data to the standard output. @@ -20,11 +42,27 @@ int main(void) { /** Stores number of bytes read from input pipe in current iteration */ ssize_t count = 0; + fprintf(stderr, "[%s] Init!\n", "process3"); + + /** + * 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"); + } + /* Reading from process2 */ read_descriptor = open(read_pipe, O_RDONLY); - fprintf(stderr, "[%s] Init!\n", "process3"); - while(1) { /* Read data from input pipe */ count = read(read_descriptor, &buffer, sizeof(int)); -- 2.30.2