Database
Process 1
Process 2
Process 3
Process 4
Concurrent processes(switching can
happen between processes)
Database
Process 1
Process 2
Process 3
Process 4
When chaos doesn’t occur?
Database
Process 1
Process 2
Process 3
Process 4
When chaos occur?
Reader writer problem or classic problem of synchronization
When process access the same data structure this may lead to race condition
(lead to undesired result)
One process should access the data at a time (means process need to be
synchronised-controlling the order of execution)
The critical section is the access to the shared data
Entry section
critical section
Exit section
Remainder section
Here the writers should have exclusive access to the database
Every process synchronisation problem should satisfy the three conditions
 Mutual exclusion –writer/writer or writer/reader
 Bounded waiting-either readers have to wait until a certain number of writers
completes its writing or writer have to wait until a certain number of readers
finish reading
 Progress-the processes which are not executing it’s remainder section has to
be considered first for critical section entry among the requests
Hardware
instructions
Synchronisation
tools
Algorithmic
approach
Semaphores Monitors
PREDEFINED STRUCTURE
struct semaphore{
int value;
struct process *queue;
}
FUNCTIONS ASSOCIATED WITH THIS
wait(semaphore *s)
{
s->value - -
if(s->value<0)
{
add process to queue
}
}
signal(semaphore *s)
{
s->value++
if(s->value<=0)
{
remove the process
from queue
}
}
MONITORS
Abstract data
type
Local variables Local functions
Accessed by
Condition variables
Through which a process
can made to wait or
proceed with the critical
section
For each condition
variable a separate queue
is maintained
Waiting Queue
maintained for
individual condition
variables
Entry queue
Wait
Signal
Simply put the process into waiting
queue of condition variable
Moves the process from waiting queue
of the condition variable to the entry
queue of the monitor
Mutual exclusion within
monitor
R/W
Readers
priority
Writers
priority
VS
Reader enter into critical section writer waits until reading
process is completed
R
R
R
W
Mutual exclusion should be established:
Between reader and writer/writer and
writer to access db
Among the readers to maintain
reader count(shared variable)
Here n readers can access the file simultaneously
VS
Reader waits writer enters into critical
section
Here n writers can access the file in sequential order.
Mutual exclusion should be established:
Between reader and writer to access the database
Among writers to maintain the writers count(shared
variable)
Among writers to access the database
1
2
3
READER’S
PRIORITY
USING
SEMAPHORES
SEMAPHORE VARIABLE INITIALISATION
semaphore mutex_update_count=1
semaphore mutex_rw=1
count=0//a variable to keep track of the no of
readers
The structure of reader process
wait(mutex_update_count)
count++
if(count==1)
wait(mutex_rw)
signal(mutex_update_count)
//reading process
wait(mutex_update_count)
count - -
if(count==0)
signal(mutex_rw)
signal(mutex_update_count)
The structure of writer process
wait(mutex_rw)
//writing process
signal (mutex_rw)
Using semaphores
SEMAPHORE VARIABLE INITIALISATION
semaphore mutex_rw=1
semaphore mutex_write=1
semaphore
mutex_update_count=1
count=0
//the structure of writer process
wait(mutex_update_count)
count++
if(count==1)
wait(mutex_rw)
signal(mutex_update_count)
wait(mutex_write)
//writing process
signal(mutex_write)
wait(mutex_update_count)
count - -
if(count==0)
signal(mutex_rw)
signal(mutex_update_count)
The structure of reader process
wait(mutex_rw)
//reading process
signal (mutex_rw)
READER’S
PRIORITY
USING
MONITORS
//Monitor declaration
monitor reader_writer
//local variables
boolean busy=false;
int readcount=0
//condition variables
oktoread
oktowrite
//local functions
void startread( )
{
readcount++
if(busy==true)
{
oktoread.wait( )
}
}
void endread( )
{
readcount - -
if(readcount==0)
oktowrite.signal( )
}
void startwrite( )
{
if(busy==true or readcount!=0)
oktowrite.wait( )
busy=true
}
void endwrite( )
{
busy=false
if(readcount!=0)
oktoread.signal( )
else
oktowrite.signal( )
}
//Invoking method
reader_writer r;
reader( )
{
r.startread( )
//reading process
r.endread( )
}
writer( )
{
r.startwrite( )
//writing process
r.endwrite( )
}
Using monitors
//Monitor declaration
monitor reader_writer
//local variables
boolean busywrite=false;
int writecount=0
int readcount=0
//condition variables
oktoread
oktowrite
//local functions
void startread( )
{
if(busywrite==true|| writecount!=0)
{
oktoread.wait( )
}
readcount++
}
void endread( )
{
readcount- -
if(writecount!=0 and readcount==0)
{
oktowrite.signal( )
}
}
void startwrite( )
{
writecount++
if(busy==true)
oktowrite.wait( )
busy=true
}
void endwrite( )
{
busy=false
writecount - -
if(writecount==0)
oktoread.signal( )
else
oktowrite.signal()
}

Reader Writer problem

  • 2.
    Database Process 1 Process 2 Process3 Process 4 Concurrent processes(switching can happen between processes)
  • 3.
    Database Process 1 Process 2 Process3 Process 4 When chaos doesn’t occur?
  • 4.
    Database Process 1 Process 2 Process3 Process 4 When chaos occur? Reader writer problem or classic problem of synchronization
  • 5.
    When process accessthe same data structure this may lead to race condition (lead to undesired result) One process should access the data at a time (means process need to be synchronised-controlling the order of execution) The critical section is the access to the shared data Entry section critical section Exit section Remainder section Here the writers should have exclusive access to the database Every process synchronisation problem should satisfy the three conditions  Mutual exclusion –writer/writer or writer/reader  Bounded waiting-either readers have to wait until a certain number of writers completes its writing or writer have to wait until a certain number of readers finish reading  Progress-the processes which are not executing it’s remainder section has to be considered first for critical section entry among the requests
  • 6.
  • 7.
    PREDEFINED STRUCTURE struct semaphore{ intvalue; struct process *queue; } FUNCTIONS ASSOCIATED WITH THIS wait(semaphore *s) { s->value - - if(s->value<0) { add process to queue } } signal(semaphore *s) { s->value++ if(s->value<=0) { remove the process from queue } }
  • 8.
    MONITORS Abstract data type Local variablesLocal functions Accessed by Condition variables Through which a process can made to wait or proceed with the critical section For each condition variable a separate queue is maintained Waiting Queue maintained for individual condition variables Entry queue Wait Signal Simply put the process into waiting queue of condition variable Moves the process from waiting queue of the condition variable to the entry queue of the monitor Mutual exclusion within monitor
  • 9.
  • 10.
    VS Reader enter intocritical section writer waits until reading process is completed
  • 11.
    R R R W Mutual exclusion shouldbe established: Between reader and writer/writer and writer to access db Among the readers to maintain reader count(shared variable) Here n readers can access the file simultaneously
  • 12.
    VS Reader waits writerenters into critical section
  • 13.
    Here n writerscan access the file in sequential order. Mutual exclusion should be established: Between reader and writer to access the database Among writers to maintain the writers count(shared variable) Among writers to access the database 1 2 3
  • 14.
  • 15.
    SEMAPHORE VARIABLE INITIALISATION semaphoremutex_update_count=1 semaphore mutex_rw=1 count=0//a variable to keep track of the no of readers The structure of reader process wait(mutex_update_count) count++ if(count==1) wait(mutex_rw) signal(mutex_update_count) //reading process wait(mutex_update_count) count - - if(count==0) signal(mutex_rw) signal(mutex_update_count)
  • 16.
    The structure ofwriter process wait(mutex_rw) //writing process signal (mutex_rw)
  • 17.
  • 18.
    SEMAPHORE VARIABLE INITIALISATION semaphoremutex_rw=1 semaphore mutex_write=1 semaphore mutex_update_count=1 count=0 //the structure of writer process wait(mutex_update_count) count++ if(count==1) wait(mutex_rw) signal(mutex_update_count) wait(mutex_write) //writing process signal(mutex_write) wait(mutex_update_count) count - - if(count==0) signal(mutex_rw) signal(mutex_update_count)
  • 19.
    The structure ofreader process wait(mutex_rw) //reading process signal (mutex_rw)
  • 20.
  • 21.
    //Monitor declaration monitor reader_writer //localvariables boolean busy=false; int readcount=0 //condition variables oktoread oktowrite //local functions void startread( ) { readcount++ if(busy==true) { oktoread.wait( ) } }
  • 22.
    void endread( ) { readcount- - if(readcount==0) oktowrite.signal( ) } void startwrite( ) { if(busy==true or readcount!=0) oktowrite.wait( ) busy=true } void endwrite( ) { busy=false if(readcount!=0) oktoread.signal( ) else oktowrite.signal( ) }
  • 23.
    //Invoking method reader_writer r; reader() { r.startread( ) //reading process r.endread( ) } writer( ) { r.startwrite( ) //writing process r.endwrite( ) }
  • 24.
  • 25.
    //Monitor declaration monitor reader_writer //localvariables boolean busywrite=false; int writecount=0 int readcount=0 //condition variables oktoread oktowrite //local functions void startread( ) { if(busywrite==true|| writecount!=0) { oktoread.wait( ) } readcount++ }
  • 26.
    void endread( ) { readcount-- if(writecount!=0 and readcount==0) { oktowrite.signal( ) } } void startwrite( ) { writecount++ if(busy==true) oktowrite.wait( ) busy=true }
  • 27.
    void endwrite( ) { busy=false writecount- - if(writecount==0) oktoread.signal( ) else oktowrite.signal() }