-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_abc.c
59 lines (50 loc) · 834 Bytes
/
print_abc.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
53
54
55
56
57
58
59
#include<stdio.h>
#include<unistd.h>
#include<semaphore.h>
#include<pthread.h>
sem_t sema, semb, semc;
void* printa(void* arg)
{
while(1)
{
sem_wait(&sema);
write(1,"a",1);
sem_post(&semb);
sleep(1);
}
}
void* printb(void* arg)
{
while(1)
{
sem_wait(&semb);
write(1,"b",1);
sem_post(&semc);
}
}
void* printc(void* arg)
{
while(1)
{
sem_wait(&semc);
write(1,"c",1);
sem_post(&sema);
}
}
int main()
{
sem_init(&sema, 0, 1);
sem_init(&semb, 0, 0);
sem_init(&semc, 0, 0);
pthread_t apid, bpid, cpid;
pthread_create(&apid, NULL, printa, NULL);
pthread_create(&bpid, NULL, printb, NULL);
pthread_create(&cpid, NULL, printc, NULL);
pthread_join(apid, NULL);
pthread_join(bpid, NULL);
pthread_join(cpid, NULL);
sem_destroy(&sema);
sem_destroy(&semb);
sem_destroy(&semc);
//exit(0);
}