Back to index

lightning-sunbird  0.9+nobinonly
semaping.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nspr.h"
00039 #include "plgetopt.h"
00040 
00041 #include <stdio.h>
00042 
00043 #define SHM_NAME "/tmp/counter"
00044 #define SEM_NAME1 "/tmp/foo.sem"
00045 #define SEM_NAME2 "/tmp/bar.sem"
00046 #define SEM_MODE  0666
00047 #define SHM_MODE  0666
00048 #define ITERATIONS 1000
00049 
00050 static PRBool debug_mode = PR_FALSE;
00051 static PRIntn iterations = ITERATIONS;
00052 static PRSem *sem1, *sem2;
00053 
00054 static void Help(void)
00055 {
00056     fprintf(stderr, "semaping test program usage:\n");
00057     fprintf(stderr, "\t-d           debug mode         (FALSE)\n");
00058     fprintf(stderr, "\t-c <count>   loop count         (%d)\n", ITERATIONS);
00059     fprintf(stderr, "\t-h           this message\n");
00060 }  /* Help */
00061 
00062 int main(int argc, char **argv)
00063 {
00064     PRProcess *proc;
00065     PRIntn i;
00066     char *child_argv[32];
00067     char **child_arg;
00068     char iterations_buf[32];
00069     PRSharedMemory *shm;
00070     PRIntn *counter_addr;
00071     PRInt32 exit_code;
00072     PLOptStatus os;
00073     PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
00074 
00075     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
00076         if (PL_OPT_BAD == os) continue;
00077         switch (opt->option) {
00078             case 'd':  /* debug mode */
00079                 debug_mode = PR_TRUE;
00080                 break;
00081             case 'c':  /* loop count */
00082                 iterations = atoi(opt->value);
00083                 break;
00084             case 'h':
00085             default:
00086                 Help();
00087                 return 2;
00088         }
00089     }
00090     PL_DestroyOptState(opt);
00091 
00092     if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) {
00093         fprintf(stderr, "warning: removed shared memory %s left over "
00094                 "from previous run\n", SHM_NAME);
00095     }
00096     if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
00097         fprintf(stderr, "warning: removed semaphore %s left over "
00098                 "from previous run\n", SEM_NAME1);
00099     }
00100     if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
00101         fprintf(stderr, "warning: removed semaphore %s left over "
00102                 "from previous run\n", SEM_NAME2);
00103     }
00104 
00105     shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE);
00106     if (NULL == shm) {
00107         fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
00108                 PR_GetError(), PR_GetOSError());
00109         exit(1);
00110     }
00111     counter_addr = PR_AttachSharedMemory(shm, 0);
00112     if (NULL == counter_addr) {
00113         fprintf(stderr, "PR_AttachSharedMemory failed\n");
00114         exit(1);
00115     }
00116     *counter_addr = 0;
00117     sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
00118     if (NULL == sem1) {
00119         fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
00120                 PR_GetError(), PR_GetOSError());
00121         exit(1);
00122     }
00123     sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
00124     if (NULL == sem2) {
00125         fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
00126                 PR_GetError(), PR_GetOSError());
00127         exit(1);
00128     }
00129 
00130     child_arg = &child_argv[0];
00131     *child_arg++ = "semapong";
00132     if (debug_mode != PR_FALSE) {
00133         *child_arg++ = "-d";
00134     }
00135     if (iterations != ITERATIONS) {
00136         *child_arg++ = "-c";
00137         PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations);
00138         *child_arg++ = iterations_buf;
00139     }
00140     *child_arg = NULL;
00141     proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
00142     if (NULL == proc) {
00143         fprintf(stderr, "PR_CreateProcess failed\n");
00144         exit(1);
00145     }
00146 
00147     /*
00148      * Process 1 waits on semaphore 1 and posts to semaphore 2.
00149      */
00150     for (i = 0; i < iterations; i++) {
00151         if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
00152             fprintf(stderr, "PR_WaitSemaphore failed\n");
00153             exit(1);
00154         }
00155         if (*counter_addr == 2*i) {
00156             if (debug_mode) printf("process 1: counter = %d\n", *counter_addr);
00157         } else {
00158             fprintf(stderr, "process 1: counter should be %d but is %d\n",
00159                     2*i, *counter_addr);
00160             exit(1);
00161         }
00162         (*counter_addr)++;
00163         if (PR_PostSemaphore(sem2) == PR_FAILURE) {
00164             fprintf(stderr, "PR_PostSemaphore failed\n");
00165             exit(1);
00166         }
00167     }
00168     if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
00169         fprintf(stderr, "PR_DetachSharedMemory failed\n");
00170         exit(1);
00171     }
00172     if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
00173         fprintf(stderr, "PR_CloseSharedMemory failed\n");
00174         exit(1);
00175     }
00176     if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
00177         fprintf(stderr, "PR_CloseSemaphore failed\n");
00178     }
00179     if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
00180         fprintf(stderr, "PR_CloseSemaphore failed\n");
00181     }
00182 
00183     if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
00184         fprintf(stderr, "PR_WaitProcess failed\n");
00185         exit(1);
00186     }
00187     if (exit_code != 0) {
00188         fprintf(stderr, "process 2 failed with exit code %d\n", exit_code);
00189         exit(1);
00190     }
00191 
00192     if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) {
00193         fprintf(stderr, "PR_DeleteSharedMemory failed\n");
00194     }
00195     if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
00196         fprintf(stderr, "PR_DeleteSemaphore failed\n");
00197     }
00198     if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
00199         fprintf(stderr, "PR_DeleteSemaphore failed\n");
00200     }
00201     printf("PASS\n");
00202     return 0;
00203 }