tst-fork1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/wait.h>
  23. static void *
  24. thread_function (void * arg)
  25. {
  26. int i = (intptr_t) arg;
  27. int status;
  28. pid_t pid;
  29. pid_t pid2;
  30. pid = fork ();
  31. switch (pid)
  32. {
  33. case 0:
  34. printf ("%ld for %d\n", (long int) getpid (), i);
  35. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 * i };
  36. nanosleep (&ts, NULL);
  37. _exit (i);
  38. break;
  39. case -1:
  40. printf ("fork: %m\n");
  41. return (void *) 1l;
  42. break;
  43. }
  44. pid2 = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  45. if (pid2 != pid)
  46. {
  47. printf ("waitpid returned %ld, expected %ld\n",
  48. (long int) pid2, (long int) pid);
  49. return (void *) 1l;
  50. }
  51. printf ("%ld with %d, expected %d\n",
  52. (long int) pid, WEXITSTATUS (status), i);
  53. return WEXITSTATUS (status) == i ? NULL : (void *) 1l;
  54. }
  55. #define N 5
  56. static const int t[N] = { 7, 6, 5, 4, 3 };
  57. int
  58. main (void)
  59. {
  60. pthread_t th[N];
  61. int i;
  62. int result = 0;
  63. pthread_attr_t at;
  64. if (pthread_attr_init (&at) != 0)
  65. {
  66. puts ("attr_init failed");
  67. return 1;
  68. }
  69. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  70. {
  71. puts ("attr_setstacksize failed");
  72. return 1;
  73. }
  74. for (i = 0; i < N; ++i)
  75. if (pthread_create (&th[i], NULL, thread_function,
  76. (void *) (intptr_t) t[i]) != 0)
  77. {
  78. printf ("creation of thread %d failed\n", i);
  79. exit (1);
  80. }
  81. if (pthread_attr_destroy (&at) != 0)
  82. {
  83. puts ("attr_destroy failed");
  84. return 1;
  85. }
  86. for (i = 0; i < N; ++i)
  87. {
  88. void *v;
  89. if (pthread_join (th[i], &v) != 0)
  90. {
  91. printf ("join of thread %d failed\n", i);
  92. result = 1;
  93. }
  94. else if (v != NULL)
  95. {
  96. printf ("join %d successful, but child failed\n", i);
  97. result = 1;
  98. }
  99. else
  100. printf ("join %d successful\n", i);
  101. }
  102. return result;
  103. }