0% found this document useful (0 votes)
5 views2 pages

This Code Snippet Is An Implementation of The Reader Process in The Readers

The document describes an implementation of the Reader process in the Readers-Writers Problem using semaphores, ensuring that multiple readers can access a shared resource simultaneously while a writer requires exclusive access. It provides a code breakdown, variable explanations, and a step-by-step explanation of the reader and writer processes. The logic maintains mutual exclusion for reader count updates and allows for efficient synchronization between readers and writers.

Uploaded by

jaitiarora6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

This Code Snippet Is An Implementation of The Reader Process in The Readers

The document describes an implementation of the Reader process in the Readers-Writers Problem using semaphores, ensuring that multiple readers can access a shared resource simultaneously while a writer requires exclusive access. It provides a code breakdown, variable explanations, and a step-by-step explanation of the reader and writer processes. The logic maintains mutual exclusion for reader count updates and allows for efficient synchronization between readers and writers.

Uploaded by

jaitiarora6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

This code snippet is an implementation of the Reader process in the Readers-Writers Problem using

semaphores. This problem deals with synchronizing access to a shared resource (typically a database
or file) so that:
 Multiple readers can access the resource simultaneously.
 A writer requires exclusive access (no other reader or writer can access the resource while
writing).

🔁 Code Breakdown and Explanation


Void reader(void)
{
down(mutex); // (1)
rc = rc + 1; // (2)
if (rc == 1) down(db); // (3)
up(mutex); // (4)

// (5) Reading happens here (critical section for readers)


db;

down(mutex); // (6)
rc = rc - 1; // (7)
if (rc == 0) up(db); // (8)
up(mutex); // (9)
}

🧠 Meaning of the Variables and Functions


 mutex: A binary semaphore that protects access to the rc (reader count) variable.
 db: A semaphore that ensures exclusive access to the shared resource (database).
 rc: A shared integer variable that keeps track of the number of readers currently accessing
the shared data.
 down(sem): Wait / P operation (acquire semaphore).
 up(sem): Signal / V operation (release semaphore).

🔄 Step-by-Step Explanation
Entry Section (Before Reading):
1. down(mutex);
The reader locks the mutex to safely update the shared rc variable.
2. rc = rc + 1;
Increments the reader count.
3. if (rc == 1) down(db);
o If this is the first reader, it locks the db semaphore to block writers from entering.
o Allows multiple readers to read concurrently once the first reader locks the
database.
4. up(mutex);
Releases the mutex, allowing other readers to update rc.
Critical Section (Reading):
5. db;
Represents the actual reading operation. All readers read concurrently.
Exit Section (After Reading):
6. down(mutex);
Locks the mutex again to safely update rc.
7. rc = rc - 1;
Reader finishes and decrements the reader count.
8. if (rc == 0) up(db);
o If this was the last reader, it unlocks the db semaphore to allow a writer to proceed.
o Ensures that writers get access only when no readers are left.
9. up(mutex);
Releases the mutex, completing the reader's operation.

✅ Summary
This logic ensures:
 Multiple readers can read simultaneously.
 Writers get exclusive access, but only when no readers are reading.
 Mutual exclusion is maintained for rc updates using mutex.

Writer Process Code (Typical Form)


Here’s a typical version of the writer function in C-style pseudocode using semaphores:
c
CopyEdit
void writer(void)
{
down(db); // (1) Lock the database for exclusive access
// (2) Perform writing
...
up(db); // (3) Release the database lock
}

🧠 Explanation of Writer Code


(1) down(db);
 The writer tries to acquire the db semaphore.
 If any other writer or the first reader has locked db, this writer waits (is blocked).
 This ensures mutual exclusion, i.e., only one writer can write at a time, and no readers are
reading during writing.
(2) Perform writing
 This is the critical section for the writer.
 The writer modifies the shared data (e.g., database, file).
 Only one writer can be in this section at any moment.
(3) up(db);
 The writer releases the db semaphore, allowing:
o The next writer to write, or
o The first reader (if any) to lock the db for concurrent reading.

You might also like