1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 #include <semaphore.h>
32 size_t ps
= sysconf (_SC_PAGESIZE
);
33 char tmpfname
[] = "/tmp/tst-sem3.XXXXXX";
41 fd
= mkstemp (tmpfname
);
44 printf ("cannot open temporary file: %m\n");
48 /* Make sure it is always removed. */
51 /* Create one page of data. */
52 memset (data
, '\0', ps
);
54 /* Write the data to the file. */
55 if (write (fd
, data
, ps
) != (ssize_t
) ps
)
61 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
62 if (mem
== MAP_FAILED
)
64 printf ("mmap failed: %m\n");
68 s
= (sem_t
*) (((uintptr_t) mem
+ __alignof (sem_t
))
69 & ~(__alignof (sem_t
) - 1));
72 if (sem_init (s
, 1, 1) == -1)
78 if (TEMP_FAILURE_RETRY (sem_wait (s
)) == -1)
80 puts ("1st wait failed");
85 if (TEMP_FAILURE_RETRY (sem_trywait (s
)) != -1)
87 puts ("trywait succeeded");
90 else if (errno
!= EAGAIN
)
92 puts ("trywait didn't return EAGAIN");
98 puts ("going to fork now");
102 puts ("fork failed");
107 /* Play some lock ping-pong. It's our turn to unlock first. */
110 puts ("child: *p != 0");
114 if (sem_post (s
) == -1)
116 puts ("child: 1st post failed");
124 if (TEMP_FAILURE_RETRY (sem_wait (s
)) == -1)
126 printf ("parent: 2nd wait failed: %m\n");
136 puts ("parent done");
142 #define TEST_FUNCTION do_test ()
143 #include "../test-skeleton.c"