tst-atfork1.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. static int val;
  23. static void
  24. prepare1 (void)
  25. {
  26. val *= 2;
  27. }
  28. static void
  29. prepare2 (void)
  30. {
  31. ++val;
  32. }
  33. static void
  34. parent1 (void)
  35. {
  36. val += 4;
  37. }
  38. static void
  39. parent2 (void)
  40. {
  41. val *= 4;
  42. }
  43. static void
  44. child1 (void)
  45. {
  46. val += 8;
  47. }
  48. static void
  49. child2 (void)
  50. {
  51. val *= 8;
  52. }
  53. static int
  54. do_test (void)
  55. {
  56. pid_t pid;
  57. int status = 0;
  58. if (pthread_atfork (prepare1, parent1, child1) != 0)
  59. {
  60. puts ("1st atfork failed");
  61. exit (1);
  62. }
  63. if (pthread_atfork (prepare2, parent2, child2) != 0)
  64. {
  65. puts ("2nd atfork failed");
  66. exit (1);
  67. }
  68. pid = fork ();
  69. if (pid == -1)
  70. {
  71. puts ("fork failed");
  72. exit (1);
  73. }
  74. if (pid != 0)
  75. {
  76. /* Parent. */
  77. if (val != 24)
  78. {
  79. printf ("expected val=%d, got %d\n", 24, val);
  80. exit (1);
  81. }
  82. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  83. {
  84. puts ("waitpid failed");
  85. exit (1);
  86. }
  87. }
  88. else
  89. {
  90. /* Child. */
  91. if (val != 80)
  92. {
  93. printf ("expected val=%d, got %d\n", 80, val);
  94. exit (2);
  95. }
  96. }
  97. return status;
  98. }
  99. #define TEST_FUNCTION do_test ()
  100. #include "../test-skeleton.c"