-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplex.c
More file actions
48 lines (41 loc) · 780 Bytes
/
multiplex.c
File metadata and controls
48 lines (41 loc) · 780 Bytes
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
/*Multiplex: it allows multiple threads to run in the critical section at the same time.Here we initialise the semaphore to the number of threads*/
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t sem;
int count=0;
void* fun1(void *arg)
{
sem_wait(&sem);
count=count+1;
sleep(1);
printf("%d\n",count);
sem_post(&sem);
}
void* fun2(void *arg)
{
sem_wait(&sem);
count=count+1;
sleep(1);
printf("%d\n",count);
sem_post(&sem);
}
void* fun3(void *arg)
{
sem_wait(&sem);
count=count+1;
sleep(1);
printf("%d\n",count);
sem_post(&sem);
}
main()
{
pthread_t t1,t2,t3;
sem_init(&sem,0,2);
pthread_create(&t1,0,fun1,0);
pthread_create(&t2,0,fun2,0);
pthread_create(&t3,0,fun3,0);
pthread_join(t1,0);
pthread_join(t2,0);
pthread_join(t3,0);
}