©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c7/sstest.c

Go to the documentation of this file.
00001 /*
00002     Simple Semaphore test program
00003     AUP2, Sec. 7.09.2, 7.10.1
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include "defs.h"
00022 #include "SimpleSem.h"
00023 
00024 /*
00025     For POSIX portability, name must begin with slash and contain no other slashes. If SystemV semaphores are used, however, this would require write permission in the root directory.
00026 */
00027 #define SEMNAME "simplesemtest"
00028 
00029 int main(void)
00030 {
00031     struct SimpleSem *sem;
00032     pid_t pid;
00033     int i;
00034 
00035     ec_false( SimpleSemRemove(SEMNAME) )
00036     if ((pid = fork()) == 0) {
00037         ec_null( sem = SimpleSemOpen(SEMNAME) )
00038         printf("child has semaphore open\n");
00039         while (true) {
00040             ec_false( SimpleSemWait(sem) )
00041             printf("child returned from wait\n");
00042         }
00043         /* won't get here */
00044         ec_false( SimpleSemClose(sem) )
00045         exit(EXIT_SUCCESS);
00046     }
00047 sleep(1);
00048     ec_null( sem = SimpleSemOpen(SEMNAME) )
00049     printf("parent has semaphore open\n");
00050     for (i = 0; i < 10; i++) {
00051         printf("parent posting (iteration %d)\n", i);
00052         ec_false( SimpleSemPost(sem) )
00053         sleep(1);
00054     }
00055     ec_false( SimpleSemClose(sem) )
00056     ec_neg1( kill(pid, SIGINT) )
00057     exit(EXIT_SUCCESS);
00058 
00059 EC_CLEANUP_BGN
00060     exit(EXIT_FAILURE);
00061 EC_CLEANUP_END
00062 }

Generated on Fri Apr 23 10:57:01 2004 for AUP2 Example Source by doxygen 1.3.1