sigactdemo.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* sigactdemo.c
  2. * purpose: shows use of sigaction()
  3. * feature: blocks ^\ while handling ^C
  4. * does not reset ^C handler, so two kill
  5. */
  6. #include <stdio.h>
  7. #include <signal.h>
  8. #define INPUTLEN 100
  9. main()
  10. {
  11. struct sigaction newhandler; /* new settings */
  12. sigset_t blocked; /* set of blocked sigs */
  13. void inthandler(); /* the handler */
  14. char x[INPUTLEN];
  15. /* load these two members first */
  16. newhandler.sa_handler = inthandler; /* handler function */
  17. newhandler.sa_flags = SA_RESETHAND | SA_RESTART; /* options */
  18. /* then build the list of blocked signals */
  19. sigemptyset(&blocked); /* clear all bits */
  20. sigaddset(&blocked, SIGQUIT); /* add SIGQUIT to list */
  21. newhandler.sa_mask = blocked; /* store blockmask */
  22. if ( sigaction(SIGINT, &newhandler, NULL) == -1 )
  23. perror("sigaction");
  24. else
  25. while( 1 ){
  26. fgets(x, INPUTLEN, stdin);
  27. printf("input: %s", x);
  28. }
  29. }
  30. void inthandler(int s)
  31. {
  32. printf("Called with signal %d\n", s);
  33. sleep(s);
  34. printf("done handling signal %d\n", s);
  35. }