-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReaderWriter.c
77 lines (61 loc) · 1.51 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
//This code is contributed by Sourajita Dewasi
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
pthread_mutex_t mutex;
sem_t wrt;
int count = 1;
int countreader = 0;
void *Reader(void *R)
{
pthread_mutex_lock(&mutex);
countreader++;
if(countreader == 1) {
sem_wait(&wrt);
}
pthread_mutex_unlock(&mutex);
printf("Reader %d: read count as %d\n",*((int *)R),count);
pthread_mutex_lock(&mutex);
countreader--;
if(countreader == 0) {
sem_post(&wrt);
}
pthread_mutex_unlock(&mutex);
}
void *Writer(void *W)
{
sem_wait(&wrt);
count = count+1;
printf("Writer %d edit count at %d\n",(*((int *)W)),count);
sem_post(&wrt);
}
int main()
{
pthread_t read[5],write[6];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {0,1,2,3,4,5};
for(int i = 0; i < 5; i++) {
pthread_create(&read[i], NULL, (void *)Reader, (void *)&a[i]);
}
for(int i = 0; i < 6; i++) {
pthread_create(&write[i], NULL, (void *)Writer, (void *)&a[i]);
}
for(int i = 0; i < 2; i++) {
pthread_join(read[i], NULL);
}
for(int i = 2; i >= 0; i--) {
pthread_join(write[i], NULL);
}
for(int i = 2; i < 4; i++) {
pthread_join(read[i], NULL);
}
for(int i = 3; i < 6; i++) {
pthread_join(write[i], NULL);
}
for(int i = 4; i < 5; i++) {
pthread_join(read[i], NULL);
}sem_destroy(&wrt);
pthread_mutex_destroy(&mutex);
return 0;
}