Readers–Writers Problem with Writer Priority Using Semaphores

 

Readers–Writers Problem with Writer Priority Using Semaphores


1. Aim

To implement the Readers–Writers process synchronization problem using semaphores, ensuring that writers are given priority over readers while accessing a shared resource.


2. Objectives

  • To study process synchronization using semaphores.

  • To understand writer priority in the Readers–Writers problem.

  • To prevent writer starvation while allowing concurrent readers.

  • To implement synchronization using POSIX threads and semaphores.


3. Problem Description

In a shared system:

  • Readers only read shared data.

  • Writers modify shared data.

  • Multiple readers may read simultaneously.

  • Writers require exclusive access.

  • If a writer is waiting, new readers must wait (writer priority).


4. Theory

Process Synchronization

Process synchronization ensures that multiple processes/threads operate on shared resources without conflicts, maintaining data consistency.

Readers–Writers Problem

This is a classical synchronization problem where:

  • Readers can share access to the resource.

  • Writers must access the resource exclusively.

  • Priority policies decide whether readers or writers get preference.

Writer Priority

In the writer-priority solution:

  • Writers are allowed to proceed as soon as the resource becomes free.

  • Incoming readers must wait if any writer is waiting.

  • This prevents writer starvation.


5. Synchronization Tools Used

Semaphores

Semaphores are synchronization variables supporting two atomic operations:

  • wait() (P operation)

  • signal() (V operation)


6. Semaphores and Shared Variables

NameTypePurpose
mutexBinaryProtects the reader count
rw_mutexBinaryControls access to the shared resource
queueBinaryEnforces writer priority
read_countIntegerNumber of active readers

7. Algorithm

Reader Algorithm (Writer Priority)

  1. Wait on queue

  2. Wait on mutex

  3. Increment read_count

  4. If read_count == 1, wait on rw_mutex

  5. Signal mutex

  6. Signal queue

  7. Read the shared data

  8. Wait on mutex

  9. Decrement read_count

  10. If read_count == 0, signal rw_mutex

  11. Signal mutex


Writer Algorithm (Writer Priority)

  1. Wait on queue

  2. Wait on rw_mutex

  3. Write to the shared data

  4. Signal rw_mutex

  5. Signal queue


8. C Program Implementation

(POSIX Threads + Semaphores)

#include <stdio.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> sem_t mutex; // Protects read_count sem_t rw_mutex; // Exclusive access to shared resource sem_t queue; // Enforces writer priority int read_count = 0; void *reader(void *arg) { int id = *(int *)arg; sem_wait(&queue); // Block if writer waiting sem_wait(&mutex); read_count++; if (read_count == 1) sem_wait(&rw_mutex); // First reader blocks writers sem_post(&mutex); sem_post(&queue); // Allow others to queue // Critical Section (Reading) printf("Reader %d is READING\n", id); sleep(1); printf("Reader %d has FINISHED READING\n", id); sem_wait(&mutex); read_count--; if (read_count == 0) sem_post(&rw_mutex); // Last reader releases writer sem_post(&mutex); return NULL; } void *writer(void *arg) { int id = *(int *)arg; sem_wait(&queue); // Writer gets priority sem_wait(&rw_mutex); // Exclusive access // Critical Section (Writing) printf("\tWriter %d is WRITING\n", id); sleep(2); printf("\tWriter %d has FINISHED WRITING\n", id); sem_post(&rw_mutex); sem_post(&queue); return NULL; } int main() { pthread_t readers[5], writers[5]; int id[5]; sem_init(&mutex, 0, 1); sem_init(&rw_mutex, 0, 1); sem_init(&queue, 0, 1); for (int i = 0; i < 5; i++) { id[i] = i + 1; pthread_create(&readers[i], NULL, reader, &id[i]); pthread_create(&writers[i], NULL, writer, &id[i]); } for (int i = 0; i < 5; i++) { pthread_join(readers[i], NULL); pthread_join(writers[i], NULL); } sem_destroy(&mutex); sem_destroy(&rw_mutex); sem_destroy(&queue); return 0; }

Compilation

gcc rw_writer_priority.c -lpthread

9. Sample Output

binuvp@debian-workstation:~$ ./a.out Reader 1 is READING Reader 1 has FINISHED READING Writer 1 is WRITING Writer 1 has FINISHED WRITING Writer 2 is WRITING Writer 2 has FINISHED WRITING Reader 2 is READING Reader 3 is READING Reader 4 is READING Reader 2 has FINISHED READING Reader 3 has FINISHED READING Reader 4 has FINISHED READING Writer 3 is WRITING Writer 3 has FINISHED WRITING Writer 4 is WRITING Writer 4 has FINISHED WRITING Reader 5 is READING Reader 5 has FINISHED READING Writer 5 is WRITING Writer 5 has FINISHED WRITING

10. Observations

  • Multiple readers read simultaneously.

  • Writers write one at a time.

  • When a writer is waiting, no new reader enters.

  • Writer starvation is avoided.


11. Result

The Readers–Writers problem with writer priority was successfully implemented using semaphores.
The program ensures proper synchronization, exclusive writing, and fair writer access.

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