sigchld.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. void test_handler(int signo)
  9. {
  10. write(1, "caught SIGCHLD\n", 15);
  11. return;
  12. }
  13. #ifdef __ARCH_USE_MMU__
  14. int main(void)
  15. {
  16. pid_t mypid;
  17. struct sigaction siga;
  18. static sigset_t sigset;
  19. /* Set up sighandling */
  20. sigfillset(&sigset);
  21. siga.sa_handler = test_handler;
  22. siga.sa_mask = sigset;
  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