1 /* Test program for POSIX shm_* functions.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
34 /* We want to see output immediately. */
35 #define STDOUT_UNBUFFERED
43 /* Create the shared memory object. */
44 fd
= shm_open ("/shm-test", O_RDWR
, 0600);
47 /* We don't regard this as a bug. Simply don't run the test. It could
48 means there is no such implementation or the object is already in
49 use in which case we don't want to disturb. */
50 perror ("failed to open shared memory object: shm_open");
59 worker (int write_now
)
70 if (fstat64 (fd
, &st
) == -1)
71 error (EXIT_FAILURE
, 0, "stat failed");
72 if (st
.st_size
!= 4000)
73 error (EXIT_FAILURE
, 0, "size incorrect");
75 mem
= mmap (NULL
, 4000, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
76 if (mem
== MAP_FAILED
)
77 error (EXIT_FAILURE
, 0, "mmap failed");
80 ts
.tv_nsec
= 500000000;
83 for (i
= 0; i
<= 4; ++i
)
86 /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
89 for (i
= 0; i
<= 4; ++i
)
94 /* OK, that's done. */
97 nanosleep (&ts
, NULL
);
101 for (i
= 0; i
<= 4; ++i
)
104 /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
107 for (i
= 0; i
<= 4; ++i
)
112 /* OK, that's done. */
115 nanosleep (&ts
, NULL
);
118 if (munmap (mem
, 4000) == -1)
119 error (EXIT_FAILURE
, errno
, "munmap");
137 /* Create the shared memory object. */
138 fd
= shm_open ("/shm-test", O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600);
141 /* We don't regard this as a bug. Simply don't run the test. It could
142 means there is no such implementation or the object is already in
143 use in which case we don't want to disturb. */
144 perror ("failed to create a shared memory object: shm_open");
148 /* Size the object. We make it 4000 bytes long. */
149 if (ftruncate (fd
, 4000) == -1)
151 /* This failed. Must be a bug in the implementation of the
152 shared memory itself. */
153 perror ("failed to size of shared memory object: ftruncate");
155 shm_unlink ("/shm-test");
159 if (fstat64 (fd
, &st
) == -1)
161 shm_unlink ("/shm-test");
162 error (EXIT_FAILURE
, 0, "initial stat failed");
164 if (st
.st_size
!= 4000)
166 shm_unlink ("/shm-test");
167 error (EXIT_FAILURE
, 0, "initial size not correct");
170 /* Spawn to processes which will do the work. */
176 /* Couldn't create a second process. */
179 shm_unlink ("/shm-test");
188 /* Couldn't create a second process. */
191 kill (pid1
, SIGTERM
);
192 waitpid (pid1
, &ignore
, 0);
194 shm_unlink ("/shm-test");
198 /* Wait until the two processes are finished. */
199 waitpid (pid1
, &status1
, 0);
200 waitpid (pid2
, &status2
, 0);
202 /* Now we can unlink the shared object. */
203 shm_unlink ("/shm-test");
205 return (!WIFEXITED (status1
) || WEXITSTATUS (status1
) != 0
206 || !WIFEXITED (status2
) || WEXITSTATUS (status2
) != 0);
208 #define TEST_FUNCTION do_test ()
210 #define CLEANUP_HANDLER shm_unlink ("/shm-test");
213 #include "../test-skeleton.c"