-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.c
More file actions
52 lines (48 loc) · 973 Bytes
/
git.c
File metadata and controls
52 lines (48 loc) · 973 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
49
50
51
52
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
sem_t mutex,writeblock;
int data = 0,rcount = 0;
void *reader(void *arg)
{
int *f;
f = ((int *)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);
printf("Data read by the reader%d is %d\n",*f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}
void *writer(void *arg)
{
int *f;
f = ((int *)arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer%d is %d\n",*f,data);
sleep(1);
sem_post(&writeblock);
}
int main()
{
int i,b,*z;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;;i++)
{ z=&i;
pthread_create(&wtid[i],NULL,writer,(void*)z);
pthread_create(&rtid[i],NULL,reader,(void*)z);
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}
return 0;
}