Professional Documents
Culture Documents
BLG305 Ders8 e
BLG305 Ders8 e
Operating systems
LESSON - 1
Week : Visa
8
One solution to this problem is to use a semaphore. The semaphores to be used here are:
m is a binary semaphore used to pick up and release the lock.
Empty is a counting semaphore whose initial value is the number of slots in the buffer
because all slots are empty at startup.
full, a counting semaphore with an initial value of 0.
At any given moment, the null value represents the number of empty slots in the buffer, and
full represents the number of filled slots in the buffer.
Limited Buffer (Producer-Consumer) Problem
PRODUCER
Limited Buffer (Producer-Consumer) Problem
PRODUCER
Looking at the above code for a manufacturer, we can see that a manufacturer waits
until there is at least one empty slot first.
It then reduces the free semaphore, because the manufacturer will add data to one of
these slots, so there will now be one less empty slot.
It then locks on the buffer so that the consumer cannot access it until the producer
completes its operation.
After performing the insert operation, the lock is released and the full value is
increased because the manufacturer has filled a slot in the buffer.
Limited Buffer (Producer-Consumer) Problem
CONSUMER
LIMITED BUFFER PROBLEM
CONSUMER
The consumer waits until there is at least one full slot in the buffer.
It then reduces the full semaphore because the number of occupied slots will decrease one by
one after the consumer completes the transaction.
After that, the consumer obtains a lock on the buffer.
Following this, the consumer completes the removal process to remove the data from one of
the occupied slots.
Then, the consumer releases the lock.
Finally, the empty semaphore 1 is incremented because the consumer has just extracted data
from a full slot, thus leaving it empty.
DİNİNG PHİLOSOPHERS
In 1965, Dijkstra posed and then solved a
synchronization problem that he called the Dining
philosophers.
The problem of feeding philosophers involves the
allocation of limited resources without being
locked into a group of processes and free from
hunger.
DİNİNG PHİLOSOPHERS
Semaphore Solution
A simple solution is to represent each bar with a
semaphore. A philosopher tries to capture a rod by
executing a wait() operation on that semaphore. It
releases chopsticks by executing the signal()
operation at the appropriate semaphores.
DİNİNG PHİLOSOPHERS
Semaphore Solution
Structure of a random philosopher i
DİNİNG PHİLOSOPHERS
Semaphore Solution
Although this solution guarantees that two
neighbors do not eat at the same time, it should
still be rejected, since it can create a deadlock.
Suppose all five philosophers were hungry at the
same time, and each of them was holding the left
chopstick. All elements of the bar will now be equal
to 0. Every time a philosopher tries to get the right
stick stick, it will be postponed forever.
DİNİNG PHİLOSOPHERS
Semaphore Solution
A few possible solutions to the crash issue are as
follows:
• Allow no more than four philosophers to sit at the
table at the same time.
• Allow a philosopher to take chopsticks only if both
sticks are present (to do this, he must pick them up
from a critical section).
• Use an asymmetrical solution—that is, an odd-
numbered philosopher takes the left bar first and then
the right bar bar, while an even-numbered philosopher
takes the right bar bar and then the left bar bar
READER-WRITER PROBLEM
The problem of food philosophers is useful for modeling
processes competing for exclusive access to a limited
number of resources, such as I/O devices. Another
famous problem is the problem of readers and writers
modeling access to a database (Courtois et al., 1971).
Consider, for example, an airline reservation system with
many competing processes that require reading and
writing. It is acceptable to have multiple processes
reading the database at the same time, but if one
transaction is updating (writing) the database, no other
process may have access to the database, not even
readers. The question is, how do you program readers
and writers?
READER-WRITER PROBLEM
Solution
In solving the initial reader-to-writer problem, reader
processes share the following data structures: