-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_readers_writers_2.c
145 lines (132 loc) · 3.07 KB
/
06_readers_writers_2.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <sched.h>
#include "queue.h"
#define max 6
int rc=0,w=0;
sem_t mutex,wrt;
pthread_attr_t tattr;
pthread_t tid[max];
int ret;
struct sched_param param;
struct Queue *wq, *rq;
struct QNode *qn;
void *reader(void *arg)
{
srand(time(0));
int id = *((int *) arg);
FILE *f;
while(1)
{
printf("Reader %d arrived",id);
if(!isQueueEmpty(rq))
printf(", Waiting in read queue\n");
else
printf("\n");
enQueue(rq, id);
printf("Read Queue: ");
printQueue(rq);
printf("\n");
while(w>0);
// goto b;
int t=(rand()%3)+1;
sem_wait(&mutex);
rc++;
if(rc==1)
sem_wait(&wrt);
sem_post(&mutex);
f = fopen("Object.txt","r");
sleep(1);
char str[100];
if( fgets (str, 60, f)!=NULL ) {
printf("Reader %d reads %s from the file\n",id,str);
}
fclose(f);
sem_wait(&mutex);
rc--;
qn = deQueue(rq);
printf("Read Queue: ");
printQueue(rq);
printf("\n");
if(rc==0)
sem_post(&wrt);
sem_post(&mutex);
//b: sleep(t);
}
pthread_exit(0);
}
void *writer(void *arg)
{
srand(time(0));
int id = *((int *) arg);
FILE *f;
while(1)
{
w++;
int t=(rand()%3)+2;
printf("Writer %d arrived",id);
if(!isQueueEmpty(wq))
printf(", Waiting in write queue\n");
else
printf("\n");
enQueue(wq, id);
printf("Writer Queue: ");
printQueue(wq);
printf("\n");
sem_wait(&wrt);
f = fopen("Object.txt","w");
sleep(1);
int n=rand()%10;
fprintf(f,"%d",n);
printf("Writer %d writes %d to the file\n",id,n);
fclose(f);
sem_post(&wrt);
qn = deQueue(wq);
printf("Writer Queue: ");
printQueue(wq);
printf("\n");
w--;
sleep(t);
}
pthread_exit(0);
}
int main()
{
wq = createQueue();
rq = createQueue();
srand(time(0));
int id[max],i;
FILE *f;
sem_init(&wrt,0,1);
sem_init(&mutex,0,1);
id[0]=0;
id[1]=0;
id[2]=1;
id[3]=1;
id[4]=2;
id[5]=2;
f = fopen("Object.txt","w");
int n = rand();
fprintf(f,"%d",n);
fclose(f);
ret=pthread_create(&tid[0],&tattr,writer,(void *) &id[0]);
printf("Writer %d created\n",id[0]);
pthread_create(&tid[1],NULL,reader,(void *) &id[1]);
printf("Reader %d created\n",id[1]);
ret=pthread_create(&tid[2],&tattr,writer,(void *) &id[2]);
printf("Writer %d created\n",id[2]);
pthread_create(&tid[3],NULL,reader,(void *) &id[3]);
printf("Reader %d created\n",id[3]);
ret=pthread_create(&tid[4],&tattr,writer,(void *) &id[4]);
printf("Writer %d created\n",id[4]);
pthread_create(&tid[5],NULL,reader,(void *) &id[5]);
printf("Reader %d created\n",id[5]);
for(i=0;i<max;i++)
pthread_join(tid[i],NULL);
return 0;
}