-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path信号_线程锁_PV操作.c
52 lines (45 loc) · 900 Bytes
/
信号_线程锁_PV操作.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* sem_mutex.c */
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<semaphore.h>
void *producter_f (void *arg);
void *consumer_f(void *arg);
sem_t sem;
int running = 1;
int main(void)
{
pthread_t consumer_t;
pthread_t producter_t;
sem_init(&sem,0,16);
pthread_create(&producter_t,NULL,(void*)producter_f,NULL);
pthread_create(&consumer_t,NULL,(void*)consumer_f,NULL);
sleep(1);
running=0;
pthread_join(consumer_t,NULL);
pthread_join(producter_t,NULL);
sem_destroy(&sem);
return 0;
}
void *producter_f(void *arg)
{
int semval=0;
while(running)
{
usleep(1);
sem_post(&sem);
sem_getvalue(&sem,&semval);
printf("produce,all numb:%d\n",semval);
}
}
void *consumer_f(void *arg)
{
int semval=0;
while(running)
{
usleep(1);
sem_wait(&sem);
sem_getvalue(&sem,&semval);
printf("consumer,all numb:%d\n",semval);
}
}