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