6.5 Classical Problems of Synchronization (Uses of Semaphores (To…
6.5 Classical Problems of Synchronization
Uses of Semaphores
To protect access to a critical section
The daabase in the R/Ws problem
To protect the shared variables used in solving a problem such as readerCount in the R/Ws problem above
To protect a relationship
Empty and full as used in the P/Cproblem
To support atomicity
pick up either both chopstick at the same time or neither, cannot pick up just one
To enforce an order on the interleaving of the statements in the processess to by synchronized
Thread2 printing before Thread1 in Order.java
Web Server Architecture
Streaming Audio or Video
Shared File on Disk
Online Course Scheduling
3 CLASSIC PROBLEM
1. BOUNDED BUFFER PROBLEM
A finite buffer pool is used to echange messanges between producer and consumer processess. because the buffer pool has a maximum size, this problem is often called the Bounded Buffer Problem
Creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.
3 types of semaphores:-
- to provide mutual exclusion for access to the buffer.
- To count the numbers of slots in the buffer that are full.
- To count the number of slots in the buffer that are empty.
2. THE READERS WRITERS PROBLEM
Some processess (readers) that only read the sared data, & never change it, and there are other processess(writers) who may change the data in addition to reading, or instead of reading it.
no reader kept waiting unless a writer has already obtained permission to access the database.
if a writer is waiting to access the database , no new readers can start reading.
The solution above may cause
. Semaphore solutions to cure it. :-
3. DINING PHILOSOPHERS PROBLEM
a large class or concurrency control problem; the need to allocate several resources among several processess in a
no two neighbouring Philosophers eat simultaneously, but has possibility of creating a deadlock
each philosophers invokes the operation
, no deadlock but starvation is possible