-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.c
97 lines (78 loc) · 2.26 KB
/
reader.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
// Shared resource
int data = 0;
int reader_count = 0; // Keeps track of the number of readers
// Semaphores and mutex
sem_t rw_mutex; // Controls access to the data for readers and writers
pthread_mutex_t mutex; // Controls access to reader_count
// Reader function
void* reader(void* arg) {
int id = ((int)arg);
while (1) {
// Entry section
pthread_mutex_lock(&mutex);
reader_count++;
if (reader_count == 1) {
// First reader locks the resource
sem_wait(&rw_mutex);
}
pthread_mutex_unlock(&mutex);
// Critical section
printf("Reader %d is reading data: %d\n", id, data);
// Exit section
pthread_mutex_lock(&mutex);
reader_count--;
if (reader_count == 0) {
// Last reader unlocks the resource
sem_post(&rw_mutex);
}
pthread_mutex_unlock(&mutex);
sleep(1); // Simulate reading time
}
}
// Writer function
void* writer(void* arg) {
int id = ((int)arg);
while (1) {
// Entry section
sem_wait(&rw_mutex);
// Critical section
data = rand() % 100; // Simulate writing
printf("Writer %d is writing data: %d\n", id, data);
// Exit section
sem_post(&rw_mutex);
sleep(2); // Simulate writing time
}
}
int main() {
pthread_t readers[5], writers[2];
int ids[5];
// Initialize semaphores and mutex
sem_init(&rw_mutex, 0, 1);
pthread_mutex_init(&mutex, NULL);
// Create writer threads
for (int i = 0; i < 2; i++) {
ids[i] = i + 1;
pthread_create(&writers[i], NULL, writer, &ids[i]);
}
// Create reader threads
for (int i = 0; i < 5; i++) {
ids[i] = i + 1;
pthread_create(&readers[i], NULL, reader, &ids[i]);
}
// Join threads (this program runs indefinitely, so technically unreachable)
for (int i = 0; i < 2; i++) {
pthread_join(writers[i], NULL);
}
for (int i = 0; i < 5; i++) {
pthread_join(readers[i], NULL);
}
// Destroy semaphores and mutex
sem_destroy(&rw_mutex);
pthread_mutex_destroy(&mutex);
return 0;
}