sigdemo3.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* sigdemo3.c
  2. * purpose: show answers to signal questions
  3. * question1: does the handler stay in effect after a signal arrives?
  4. * question2: what if a signalX arrives while handling signalX?
  5. * question3: what if a signalX arrives while handling signalY?
  6. * question4: what happens to read() when a signal arrives?
  7. */
  8. #include <stdio.h>
  9. #include <signal.h>
  10. #define INPUTLEN 100
  11. main(int ac, char *av[])
  12. {
  13. void inthandler(int);
  14. void quithandler(int);
  15. char input[INPUTLEN];
  16. int nchars;
  17. signal( SIGINT, inthandler ); /* set handler */
  18. signal( SIGQUIT, quithandler ); /* set handler */
  19. do {
  20. printf("\nType a message\n");
  21. nchars = read(0, input, (INPUTLEN-1));
  22. if ( nchars == -1 )
  23. perror("read returned an error");
  24. else {
  25. input[nchars] = '\0';
  26. printf("You typed: %s", input);
  27. }
  28. }
  29. while( strncmp( input , "quit" , 4 ) != 0 );
  30. }
  31. void inthandler(int s)
  32. {
  33. printf(" Received signal %d .. waiting\n", s );
  34. sleep(2);
  35. printf(" Leaving inthandler \n");
  36. }
  37. void quithandler(int s)
  38. {
  39. printf(" Received signal %d .. waiting\n", s );
  40. sleep(3);
  41. printf(" Leaving quithandler \n");
  42. }