fork.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fork test for uClibc
  4. * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <sys/wait.h>
  13. #define GOT1 (1 << 1)
  14. #define GOT2 (1 << 2)
  15. #define GOT3 (1 << 3)
  16. #ifdef __ARCH_USE_MMU__
  17. static void child_handler(int sig)
  18. {
  19. fprintf(stderr, "I got a SIGCHLD\n");
  20. }
  21. int main(void)
  22. {
  23. pid_t pid1, pid2, pid3;
  24. int status, result, wpid;
  25. signal(SIGCHLD, child_handler);
  26. if ((pid1 = fork()) == 0) {
  27. fprintf(stderr, "The child process sleeps 2 seconds...\n");
  28. sleep(4);
  29. fprintf(stderr, "Child exiting.\n");
  30. exit(-1);
  31. }
  32. if ((pid2 = fork()) == 0) {
  33. fprintf(stderr, "The child process sleeps 3 seconds...\n");
  34. sleep(3);
  35. fprintf(stderr, "Child exiting.\n");
  36. exit(-1);
  37. }
  38. if ((pid3 = fork()) == 0) {
  39. fprintf(stderr, "The child process sleeps 4 seconds...\n");
  40. sleep(2);
  41. fprintf(stderr, "Child exiting.\n");
  42. exit(-1);
  43. }
  44. fprintf(stderr, "Parent: waiting for the child to die.\n");
  45. status = 0;
  46. while (1) {
  47. wpid = waitpid(pid1, &result, WNOHANG);
  48. if (wpid == pid1)
  49. status |= GOT1;
  50. wpid = waitpid(pid2, &result, WNOHANG);
  51. if (wpid == pid2)
  52. status |= GOT2;
  53. wpid = waitpid(pid3, &result, WNOHANG);
  54. if (wpid == pid3)
  55. status |= GOT3;
  56. if (status == (GOT1 | GOT2 | GOT3))
  57. break;
  58. }
  59. fprintf(stderr, "Child process exited.\nGoodbye.\n");
  60. return EXIT_SUCCESS;
  61. }
  62. #else
  63. int main(void)
  64. {
  65. printf("Skipping test on non-mmu host!\n");
  66. return EXIT_SUCCESS;
  67. }
  68. #endif
  69. /*
  70. Local Variables:
  71. c-file-style: "linux"
  72. c-basic-offset: 4
  73. tab-width: 4
  74. End:
  75. */