c - Get if a signal is received -


how can if process receives signal? purpose of following example fork process, read character child process , send sigusr1 parent, if after 10 seconds user still have insert character, child process terminated. question how know if sigusr1 received:

#define _posix_source  #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h>  void handle_usr1() {     return; }  int main(void) {     int p[2];     pid_t pid;      if(pipe(p) == -1)     {         perror("pipe");         return -1;     }      if((pid = fork()) == -1)     {         perror("fork");         return -1;     }      if(pid == 0)     {         close(p[0]);         char ch, b;          ch = getchar();          if(ch != '\n')             while((b = getchar()) != '\n' && b != eof);          write(p[1], &ch, 1);         kill(getppid(),sigusr1);          exit(-1);     }     else     {         signal(sigusr1,handle_usr1);         close(p[1]);         char ch;          sleep(10);         kill(pid, sigterm);          read(p[0],&ch,1);          //if(/*sigusr1 not recived*/)         //{         //    exit(-1);         //}          printf("\n\n%c",ch);     }      return 0; } 

what can replace /*sigusr1 not recived*/ with?

in comment, suggested:

your handle_usr1() function should set static volatile sig_atomic_t variable known value (e.g. 1). can check variable know whether signal has been received. in standard c, there's little else can in signal handler in strictly conforming way; in posix, may considerably more (but careful: avoid using printf() in signal handler may you).

here's meant translated code, closely based on code:

sigusr1.c

#define _posix_source  #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <unistd.h>  static volatile sig_atomic_t sig = 0;  static void handle_usr1(int signum) {     sig = signum; }  int main(void) {     int p[2];     pid_t pid;      signal(sigusr1, handle_usr1);      if (pipe(p) == -1)     {         perror("pipe");         return -1;     }      if ((pid = fork()) == -1)     {         perror("fork");         return -1;     }      if (pid == 0)     {         close(p[0]);         char ch, b;          ch = getchar();          if (ch != '\n')             while ((b = getchar()) != '\n' && b != eof)                 ;          write(p[1], &ch, 1);         kill(getppid(), sigusr1);          exit(-1);     }     else     {         close(p[1]);         char ch;          sleep(10);         if (sig != 0)             printf("signal number %d received while sleeping\n", sig);         else         {             printf("sending sigterm child %d\n", (int)pid);             kill(pid, sigterm);         }          if (read(p[0], &ch, 1) == 1)             printf("character sent child: [%c]\n", ch);         else             printf("no data sent child before died\n");     }      return 0; } 

compilation

gcc 5.3.0 on mac os x 10.11.3 el capitan:

$ gcc -o3 -g -std=c11 -wall -wextra -wmissing-prototypes -wstrict-prototypes \ >     -wold-style-definition -werror sigusr1.c -o sigusr1 $ 

sample outputs

$ ./sigusr1 sending sigterm child 70657 no data sent child before died $ ./sigusr1 kaput signal number 30 received while sleeping character sent child: [k] $ 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -