fork.c 1.8 KB

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