tst-fork1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2002, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Roland McGrath <roland@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/wait.h>
  22. static void *
  23. thread_function (void * arg)
  24. {
  25. int i = (intptr_t) arg;
  26. int status;
  27. pid_t pid;
  28. pid_t pid2;
  29. pid = fork ();
  30. switch (pid)
  31. {
  32. case 0:
  33. printf ("%ld for %d\n", (long int) getpid (), i);
  34. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 * i };
  35. nanosleep (&ts, NULL);
  36. _exit (i);
  37. break;
  38. case -1:
  39. printf ("fork: %m\n");
  40. return (void *) 1l;
  41. break;
  42. }
  43. pid2 = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  44. if (pid2 != pid)
  45. {
  46. printf ("waitpid returned %ld, expected %ld\n",
  47. (long int) pid2, (long int) pid);
  48. return (void *) 1l;
  49. }
  50. printf ("%ld with %d, expected %d\n",
  51. (long int) pid, WEXITSTATUS (status), i);
  52. return WEXITSTATUS (status) == i ? NULL : (void *) 1l;
  53. }
  54. #define N 5
  55. static const int t[N] = { 7, 6, 5, 4, 3 };
  56. int
  57. main (void)
  58. {
  59. pthread_t th[N];
  60. int i;
  61. int result = 0;
  62. pthread_attr_t at;
  63. if (pthread_attr_init (&at) != 0)
  64. {
  65. puts ("attr_init failed");
  66. return 1;
  67. }
  68. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  69. {
  70. puts ("attr_setstacksize failed");
  71. return 1;
  72. }
  73. for (i = 0; i < N; ++i)
  74. if (pthread_create (&th[i], NULL, thread_function,
  75. (void *) (intptr_t) t[i]) != 0)
  76. {
  77. printf ("creation of thread %d failed\n", i);
  78. exit (1);
  79. }
  80. if (pthread_attr_destroy (&at) != 0)
  81. {
  82. puts ("attr_destroy failed");
  83. return 1;
  84. }
  85. for (i = 0; i < N; ++i)
  86. {
  87. void *v;
  88. if (pthread_join (th[i], &v) != 0)
  89. {
  90. printf ("join of thread %d failed\n", i);
  91. result = 1;
  92. }
  93. else if (v != NULL)
  94. {
  95. printf ("join %d successful, but child failed\n", i);
  96. result = 1;
  97. }
  98. else
  99. printf ("join %d successful\n", i);
  100. }
  101. return result;
  102. }