-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsem.c
46 lines (40 loc) · 747 Bytes
/
sem.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
#include<stdio.h>
#include<pthread.h>
pthread_t pid1,pid2;
pthread_mutex_t mutex;
static int value =1;
void func1()
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("pid1 is %d,this is pid1 %d\n",pthread_self(),getpid());
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
void func2()
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("pid2 is %d,this is pid2%d\n",pthread_self(),getpid());
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
void main()
{
pthread_mutex_init(&mutex,NULL);
pthread_create(&pid1,NULL,func1,NULL);
printf("pid is %d\n,",getpid());
pthread_create(&pid2,NULL,func2,NULL);
/*
while(1)
{
sleep(1);
}
*/
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
}