1 /* Test program for POSIX shm_* functions.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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 (fstat (fd
, &st
) == -1 || st
.st_size
!= 4000)
71 error (EXIT_FAILURE
, 0, "stat failed");
73 mem
= mmap (NULL
, 4000, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
75 error (EXIT_FAILURE
, 0, "mmap failed");
78 ts
.tv_nsec
= 500000000;
81 for (i
= 0; i
<= 4; ++i
)
84 /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
87 for (i
= 0; i
<= 4; ++i
)
92 /* OK, that's done. */
95 nanosleep (&ts
, NULL
);
99 for (i
= 0; i
<= 4; ++i
)
102 /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
105 for (i
= 0; i
<= 4; ++i
)
110 /* OK, that's done. */
113 nanosleep (&ts
, NULL
);
116 if (munmap (mem
, 4000) == -1)
117 error (EXIT_FAILURE
, errno
, "munmap");
134 /* Create the shared memory object. */
135 fd
= shm_open ("/shm-test", O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600);
138 /* We don't regard this as a bug. Simply don't run the test. It could
139 means there is no such implementation or the object is already in
140 use in which case we don't want to disturb. */
141 perror ("failed to create a shared memory object: shm_open");
145 /* Size the object. We make it 4000 bytes long. */
146 if (ftruncate (fd
, 4000) == -1)
148 /* This failed. Must be a bug in the implementation of the
149 shared memory itself. */
150 perror ("failed to size of shared memory object: ftruncate");
152 shm_unlink ("/shm-test");
156 /* Spawn to processes which will do the work. */
162 /* Couldn't create a second process. */
165 shm_unlink ("/shm-test");
174 /* Couldn't create a second process. */
177 kill (pid1
, SIGTERM
);
178 waitpid (pid1
, &ignore
, 0);
180 shm_unlink ("/shm-test");
184 /* Wait until the two processes are finished. */
185 waitpid (pid1
, &status1
, 0);
186 waitpid (pid2
, &status2
, 0);
188 /* Now we can unlink the shared object. */
189 shm_unlink ("/shm-test");
191 return (!WIFEXITED (status1
) || WEXITSTATUS (status1
) != 0
192 || !WIFEXITED (status2
) || WEXITSTATUS (status2
) != 0);
194 #define TEST_FUNCTION do_test ()
196 #define CLEANUP_HANDLER shm_unlink ("/shm-test");
199 #include "../test-skeleton.c"