clone.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * clone 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 <sched.h>
  29. #include <sys/wait.h>
  30. #define GOT1 (1 << 1)
  31. #define GOT2 (1 << 2)
  32. #define GOT3 (1 << 3)
  33. #define ALLGOT (GOT1|GOT2|GOT3)
  34. void child_handler(int sig)
  35. {
  36. printf("I got a SIGCHLD\n");
  37. }
  38. int clone_main(void *arg)
  39. {
  40. unsigned long input = (unsigned long)arg;
  41. int secs = (input / 10) * 4;
  42. printf("Clone got %lu, sleeping for %i secs\n", input, secs);
  43. sleep(secs);
  44. return input + 20;
  45. }
  46. int main(void)
  47. {
  48. int clone1, clone2, clone3;
  49. char clone1_stack[8192], clone2_stack[8192], clone3_stack[8192];
  50. int status, nostatus, result, wpid;
  51. signal(SIGCHLD, child_handler);
  52. if ((clone1 = clone(clone_main, clone1_stack, 0, (void*)11)) == -1) {
  53. perror("Clone 1 failed");
  54. exit(-1);
  55. }
  56. if ((clone2 = clone(clone_main, clone2_stack, 0, (void*)22)) == -1) {
  57. perror("Clone 2 failed");
  58. exit(-2);
  59. }
  60. if ((clone3 = clone(clone_main, clone3_stack, 0, (void*)33)) == -1) {
  61. perror("Clone 3 failed");
  62. exit(-3);
  63. }
  64. sleep(1);
  65. printf("Parent: waiting for the clones to die.\n");
  66. nostatus = status = 0;
  67. while (1) {
  68. if ((wpid = waitpid(clone1, &result, WNOHANG|__WCLONE)) == -1)
  69. nostatus |= GOT1;
  70. if (wpid == clone1) {
  71. status |= GOT1;
  72. printf("Clone1 gave back %i\n", WEXITSTATUS(result));
  73. }
  74. if ((wpid = waitpid(clone2, &result, WNOHANG|__WCLONE)) == -1)
  75. nostatus |= GOT2;
  76. if (wpid == clone2) {
  77. status |= GOT2;
  78. printf("Clone2 gave back %i\n", WEXITSTATUS(result));
  79. }
  80. if ((wpid = waitpid(clone3, &result, WNOHANG|__WCLONE)) == -1)
  81. nostatus |= GOT3;
  82. if (wpid == clone3) {
  83. status |= GOT3;
  84. printf("Clone3 gave back %i\n", WEXITSTATUS(result));
  85. }
  86. if (status == ALLGOT || nostatus == ALLGOT)
  87. break;
  88. }
  89. if (status == ALLGOT) {
  90. printf("Clones exited.\nGoodbye.\n");
  91. return EXIT_SUCCESS;
  92. } else {
  93. perror("Waiting for clones failed");
  94. return EXIT_FAILURE;
  95. }
  96. }
  97. /*
  98. Local Variables:
  99. c-file-style: "linux"
  100. c-basic-offset: 4
  101. tab-width: 4
  102. End:
  103. */