1 /*
2  *  Copyright (c) 2003, Intel Corporation. All rights reserved.
3  *  Created by:  majid.awad REMOVE-THIS AT intel DOT com
4  *  This file is licensed under the GPL license.  For the full content
5  *  of this license, see the COPYING file at the top level of this
6  *  source tree.
7  */
8 
9 /*
10  *  This test case illustrate the semaphore is shared between processes when
11  *  pshared value is non-zero.
12  */
13 
14 #include <sys/types.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <semaphore.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include "posixtest.h"
23 
24 #define TEST "3-1"
25 #define FUNCTION "sem_init"
26 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
27 
28 
29 
30 sem_t psem, csem;
31 int n;
32 
main()33 int main()
34 {
35     pthread_t prod, cons;
36    void *producer(void *);
37    void *consumer(void *);
38    long cnt = 3;
39 
40    n = 0;
41    if (sem_init(&csem, 0, 0) < 0) {
42         perror("sem_init");
43         return PTS_UNRESOLVED;
44    }
45     if (sem_init(&psem, 0, 1) < 0) {
46         perror("sem_init");
47         return PTS_UNRESOLVED;
48    }
49    if (pthread_create(&prod, NULL, producer, (void *)cnt) != 0) {
50         perror("pthread_create");
51         return PTS_UNRESOLVED;
52    }
53    if (pthread_create(&cons, NULL, consumer, (void *)cnt) != 0) {
54         perror("pthread_create");
55         return PTS_UNRESOLVED;
56    }
57 
58    if (( pthread_join(prod, NULL) == 0) && ( pthread_join(cons, NULL) == 0)) {
59 	   puts("TEST PASS");
60 	   	pthread_exit(NULL);
61    		sem_destroy(&psem);
62 		sem_destroy(&csem);
63 	   return PTS_PASS;
64    } else {
65 	   puts("TEST FAILED");
66 	   return PTS_FAIL;
67    }
68 }
69 
70 
producer(void * arg)71 void * producer(void *arg)
72 {
73     int i, cnt;
74     cnt = (long)arg;
75     for (i=0; i<cnt; i++) {
76             sem_wait(&psem);
77             n++;
78             sem_post(&csem);
79     }
80     return NULL;
81 }
82 
consumer(void * arg)83 void * consumer(void *arg)
84 {
85     int i, cnt;
86     cnt = (long)arg;
87     for (i=0; i<cnt; i++) {
88            sem_wait(&csem);
89            sem_post(&psem);
90     }
91     return NULL;
92 }
93 
94