Chapter 3 OS
Chapter 3 OS
CONCURRENCY CONTROL
SYLLABUS
MULTIPLE PROCESSES
Operating System design is concerned with the
management of processes and threads:
Multiprogramming - Management of multiple
processes in uniprocessor system.
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
SIMPLE EXAMPLE
In single processor multiprogramming
system=>
User jumps from one application to another.
In multiprocessor system=>
Same problem of protected shared resources arise
remainder section
//A thead may exit its execution in this
section.
GENERAL STRUCTURE OF PROCESS
Explaination
Entry Section: Process will request to enter inside
critical section. The part of code decides whether
process can enter inside the critical section or
not.
Critical Section: Code segment that accesses
shared resources and that has to be executed as
an atomic action.
Exit Section: Locking section can be undone which
is done in critical section.
Remainder Section: Remaining part of the program.
Solutions to
Critical SecTion
Peterson’s Solution
PETERSON’S SOLUTION
Peterson's solution is restricted to two processes that
alternate execution between their critical sections and
remainder sections.
The processes are numbered Po and P1.
Peterson's solution requires the two processes to share
ALGORITHM
Meets all three requirements; solves the critical-
section problem for two processes.
Process P0 Process P1
do { do {
flag [0]= T; flag [1]= T;
turn = 1; turn = 0;
while (flag [1] ==T && while (flag [0] ==T &&
turn ==1) ; turn ==0) ;
critical section critical section
flag [0] = F; flag [1] = F;
remainder section remainder section
} while (1); } while (1);
Operating System Concepts
ALGORITHM
Progress: No blocking.
Progress: No blocking.
{
S++;
} Increme
nts
Value
SEMAPHORE
Example
Suppose s=1
Process P1 came.
do{ s=1 wait(s)=> s=0
wait(s) P1 enters in CS.
// critical P2 even came sm
time=>
section
s=0
signal(s) wait(s)=>s<=0=>T
remainder keep moving in loop.
section When P1 finishes=>
s=0 signal(s)=>s=1
}while(T) Now P2 can enter.
SEMAPHORE
Mutual Exclusion- achieved.
Progress- No blocking of process.
Bounded wait – One process keep on entering
inside the CS and others won’t get chance. So
this is not achieved.
Semaphore is a good solution as it fulfils two
important criteria's.
SEMAPHORE AS GENERAL
SYNCHRONIZATION TOOL
Counting semaphore – integer value can range over an
unrestricted domain.
Two operations:
block – place the process invoking the operation on the
52
MONITORS
A high-level abstraction that provides a convenient and effective
mechanism for process synchronization
Abstract data type, internal variables only accessible by code within
the procedure
Only one process may be active within the monitor at a time
But not powerful enough to model some synchronization schemes
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
Bounded-Buffer Problem
Produce Consum
r er
Three issues=>
1) Producer has to make sure that at least one cell is empty.
2) Consumer has to make sure that at least one cell is filled.
3) At a time both can’t work with buffer.
BOUNDED-BUFFER PROBLEM
Three semaphores are taken:
Semaphore mutex initialized to the value 1
updates
Writers – can both read and write
Problem – allow multiple readers to read at the same time. Only
one single writer can access the shared data at the same time.
Shared Data
Data set
Semaphore mutex initialized to 1 (controls access to readcount)
Semaphore wrt initialized to 1 (writer access)
Integer readcount initialized to 0 (how many processes are
reading object)
READERS-WRITERS PROBLEM (CONT.)
The structure of a writer Initially wrt= 1
process
wrt = 1=>0
do {
Write opn is
wait (wrt) ;
performed.
// writing is performed
wrt=0=>1
signal (wrt) ;
} while (TRUE);
READERS-WRITERS PROBLEM (CONT.)
Reader=>
If reader1 is inside the CS and reader2
came, it is allowed.
If reader1 is inside the CS and writer came,
it is not allowed.
mutex=1 synchronized between different
readers.
READERS-WRITERS PROBLEM (CONT.)
The structure of a reader process
do { First reader comes=>
wait (mutex) ; mutex = 1=>0
readcount ++ ;
if (readcount == 1) rc=0=>1
wait (wrt) ;
signal (mutex)
rc=1=>wrt=1=>0
// reading is performed Writer cant access CS.
wait (mutex) ;
readcount - - ; mutex = 1
if (readcount == 0) As mutex = 1 other
signal (wrt) ;
signal (mutex) ; reader can come and
} while (TRUE); enter for read opn.
rc = 2=>two readers
reading at the same
time.
READERS-WRITERS PROBLEM (CONT.)
The structure of a reader process
do { Readers done with
wait (mutex) ;
readcount ++ ;
reading
if (readcount == 1)
wait (wrt) ;
mutex=1=>0
signal (mutex) rc =rc-1
// reading is performed
wait (mutex) ; If rc=0 then allow writer
readcount - - ;
if (readcount == 0) to enter CS by
signal (wrt) ; incrementing wrt to 1.
signal (mutex) ;
} while (TRUE); mutex=1