1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1999-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
40
#define SHM_NAME "/tmp/counter"
41
#define SEM_NAME1 "/tmp/foo.sem"
42
#define SEM_NAME2 "/tmp/bar.sem"
45
#define ITERATIONS 1000
47
static PRBool debug_mode = PR_FALSE;
48
static PRIntn iterations = ITERATIONS;
49
static PRSem *sem1, *sem2;
51
static void Help(void)
53
fprintf(stderr, "semaping test program usage:\n");
54
fprintf(stderr, "\t-d debug mode (FALSE)\n");
55
fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS);
56
fprintf(stderr, "\t-h this message\n");
59
int main(int argc, char **argv)
65
char iterations_buf[32];
70
PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
72
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
73
if (PL_OPT_BAD == os) continue;
74
switch (opt->option) {
75
case 'd': /* debug mode */
78
case 'c': /* loop count */
79
iterations = atoi(opt->value);
87
PL_DestroyOptState(opt);
89
if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) {
90
fprintf(stderr, "warning: removed shared memory %s left over "
91
"from previous run\n", SHM_NAME);
93
if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
94
fprintf(stderr, "warning: removed semaphore %s left over "
95
"from previous run\n", SEM_NAME1);
97
if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
98
fprintf(stderr, "warning: removed semaphore %s left over "
99
"from previous run\n", SEM_NAME2);
102
shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE);
104
fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
105
PR_GetError(), PR_GetOSError());
108
counter_addr = PR_AttachSharedMemory(shm, 0);
109
if (NULL == counter_addr) {
110
fprintf(stderr, "PR_AttachSharedMemory failed\n");
114
sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
116
fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
117
PR_GetError(), PR_GetOSError());
120
sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
122
fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
123
PR_GetError(), PR_GetOSError());
127
child_arg = &child_argv[0];
128
*child_arg++ = "semapong";
129
if (debug_mode != PR_FALSE) {
132
if (iterations != ITERATIONS) {
134
PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations);
135
*child_arg++ = iterations_buf;
138
proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
140
fprintf(stderr, "PR_CreateProcess failed\n");
145
* Process 1 waits on semaphore 1 and posts to semaphore 2.
147
for (i = 0; i < iterations; i++) {
148
if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
149
fprintf(stderr, "PR_WaitSemaphore failed\n");
152
if (*counter_addr == 2*i) {
153
if (debug_mode) printf("process 1: counter = %d\n", *counter_addr);
155
fprintf(stderr, "process 1: counter should be %d but is %d\n",
160
if (PR_PostSemaphore(sem2) == PR_FAILURE) {
161
fprintf(stderr, "PR_PostSemaphore failed\n");
165
if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
166
fprintf(stderr, "PR_DetachSharedMemory failed\n");
169
if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
170
fprintf(stderr, "PR_CloseSharedMemory failed\n");
173
if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
174
fprintf(stderr, "PR_CloseSemaphore failed\n");
176
if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
177
fprintf(stderr, "PR_CloseSemaphore failed\n");
180
if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
181
fprintf(stderr, "PR_WaitProcess failed\n");
184
if (exit_code != 0) {
185
fprintf(stderr, "process 2 failed with exit code %d\n", exit_code);
189
if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) {
190
fprintf(stderr, "PR_DeleteSharedMemory failed\n");
192
if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
193
fprintf(stderr, "PR_DeleteSemaphore failed\n");
195
if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
196
fprintf(stderr, "PR_DeleteSemaphore failed\n");