-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaderwriter.c
90 lines (73 loc) · 2.23 KB
/
readerwriter.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define X 5
#define NUM_WRITERS 5
#define NUM_READERS 5
int GLOBAL_VAR = 0;
int num_resource = 0;
void * writer(void * param);
void * reader(void * param);
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c_writer = PTHREAD_COND_INITIALIZER;
pthread_cond_t c_reader = PTHREAD_COND_INITIALIZER;
int main(int argc, const char * argv[]) {
time_t t;
srand((unsigned) time(&t));
pthread_t readers[NUM_READERS];
pthread_t writers[NUM_WRITERS];
for(int i = 0; i < NUM_READERS; i++) {
pthread_create(readers + i, NULL, reader, NULL);
}
for(int i = 0; i < NUM_WRITERS; i++) {
pthread_create(writers + i, NULL, writer, NULL);
}
for(int i = 0; i < NUM_READERS; i++) {
pthread_join(readers[i], NULL);
}
for(int i = 0; i < NUM_WRITERS; i++) {
pthread_join(writers[i], NULL);
}
printf("Parent thread quiting...\n");
}
void * writer(void * param) {
for(int i = 0; i < X; i++) {
int rand_num = rand() % 50;
usleep(rand_num);
pthread_mutex_lock(&m);
while(num_resource != 0) {
pthread_cond_wait(&c_writer, &m);
}
num_resource = -1;
pthread_mutex_unlock(&m);
// Write i //
GLOBAL_VAR = rand_num;
printf("WRITER : VALUE %d WRITTEN\n", rand_num);
printf("%d READERS ARE PRESENT\n", num_resource + 1);
num_resource = 0;
pthread_cond_broadcast(&c_reader);
pthread_cond_signal(&c_writer);
}
}
void * reader(void * param) {
for(int i = 0; i < X; i++) {
int rand_num = rand() % 50;
usleep(rand_num);
pthread_mutex_lock(&m);
while(num_resource == -1) {
pthread_cond_wait(&c_reader, &m);
}
num_resource++;
pthread_mutex_unlock(&m);
// Read GLOBAL_VAR //
printf("READER: VALUE %d FROM GLOBAL_VAR READ.\n", GLOBAL_VAR);
printf("%d READERS ARE PRESENT\n", num_resource);
pthread_mutex_lock(&m);
num_resource--;
pthread_mutex_unlock(&m);
if(num_resource == 0) {
pthread_cond_signal(&c_writer);
}
}
}