fork.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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,2001 by Erik Andersen <andersen@uclibc.org>
  7. * Written by Erik Andersen <andersen@uclibc.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Library General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <signal.h>
  28. #include <sys/wait.h>
  29. #define GOT1 (1 << 1)
  30. #define GOT2 (1 << 2)
  31. #define GOT3 (1 << 3)
  32. #ifdef __ARCH_USE_MMU__
  33. void child_handler(int sig)
  34. {
  35. fprintf(stderr, "I got a SIGCHLD\n");
  36. }
  37. int main(void)
  38. {
  39. pid_t pid1, pid2, pid3;
  40. int status, result, wpid;
  41. signal(SIGCHLD, child_handler);
  42. if ((pid1 = fork()) == 0) {
  43. fprintf(stderr, "The child process sleeps 2 seconds...\n");
  44. sleep(4);
  45. fprintf(stderr, "Child exiting.\n");
  46. exit(-1);
  47. }
  48. if ((pid2 = fork()) == 0) {
  49. fprintf(stderr, "The child process sleeps 3 seconds...\n");
  50. sleep(3);
  51. fprintf(stderr, "Child exiting.\n");
  52. exit(-1);
  53. }
  54. if ((pid3 = fork()) == 0) {
  55. fprintf(stderr, "The child process sleeps 4 seconds...\n");
  56. sleep(2);
  57. fprintf(stderr, "Child exiting.\n");
  58. exit(-1);
  59. }
  60. fprintf(stderr, "Parent: waiting for the child to die.\n");
  61. status = 0;
  62. while (1) {
  63. wpid = waitpid(pid1, &result, WNOHANG);
  64. if (wpid == pid1)
  65. status |= GOT1;
  66. wpid = waitpid(pid2, &result, WNOHANG);
  67. if (wpid == pid2)
  68. status |= GOT2;
  69. wpid = waitpid(pid3, &result, WNOHANG);
  70. if (wpid == pid3)
  71. status |= GOT3;
  72. if (status == (GOT1 | GOT2 | GOT3))
  73. break;
  74. }
  75. fprintf(stderr, "Child process exited.\nGoodbye.\n");
  76. return EXIT_SUCCESS;
  77. }
  78. #else
  79. int main(void)
  80. {
  81. printf("Skipping test on non-mmu host!\n");
  82. return EXIT_SUCCESS;
  83. }
  84. #endif
  85. /*
  86. Local Variables:
  87. c-file-style: "linux"
  88. c-basic-offset: 4
  89. tab-width: 4
  90. End:
  91. */