1 /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #include <semaphore.h>
33 size_t ps
= sysconf (_SC_PAGESIZE
);
34 char tmpfname
[] = "/tmp/tst-sem3.XXXXXX";
42 fd
= mkstemp (tmpfname
);
45 printf ("cannot open temporary file: %m\n");
49 /* Make sure it is always removed. */
52 /* Create one page of data. */
53 memset (data
, '\0', ps
);
55 /* Write the data to the file. */
56 if (write (fd
, data
, ps
) != (ssize_t
) ps
)
62 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
63 if (mem
== MAP_FAILED
)
65 printf ("mmap failed: %m\n");
69 s
= (sem_t
*) (((uintptr_t) mem
+ __alignof (sem_t
))
70 & ~(__alignof (sem_t
) - 1));
73 if (sem_init (s
, 1, 1) == -1)
79 if (TEMP_FAILURE_RETRY (sem_wait (s
)) == -1)
81 puts ("1st wait failed");
86 if (TEMP_FAILURE_RETRY (sem_trywait (s
)) != -1)
88 puts ("trywait succeeded");
91 else if (errno
!= EAGAIN
)
93 puts ("trywait didn't return EAGAIN");
99 puts ("going to fork now");
103 puts ("fork failed");
108 /* Play some lock ping-pong. It's our turn to unlock first. */
111 puts ("child: *p != 0");
115 if (sem_post (s
) == -1)
117 puts ("child: 1st post failed");
125 if (TEMP_FAILURE_RETRY (sem_wait (s
)) == -1)
127 printf ("parent: 2nd wait failed: %m\n");
137 puts ("parent done");
143 #define TEST_FUNCTION do_test ()
144 #include "../test-skeleton.c"