Prerequisite – Monitors, Readers-Writers Problem
There is a shared resource which is accessed by multiple processes i.e. readers and writers. Any number of readers can read from the shared resource simultaneously, but only one writer can write to the shared resource at a time. When a writer is writing data to the resource, no other process can access the resource. A writer cannot write to the resource if there are any readers accessing the resource at that time. Similarly, a reader can not read if there is a writer accessing the resource or if there are any waiting writers.
The Reader-Writer problem using monitor can be implemented using pthreads. The POSIX threads (or pthread) libraries are a standards based thread API for C/C++. The library provides following synchronization mechanisms:
- Mutexes (pthread_mutex_t) – Mutual exclusion lock:
Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables.
- Condition Variables – (pthread_cond_t):
The condition variable mechanism allows threads to suspend execution and relinquish the processor until some condition is true.
Implementation of Reader-Writer solution using pthread library:
Execute the program using following command in your Linux system
$g++ -pthread program_name.cpp $./a.out or $g++ -pthread program_name.cpp -o object_name $./object_name
- Monitors in Process Synchronization
- Dining-Philosophers Solution Using Monitors
- Reader-Writers solution using Monitors
- Producer Consumer Problem using Semaphores | Set 1
- Dining Philosopher Problem Using Semaphores
- Sleeping Barber problem in Process Synchronization
- Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution)
- System Protection in Operating System
- C-SCAN Disk Scheduling Algorithm
- Difference between Loading and Linking
- Difference between File and Folder
- SetUID, SetGID, and Sticky Bits in Linux File Permissions
- Classical problems of Synchronization with Semaphore Solution
- SCAN (Elevator) Disk Scheduling Algorithms
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.