©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/semtest.c

Go to the documentation of this file.
00001 #include "defs.h"
00002 #include <semaphore.h>
00003 
00004 #define SEMFILE "/testsem"
00005 
00006 int main(void)
00007 {
00008     sem_t *sem;
00009     pid_t pid;
00010     int val, i;
00011 
00012     (void)sem_unlink(SEMFILE);
00013     ec_cmp( sem = sem_open(SEMFILE, O_CREAT, 0666, 5), SEM_FAILED )
00014     if ((pid = fork()) == 0) {
00015         while (true) {
00016             printf("child waiting...\n");
00017             ec_neg1( sem_wait(sem) )
00018             ec_neg1( sem_getvalue(sem, &val) )
00019             printf("child got semaphore; value = %d\n", val);
00020         }
00021     }
00022     printf("parent ready for post loop\n");
00023     for (i = 0; i < 3; i++) {
00024         ec_neg1( sem_post(sem) )
00025         sleep(1);
00026     }
00027     kill(pid, SIGKILL);
00028     waitpid(pid, NULL, 0);
00029     printf("parent done\n");
00030     exit(EXIT_SUCCESS);
00031 
00032 EC_CLEANUP_BGN
00033     exit(EXIT_FAILURE);
00034 EC_CLEANUP_END
00035 }

Generated on Fri Apr 23 10:56:58 2004 for AUP2 Example Source by doxygen 1.3.1