Readers–Writers Problem with Reader Priority Using Semaphores

 

Readers–Writers Problem Using Semaphores


1. AIM

To implement the Readers–Writers synchronization problem using semaphores, allowing multiple readers to access shared data simultaneously while writers get exclusive access.


2. OBJECTIVES

  • Understand the challenges of concurrent access to shared resources.

  • Learn how to use binary and counting semaphores.

  • Implement synchronization logic to ensure:

    • Multiple readers can read concurrently.

    • Only one writer can write at a time.

    • No reader should read while writer is writing.

  • Demonstrate classical process synchronization in Operating Systems.


3. THEORY

Readers–Writers Problem

A shared resource (database, file, variable) is accessed by two types of processes:

Readers

  • Only read the data.

  • Do not modify it.

  • Many readers can read simultaneously.

Writers

  • Modify the data.

  • Must have exclusive access.

  • No other reader or writer can access the resource while a writer is writing.

The main goal is to ensure correctness and consistency of shared data.


4. CONSTRAINTS OF THE PROBLEM

  1. Multiple readers can read at the same time.

  2. Only one writer can write at a time.

  3. No reader should read while writer is writing.

  4. No two writers should write at the same time.


5. SEMAPHORES USED

SemaphoreTypePurpose
mutexBinaryProtects read_count variable
rw_mutexBinaryControls access to shared resource (writers or first/last reader)

Variable

int read_count = 0;

Keeps track of number of active readers.


6. ALGORITHM


Reader Algorithm

wait(mutex) read_count++ if read_count == 1: wait(rw_mutex) // first reader locks writers out signal(mutex) READ DATA wait(mutex) read_count-- if read_count == 0: signal(rw_mutex) // last reader unlocks resource signal(mutex)

Writer Algorithm


wait(rw_mutex) // Only one writer at a time WRITE DATA signal(rw_mutex)

This is the Reader-Priority solution (standard first readers-writers implementation).


7. C PROGRAM IMPLEMENTATION (POSIX Threads + Semaphores)

#include <stdio.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> sem_t rw_mutex; // Controls access to shared resource sem_t mutex; // Controls read_count access int read_count = 0; void *reader(void *arg) { int id = *(int *)arg; sem_wait(&mutex); read_count++; if (read_count == 1) sem_wait(&rw_mutex); // First reader blocks writers sem_post(&mutex); // Critical Section printf("Reader %d is READING\n", id); sleep(1); printf("Reader %d is DONE reading\n", id); sem_wait(&mutex); read_count--; if (read_count == 0) sem_post(&rw_mutex); // Last reader releases writers sem_post(&mutex); return NULL; } void *writer(void *arg) { int id = *(int *)arg; sem_wait(&rw_mutex); // Writer needs exclusive access // Critical Section printf("\tWriter %d is WRITING\n", id); sleep(2); printf("\tWriter %d is DONE writing\n", id); sem_post(&rw_mutex); return NULL; } int main() { pthread_t r[5], w[5]; int ids[5]; sem_init(&rw_mutex, 0, 1); sem_init(&mutex, 0, 1); for (int i = 0; i < 5; i++) ids[i] = i + 1; for (int i = 0; i < 5; i++) { pthread_create(&r[i], NULL, reader, &ids[i]); pthread_create(&w[i], NULL, writer, &ids[i]); } for (int i = 0; i < 5; i++) { pthread_join(r[i], NULL); pthread_join(w[i], NULL); } sem_destroy(&rw_mutex); sem_destroy(&mutex); return 0; }

8. SAMPLE OUTPUT

binuvp@debian-workstation:~$ gcc rw.c
binuvp@debian-workstation:~$ ./a.out
Reader 1 is READING
Reader 3 is READING
Reader 2 is READING
Reader 4 is READING
Reader 5 is READING
Reader 3 is DONE reading
Reader 2 is DONE reading
Reader 1 is DONE reading
Reader 5 is DONE reading
Reader 4 is DONE reading
        Writer 1 is WRITING
        Writer 1 is DONE writing
        Writer 2 is WRITING
        Writer 2 is DONE writing
        Writer 3 is WRITING
        Writer 3 is DONE writing
        Writer 4 is WRITING
        Writer 4 is DONE writing
        Writer 5 is WRITING
        Writer 5 is DONE writing

9. RESULT

The Readers–Writers problem was successfully implemented using semaphores.
Multiple readers were able to read concurrently while writers obtained exclusive access, satisfying the problem’s constraints.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command