-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier.c
More file actions
65 lines (58 loc) · 1.04 KB
/
barrier.c
File metadata and controls
65 lines (58 loc) · 1.04 KB
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
60
61
62
63
64
65
/* Here the first n-1 threads arrive they block until the nth thread arrives,at which point all the threads may proceed */
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t mutex,barrier;
int count=0,n;
void* fun1(void *arg)
{
sem_wait(&mutex);
printf("a1\n");
count++;
sem_post(&mutex);
if(count==n){
sem_post(&barrier);
}
sem_wait(&barrier);
printf("b1\n");
sem_post(&barrier);
}
void* fun2(void *arg)
{
sem_wait(&mutex);
printf("a2\n");
count++;
sem_post(&mutex);
if(count==n){
sem_post(&barrier);
}
sem_wait(&barrier);
printf("b2\n");
sem_post(&barrier);
}
void* fun3(void *arg)
{
sem_wait(&mutex);
printf("a3\n");
count++;
sem_post(&mutex);
if(count==n){
sem_post(&barrier);
}
sem_wait(&barrier);
printf("b3\n");
sem_post(&barrier);
}
main()
{
n=3;
pthread_t t[n];
sem_init(&mutex,0,1);
sem_init(&barrier,0,0);
pthread_create(&t[0],0,fun1,0);
pthread_create(&t[1],0,fun2,0);
pthread_create(&t[2],0,fun3,0);
pthread_join(t[0],0);
pthread_join(t[1],0);
pthread_join(t[2],0);
}