-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstwriterreader.c
More file actions
executable file
·61 lines (53 loc) · 1.22 KB
/
firstwriterreader.c
File metadata and controls
executable file
·61 lines (53 loc) · 1.22 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
// src: IIIT Hyderabad, Operating Systems 2011.
#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
sem_t wrt,read,mutex;
int wrtcount=0;
char data[100] = "";
void *readThread(void *param){
sem_wait(&read);
printf("Reader Thread %ld, Data read is %s\n", (long)param,data);
sem_post(&read);
}
void *writeThread(void *param){
sem_wait(&mutex);
wrtcount++;
if(wrtcount==1)
sem_wait(&read);
sem_post(&mutex);
sem_wait(&wrt);
strcat(data, "hi ");
printf("Writer thread %ld Data written: hi\n", (long)param);
sem_post(&wrt);
sem_wait(&mutex);
wrtcount--;
if(wrtcount ==0)
sem_post(&read);
sem_post(&mutex);
}
int main(){
sem_init(&wrt,0,1);
sem_init(&read,0,1);
sem_init(&mutex,0,1);
int i;
pthread_t rthread[5];
pthread_t wthread[5];
for(i=0;i<5;i++)
{
pthread_create(&wthread[i],NULL,writeThread,(void *)i);
}
for(i=0;i<5;i++)
{
pthread_create(&rthread[i],NULL,readThread,(void *)i);
}
for(i=0;i<5;i++)
pthread_join(rthread[i],NULL);
for(i=0;i<5;i++)
pthread_join(wthread[i],NULL);
printf("Final string is %s\n",data);
pthread_exit(NULL);
return 0;
}