tst-atfork1.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@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 <unistd.h>
  20. #include <sys/wait.h>
  21. #include "../test-skeleton.h"
  22. #ifdef __ARCH_USE_MMU__
  23. static int val;
  24. static void
  25. prepare1 (void)
  26. {
  27. val *= 2;
  28. }
  29. static void
  30. prepare2 (void)
  31. {
  32. ++val;
  33. }
  34. static void
  35. parent1 (void)
  36. {
  37. val += 4;
  38. }
  39. static void
  40. parent2 (void)
  41. {
  42. val *= 4;
  43. }
  44. static void
  45. child1 (void)
  46. {
  47. val += 8;
  48. }
  49. static void
  50. child2 (void)
  51. {
  52. val *= 8;
  53. }
  54. static int
  55. do_test (void)
  56. {
  57. pid_t pid;
  58. int status = 0;
  59. if (pthread_atfork (prepare1, parent1, child1) != 0)
  60. {
  61. puts ("1st atfork failed");
  62. exit (1);
  63. }
  64. if (pthread_atfork (prepare2, parent2, child2) != 0)
  65. {
  66. puts ("2nd atfork failed");
  67. exit (1);
  68. }
  69. pid = fork ();
  70. if (pid == -1)
  71. {
  72. puts ("fork failed");
  73. exit (1);
  74. }
  75. if (pid != 0)
  76. {
  77. /* Parent. */
  78. if (val != 24)
  79. {
  80. printf ("expected val=%d, got %d\n", 24, val);
  81. exit (1);
  82. }
  83. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  84. {
  85. puts ("waitpid failed");
  86. exit (1);
  87. }
  88. }
  89. else
  90. {
  91. /* Child. */
  92. if (val != 80)
  93. {
  94. printf ("expected val=%d, got %d\n", 80, val);
  95. exit (2);
  96. }
  97. }
  98. return status;
  99. }
  100. #define TEST_FUNCTION do_test ()
  101. #include "../test-skeleton.c"
  102. #else
  103. int main(void)
  104. {
  105. printf("Skipping test on non-mmu host!\n");
  106. return 23;
  107. }
  108. #endif