Title: Classical Resource Sharing Problems
1 Classical Resource Sharing Problems
- Producer / Consumer Bounded Buffer Problem .
- Readers Writers Problem.
- Message Passing Problem.
- Dining Philosophers Problem More relevant for
Deadlock Prevention . - Barber Shop Problem.
2Producer / Consumer Bounded Buffer Problem
Basic Features
- There exists a Group of Producers The Input
Processes , Enqueuer etc. and a Group of
Consumers The Data Receptor Processes ,
Dequeuer etc. - There exists a finite sized / bounded buffer ( A
Queue of Items implemented by a Circular Array
AN where - Next Index ? Current Index MOD N 1) ,
which is being shared among ALL the producers
as well as ALL the consumers. - Each of the Producers produces items , one at a
time, at its own speed. - After producing an item each of the Producers
puts that item ( ENQUEUES an Item ) in the shared
finite sized buffer provided there is some empty
space available in the shared finite sized buffer
. - Each of the consumer consumes (DEQUEUES an Item)
from the Circular Array of produced items
Element Queue , provided there is some item
left in the Queue, one at a time, at its own
speed.
3The Actual Scenario
- Queue implemented by a Circular Array
Item being Consumed
INPUT Index
OUTPUT Index
Produced Items
Consumers
Bounded (Circular) Buffer
Producers
4The Shared Bounded Buffer
- 1. An array of N items.
- 2. The following Possible Status
- a) Some Empty Space available
- ? A new Item can be put in by a
Producer. - b) No Empty Space available ( Full )
- ? No New Item can be put in by any
Producer. - c) Empty ? No Item exists to Consume
by any Consumer. - 3. Circularity
- Before ENQUEING In_Ptr ? In_Ptr MOD N
1 - After DEQUEING Out_Ptr ? Out_Ptr MOD N
1 -
-
5Bounded Buffer Access by the Producers The
Access Protocol
- 1) No new Item can be Inserted in the
Shared Bounded Buffer / Queue by any Producer if
there is No Empty Place already created in it by
some consumer. - 2) Putting an Item in the shared Bounded
Buffer / Queue by any Producer should be done via
a dedicated Input Index shared among the
Producers only. - 3) Exclusive access of the Input Index
by any Producer while putting in an item /
Enqueuing an Item is mandatory - 4) Before Enqueuing an Item in the Shared
Buffer , any Producer MUST - i) WAIT for an Empty Space in the
Shared Buffer . - AFTER GETTING an EMPTY Space
-
- 1. Lock the INPUT Index (IN ) from
ALL other Producers - 2. Update the INPUT Index IN ?
IN MOD N 1 - 3. BUFFER IN ? Produced Item.
- 4. Release the Input Index IN
- 5. Signal to the Consumers about
the Filled Item. -
6Bounded Buffer Access by the Consumers The
Access Protocol
- 1) No Item can be taken from the Shared
Buffer by any Consumer unless some Item had
already been Inserted by some producer. - 2) Taking out an Item from the shared
Bounded Buffer by any Consumer should be done via
a dedicated Output Index shared among the
Consumers only. - 3) Exclusive access of the Output Index
by any Consumer while taking out in an item /
DEqueuing an Item via its Output Index OUT is
mandatory. - 4) Before DEqueuing an Item from the
Shared Buffer any Consumer MUST - i) WAIT for an Filled Space in
the Shared Buffer . - AFTER GETTING a Space Filled with
an Item -
- 1. Lock the Output Index (OUT )
from all other Consumers. - 2. Item ? BUFFER OUT . 3.
Update OUTPUT Index i.e. OUT ? OUT MOD N 1 - 4. Release the Output Index OUT
for the other Consumers. - 5. Signal to the Producers about
the Empty Space created. -
- .
7Fairness Criteria
-
- 1) Each of the producers as well as
consumers MUST be allowed to proceed at their own
speed without being dictated by any Producer /
Consumer. - 2) No producer and /or Consumer should
be forced to wait unnecessarily . - .
8The Producer Process Outline (Inserting an Item)
- Step 1. Produce an Item.
- Step 2. If there is No Empty Place in Buffer
- THEN Wait (for Empty).
- ELSE // There is Some Empty Place
in the Buffer Prepare to Insert - If the Input Index ( IN_Ptr ) is
already in Use by some other Producer - THEN Wait ( for IN_Ptr)
- ELSE // There is Some Empty Place in
the Buffer AND the Input Index ( IN_Ptr) is Free. -
- // Grab the IN_Ptr for Exclusive
Usage. - Step 3. // Update IN_Ptr in a Bounded / Circular
Buffer. - IN_Ptr ? IN_Ptr MOD N 1
- Step 4. // Insert the Produced Item.
- Buffer IN_Ptr ? Produced Item.
9The Consumer Process Outline (Consuming an Item)
- Step 1. If there is No Item in the Buffer
- THEN Wait (for Full).
- ELSE // There is Some Item left in
the Buffer Prepare to Consume it - If the Output Index ( OUT_Ptr ) is
already in Use by some other Consumer - THEN Wait ( for OUT_Ptr)
- ELSE // There is Some Item left in
the Buffer AND the Output Index ( OUT_Ptr) is
Free. -
- // Grab the OUT_Ptr for Exclusive
Usage. - Step 2. // Consume the Left Over Item.
- Consumed Item ? Buffer OUT_Ptr.
-
- Step 3. // Update OUT_Ptr in a Bounded /
Circular Buffer. - OUT_Ptr ? OUT_Ptr MOD N 1
-
10Producer Consumer Co- Operation
-
- As soon as any producer puts an item in the
shared bounded buffer it should signal to the
group of consumers about the existence of a new
item - ( Fullness ) of the shared bounded buffer
so that some consumer can take/consume that item. - As soon as any consumer consumes an item from
the shared bounded buffer it should signal to the
group of producers about an available space - ( Emptiness) of the shared bounded
buffer so that some producer can put in an item
there.
11The Fairness ( Lifeness ) Criteria
- 1) Each of the producers as well as consumers
MUST be allowed to proceed at their own speed
without being dictated by any Producer /
Consumer. - 2) No producer and /or consumer should be
forced to wait unnecessarily.
12Bounded Buffer The Required Semaphores
- One semaphore Mutex ( Short hand Notation for
Mutual Exclusion ) for enforcing Mutual Exclusion
among the Producers while sharing the IN_Ptr as
well as among the Consumers while sharing the
Out_Ptr. - Two counting Semaphores Empty Full for
Producer Consumer Co-operation. - Producers wait on Empty while signal Full.
- Consumers wait on Full while signal Empty.
13The Declaration Section
- const N . // Buffer Size
- typedef Item_type .
- shared struct Buffer
- // begin Buffer
- Elmnt Array 0..N-1 of Item_type
- In, Out 0..N-1 // Buffer Indices
- // end Buffer
- shared B Sem_T Mutex // Binary Semaphore
- // for Mutual Exclusion among its
Indices - shared Sem_T Full, Empty // Counting
- //
Semaphores
14The Initialization Section
- Empty. Count n // Entire n Element Buffer is
empty - Full. Count 0 // No Elements in the Buffer is
filled - Mutex 1 // Allow Buffer Index
- // Manipulation
- Buffer. In Buffer .Out 0 // Initialize
Buffer Pointers
15The Producer Process
- Void Producer ( void )
- // begin Producer
-
- Item_type Item
- repeat
- Produce (Item)
- Wait (Empty) // wait till some
empty space is available on buffer - Wait (Mutex) // Wait for
availability of Buffer Input Index - // capture Buffer
- // ENQ ( BUFFER, Item)
- Buffer. In Buffer. In mod
N 1 // Update Buffer Input -
// Index - Buffer. Elmnt Buffer. In
Item // Put Element in Buffer - Signal (Mutex) // Release
buffer Input Index - Signal (Full) // To the
Consumers - forever
- // end Producer
16The Consumer Process
- Void Consumer ( void )
- // begin Consumer
- Item_type Item
- repeat
- Wait (Full) // Wait for some filled up
places in Buffer -
- Wait (Mutex) // Wait for Buffer Out
Index to be Free - // Capture Buffer
- // DEQ (Buffer, Item)
- Item Buffer .Elmnt Buffer. Out // Take
out the -
// Element - Buffer. Out Buffer. Out mod N 1
// Update Buffer Out Index - Signal (Mutex) // Release Buffer
Out Index - Signal (Empty) // To the Producer
- forever
- // end Consumer