clone.c 2.2 KB

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