nfs-ganesha 1.4

test_sem2.c

Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <stdio.h>
00006 #include <pthread.h>
00007 #include <errno.h>
00008 #include <unistd.h>
00009 #include <stdlib.h>
00010 #include "SemN.h"
00011 #include "log_macros.h"
00012 
00013 semaphore_t x;
00014 
00015 void *sem_me(void *arg)
00016 {
00017         int me = (int)arg;
00018         semaphore_P(&x);
00019         printf("%d: Got it\n", me);
00020         sleep(1);
00021         semaphore_V(&x);
00022         printf("%d: dropped it\n", me);
00023 }
00024 
00025 int main()
00026 {
00027         int i;
00028         pthread_t t[3];
00029 
00030         semaphore_init(&x, 2);
00031 
00032         for (i=0; i < 3; i++) {
00033                 pthread_create(t+i, NULL, sem_me, (void *)i);
00034         }
00035 
00036         for (i=0; i < 3; i++) {
00037                 pthread_join(t[i], NULL);
00038         }
00039 
00040         return 0;
00041 }