Please enable JavaScript.
Coggle requires JavaScript to display documents.
6.5 Classical Problems of Synchronization (Uses of Semaphores (To protect…
6.5 Classical Problems of Synchronization
Uses of Semaphores
To protect access to a critical section
The daabase in the R/Ws problem
As a
mutex
lock
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
Real-World Application
Producer-Consumer
Web Server Architecture
Streaming Audio or Video
Readers-Writers
Shared File on Disk
Database Application
Dining Philosophers
Printer
Online Course Scheduling
3 CLASSIC PROBLEM
1. BOUNDED BUFFER PROBLEM
a.k.a the
Producer-Consumer Problem
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
Solution:
Creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.
3 types of semaphores:-
mutex
- to provide mutual exclusion for access to the buffer.
full
- To count the numbers of slots in the buffer that are full.
empty
- To count the number of slots in the buffer that are empty.
2. THE READERS WRITERS PROBLEM
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.
Solution:
Readers Priority:
no reader kept waiting unless a writer has already obtained permission to access the database.
Writers' Priority:
if a writer is waiting to access the database , no new readers can start reading.
The solution above may cause
starvation
. Semaphore solutions to cure it. :-
reader-writer locks
3. DINING PHILOSOPHERS PROBLEM
Problem:
a large class or concurrency control problem; the need to allocate several resources among several processess in a
deadlock-free
and
starvation-free
manner.
Solution:
no two neighbouring Philosophers eat simultaneously, but has possibility of creating a deadlock
each philosophers invokes the operation
pickup()
&
putdown()
, no deadlock but starvation is possible