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 |
|
---|
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.
|
---|
9 |
|
---|
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.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU Lesser General Public
|
---|
16 | License along with the GNU C Library; if not, write to the Free
|
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
18 | 02111-1307 USA. */
|
---|
19 |
|
---|
20 | #include <errno.h>
|
---|
21 | #include <semaphore.h>
|
---|
22 | #include <stdint.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <string.h>
|
---|
26 | #include <unistd.h>
|
---|
27 | #include <sys/mman.h>
|
---|
28 | #include <sys/wait.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | int
|
---|
32 | main (void)
|
---|
33 | {
|
---|
34 | size_t ps = sysconf (_SC_PAGESIZE);
|
---|
35 | char tmpfname[] = "/tmp/tst-sem3.XXXXXX";
|
---|
36 | char data[ps];
|
---|
37 | void *mem;
|
---|
38 | int fd;
|
---|
39 | sem_t *s;
|
---|
40 | pid_t pid;
|
---|
41 | char *p;
|
---|
42 |
|
---|
43 | fd = mkstemp (tmpfname);
|
---|
44 | if (fd == -1)
|
---|
45 | {
|
---|
46 | printf ("cannot open temporary file: %m\n");
|
---|
47 | exit (1);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /* Make sure it is always removed. */
|
---|
51 | unlink (tmpfname);
|
---|
52 |
|
---|
53 | /* Create one page of data. */
|
---|
54 | memset (data, '\0', ps);
|
---|
55 |
|
---|
56 | /* Write the data to the file. */
|
---|
57 | if (write (fd, data, ps) != (ssize_t) ps)
|
---|
58 | {
|
---|
59 | puts ("short write");
|
---|
60 | exit (1);
|
---|
61 | }
|
---|
62 |
|
---|
63 | mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
---|
64 | if (mem == MAP_FAILED)
|
---|
65 | {
|
---|
66 | printf ("mmap failed: %m\n");
|
---|
67 | exit (1);
|
---|
68 | }
|
---|
69 |
|
---|
70 | s = (sem_t *) (((uintptr_t) mem + __alignof (sem_t))
|
---|
71 | & ~(__alignof (sem_t) - 1));
|
---|
72 | p = (char *) (s + 1);
|
---|
73 |
|
---|
74 | if (sem_init (s, 1, 1) == -1)
|
---|
75 | {
|
---|
76 | puts ("init failed");
|
---|
77 | exit (1);
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
|
---|
81 | {
|
---|
82 | puts ("1st wait failed");
|
---|
83 | exit (1);
|
---|
84 | }
|
---|
85 |
|
---|
86 | errno = 0;
|
---|
87 | if (TEMP_FAILURE_RETRY (sem_trywait (s)) != -1)
|
---|
88 | {
|
---|
89 | puts ("trywait succeeded");
|
---|
90 | exit (1);
|
---|
91 | }
|
---|
92 | else if (errno != EAGAIN)
|
---|
93 | {
|
---|
94 | puts ("trywait didn't return EAGAIN");
|
---|
95 | exit (1);
|
---|
96 | }
|
---|
97 |
|
---|
98 | *p = 0;
|
---|
99 |
|
---|
100 | puts ("going to fork now");
|
---|
101 | pid = fork ();
|
---|
102 | if (pid == -1)
|
---|
103 | {
|
---|
104 | puts ("fork failed");
|
---|
105 | exit (1);
|
---|
106 | }
|
---|
107 | else if (pid == 0)
|
---|
108 | {
|
---|
109 | /* Play some lock ping-pong. It's our turn to unlock first. */
|
---|
110 | if ((*p)++ != 0)
|
---|
111 | {
|
---|
112 | puts ("child: *p != 0");
|
---|
113 | exit (1);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (sem_post (s) == -1)
|
---|
117 | {
|
---|
118 | puts ("child: 1st post failed");
|
---|
119 | exit (1);
|
---|
120 | }
|
---|
121 |
|
---|
122 | puts ("child done");
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
|
---|
127 | {
|
---|
128 | printf ("parent: 2nd wait failed: %m\n");
|
---|
129 | exit (1);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (*p != 1)
|
---|
133 | {
|
---|
134 | puts ("*p != 1");
|
---|
135 | exit (1);
|
---|
136 | }
|
---|
137 |
|
---|
138 | puts ("parent done");
|
---|
139 | }
|
---|
140 |
|
---|
141 | exit (0);
|
---|
142 | }
|
---|