clone.c 2.9 KB

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