-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
52 lines (49 loc) · 1.12 KB
/
queue.c
File metadata and controls
52 lines (49 loc) · 1.12 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
/* Here the leader and follower aree two semaphore variables. here when the leader arrives,it checks too see if threre is a follower waiting.If so they can both proceed. Otherwisse it waits . similar in the case of follower*/
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
sem_t leader,follower;
int n;
void* fun1(void *arg)
{
printf("entering the leader1\n");
sem_post(&follower);
sem_wait(&leader);
printf("leader1\n");
}
void* fun2(void *arg)
{
printf("entering the follower1\n");
sem_post(&leader);
sem_wait(&follower);
printf("follower1\n");
}
void* fun3(void *arg)
{
printf("entering the leader2\n");
sem_post(&leader);
sem_wait(&follower);
printf("leader2\n");
}
void* fun4(void *arg)
{
printf("entering the follower2\n");
sem_post(&leader);
sem_wait(&follower);
printf("follower2\n");
}
main()
{
n=4;
pthread_t t[n];
sem_init(&leader,0,0);
sem_init(&follower,0,0);
pthread_create(&t[0],0,fun1,0);
pthread_create(&t[1],0,fun2,0);
// pthread_create(&t[2],0,fun1,0);
// pthread_create(&t[3],0,fun2,0);
pthread_join(t[0],0);
pthread_join(t[1],0);
// pthread_join(t[2],0);
// pthread_join(t[3],0);
}