112 lines
2.7 KiB
C
112 lines
2.7 KiB
C
![]() |
// Write a C program to solve the readers and writers Problem.
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <pthread.h>
|
||
|
#include <semaphore.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#define NUM_READERS 3
|
||
|
#define NUM_WRITERS 2
|
||
|
|
||
|
// shared data structure - simple integer
|
||
|
int shared_data = 0;
|
||
|
|
||
|
// sync variables
|
||
|
pthread_mutex_t mutex; // mutex for read_count protection
|
||
|
sem_t wrt; // semaphore for writer access control
|
||
|
int read_count = 0; // tracks active readers
|
||
|
|
||
|
void* reader(void* arg) {
|
||
|
int id = *((int*)arg);
|
||
|
while (1) {
|
||
|
// read delay sim
|
||
|
sleep(1);
|
||
|
|
||
|
// critical section entry
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
read_count++;
|
||
|
if (read_count == 1) {
|
||
|
// first reader blocks writers
|
||
|
sem_wait(&wrt);
|
||
|
}
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
|
||
|
// critical section
|
||
|
printf("Reader %d is reading shared_data = %d\n", id, shared_data);
|
||
|
sleep(2); // read time sim
|
||
|
|
||
|
// critical section exit
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
read_count--;
|
||
|
if (read_count == 0) {
|
||
|
// last reader unblocks writers
|
||
|
sem_post(&wrt);
|
||
|
}
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
}
|
||
|
pthread_exit(NULL);
|
||
|
}
|
||
|
|
||
|
void* writer(void* arg) {
|
||
|
int id = *((int*)arg);
|
||
|
while (1) {
|
||
|
// write delay sim
|
||
|
sleep(3);
|
||
|
|
||
|
// critical section entry
|
||
|
sem_wait(&wrt); // exclusive access control
|
||
|
|
||
|
// critical section
|
||
|
shared_data += 1;
|
||
|
printf("Writer %d is writing: shared_data becomes %d\n", id, shared_data);
|
||
|
sleep(2); // write time sim
|
||
|
|
||
|
// critical section exit
|
||
|
sem_post(&wrt);
|
||
|
}
|
||
|
pthread_exit(NULL);
|
||
|
}
|
||
|
|
||
|
int main(void) {
|
||
|
pthread_t readers[NUM_READERS], writers[NUM_WRITERS];
|
||
|
int reader_ids[NUM_READERS], writer_ids[NUM_WRITERS];
|
||
|
int i;
|
||
|
|
||
|
// sync init
|
||
|
pthread_mutex_init(&mutex, NULL);
|
||
|
sem_init(&wrt, 0, 1); // binary semaphore init
|
||
|
|
||
|
// reader thread creation
|
||
|
for (i = 0; i < NUM_READERS; i++) {
|
||
|
reader_ids[i] = i + 1;
|
||
|
if (pthread_create(&readers[i], NULL, reader, &reader_ids[i]) != 0) {
|
||
|
perror("Failed to create reader thread");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// writer thread creation
|
||
|
for (i = 0; i < NUM_WRITERS; i++) {
|
||
|
writer_ids[i] = i + 1;
|
||
|
if (pthread_create(&writers[i], NULL, writer, &writer_ids[i]) != 0) {
|
||
|
perror("Failed to create writer thread");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// thread joining
|
||
|
for (i = 0; i < NUM_READERS; i++) {
|
||
|
pthread_join(readers[i], NULL);
|
||
|
}
|
||
|
for (i = 0; i < NUM_WRITERS; i++) {
|
||
|
pthread_join(writers[i], NULL);
|
||
|
}
|
||
|
|
||
|
// cleanup
|
||
|
pthread_mutex_destroy(&mutex);
|
||
|
sem_destroy(&wrt);
|
||
|
|
||
|
return 0;
|
||
|
}
|