tst-fork1.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "../test-skeleton.h"
  23. #ifdef __ARCH_USE_MMU__
  24. static void *
  25. thread_function (void * arg)
  26. {
  27. int i = (intptr_t) arg;
  28. int status;
  29. pid_t pid;
  30. pid_t pid2;
  31. pid = fork ();
  32. switch (pid)
  33. {
  34. case 0:
  35. printf ("%ld for %d\n", (long int) getpid (), i);
  36. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 * i };
  37. nanosleep (&ts, NULL);
  38. _exit (i);
  39. break;
  40. case -1:
  41. printf ("fork: %m\n");
  42. return (void *) 1l;
  43. break;
  44. }
  45. pid2 = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  46. if (pid2 != pid)
  47. {
  48. printf ("waitpid returned %ld, expected %ld\n",
  49. (long int) pid2, (long int) pid);
  50. return (void *) 1l;
  51. }
  52. printf ("%ld with %d, expected %d\n",
  53. (long int) pid, WEXITSTATUS (status), i);
  54. return WEXITSTATUS (status) == i ? NULL : (void *) 1l;
  55. }
  56. #define N 5
  57. static const int t[N] = { 7, 6, 5, 4, 3 };
  58. int
  59. main (void)
  60. {
  61. pthread_t th[N];
  62. int i;
  63. int result = 0;
  64. pthread_attr_t at;
  65. if (pthread_attr_init (&at) != 0)
  66. {
  67. puts ("attr_init failed");
  68. return 1;
  69. }
  70. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  71. {
  72. puts ("attr_setstacksize failed");
  73. return 1;
  74. }
  75. for (i = 0; i < N; ++i)
  76. if (pthread_create (&th[i], NULL, thread_function,
  77. (void *) (intptr_t) t[i]) != 0)
  78. {
  79. printf ("creation of thread %d failed\n", i);
  80. exit (1);
  81. }
  82. if (pthread_attr_destroy (&at) != 0)
  83. {
  84. puts ("attr_destroy failed");
  85. return 1;
  86. }
  87. for (i = 0; i < N; ++i)
  88. {
  89. void *v;
  90. if (pthread_join (th[i], &v) != 0)
  91. {
  92. printf ("join of thread %d failed\n", i);
  93. result = 1;
  94. }
  95. else if (v != NULL)
  96. {
  97. printf ("join %d successful, but child failed\n", i);
  98. result = 1;
  99. }
  100. else
  101. printf ("join %d successful\n", i);
  102. }
  103. return result;
  104. }
  105. #else
  106. int main(void)
  107. {
  108. printf("Skipping test on non-mmu host!\n");
  109. return 23;
  110. }
  111. #endif