2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 /* Test shared memory */
33 #include <sys/types.h>
34 #include <sys/param.h>
61 pgsize = sysconf(_SC_PAGESIZE);
64 // printf("seed(%d) = %d\n", getpid(), seed);
65 shmkey = ftok("/tmp", seed);
66 if ((shmid = shmget(shmkey, 10 * pgsize, IPC_CREAT | IPC_EXCL | 0640)) == -1) {
67 if (errno == ENOSPC) {
68 fprintf(stderr, "Max number of semaphores reached.\n");
71 err(1, "shmget (%s:%d)", __FILE__, __LINE__);
75 if ((shm_buf = shmat(shmid, NULL, 0)) == (void *) -1)
76 err(1, "sender: shmat (%s:%d)", __FILE__, __LINE__);
78 // printf("seed(%d) = %d\n", getpid(), seed);
79 semkey = ftok("/var", seed);
80 if ((semid = semget(semkey, 2, IPC_CREAT | IPC_EXCL | 0640)) == -1) {
81 if (errno == ENOSPC) {
82 fprintf(stderr, "Max number of semaphores reached.\n");
85 err(1, "semget (%s:%d)", __FILE__, __LINE__);
87 /* Initialize the semaphore. */
89 sop[0].sem_op = 0; /* This is the number of runs without queuing. */
92 sop[1].sem_op = 0; /* This is the number of runs without queuing. */
94 if (semop(semid, sop, 2) == -1)
95 err(1, "init: semop (%s:%d)", __FILE__, __LINE__);
103 if (shmctl(shmid, IPC_RMID, NULL) == -1 && errno != EINVAL)
104 warn("shmctl IPC_RMID (%s:%d)", __FILE__, __LINE__);
106 if (semctl(semid, 0, IPC_RMID, 0) == -1 && errno != EINVAL)
107 warn("shmctl IPC_RMID (%s:%d)", __FILE__, __LINE__);
114 if (semop(semid, sop, 1) == -1) {
115 if (errno != EINTR && errno != EIDRM && errno != EINVAL)
116 warn("Wait: semop (%s:%d)", __FILE__, __LINE__);
125 if (semop(semid, sop, 1) == -1) {
126 if (errno != EINTR && errno != EIDRM && errno != EINVAL)
127 warn("Sig: semop (%s:%d)", __FILE__, __LINE__);
143 if (pid == 0) { /* child */
147 if (done_testing == 1)
149 if (shm_buf[i] != (i % 128)) {
151 "child %d: expected %d, got %d\n",
152 getpid(), i % 128, shm_buf[i]);
156 i = (i + 1) % (10 * pgsize);
157 shm_buf[i] = (i % 128);
158 i = (i + 1) % (10 * pgsize);
163 } else { /* parent */
166 shm_buf[i] = (i % 128);
168 i = (i + 1) % (10 * pgsize);
170 if (done_testing == 1)
172 if (shm_buf[i] != (i % 128)) {
174 "parent(%d): expected %d, got %d\n",
175 getpid(), i % 128, shm_buf[i]);
179 i = (i + 1) % (10 * pgsize);