sigchld.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <sys/signal.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #ifdef __ARCH_USE_MMU__
  9. static void test_handler(int signo)
  10. {
  11. write(1, "caught SIGCHLD\n", 15);
  12. return;
  13. }
  14. int main(void)
  15. {
  16. pid_t mypid;
  17. struct sigaction siga;
  18. static sigset_t set;
  19. /* Set up sighandling */
  20. sigfillset(&set);
  21. siga.sa_handler = test_handler;
  22. siga.sa_mask = set;
  23. siga.sa_flags = 0;
  24. if (sigaction(SIGCHLD, &siga, (struct sigaction *)NULL) != 0) {
  25. fprintf(stderr, "sigaction choked: %s!", strerror(errno));
  26. exit(EXIT_FAILURE);
  27. }
  28. /* Setup a child process to exercise the sig handling for us */
  29. mypid = getpid();
  30. if (fork() == 0) {
  31. int i;
  32. for (i=0; i < 3; i++) {
  33. sleep(2);
  34. kill(mypid, SIGCHLD);
  35. }
  36. _exit(EXIT_SUCCESS);
  37. }
  38. /* Wait for signals */
  39. write(1, "waiting for a SIGCHLD\n",22);
  40. for(;;) {
  41. sleep(10);
  42. if (waitpid(-1, NULL, WNOHANG | WUNTRACED) > 0)
  43. break;
  44. write(1, "after sleep\n", 12);
  45. }
  46. printf("Bye-bye! All done!\n");
  47. return 0;
  48. }
  49. #else
  50. int main(void)
  51. {
  52. printf("Skipping test on non-mmu host!\n");
  53. return 0;
  54. }
  55. #endif