OS Session3 U2
OS Session3 U2
Dr P S Patheja
• It is Time-Consuming in multiprocessor as No messages
can be passed among processes therefore delay and
decrease efficiency.
Mutex Locks
• Previous solutions are complicated and generally
inaccessible to application programmers.
• OS designers build software tools to solve critical
section problem.
• Simplest is mutex lock.
• Protect a critical section by first acquire() a lock then
release() the lock:
– Boolean variable indicating if lock is available or not.
• Calls to acquire() and release() must be atomic:
Dr P S Patheja
– Usually implemented via hardware atomic instructions.
• But this solution requires busy waiting:
– This lock therefore called a spinlock.
Classic Example
Dr P S Patheja
Rs.1000.
Rs100
Classic Example Contd….
Dr P S Patheja
Dr P S Patheja
A lock is a synchronization mechanism for
enforcing limits on access to a resource in an
environment where there are many threads/
processes of execution.
Dr P S Patheja
Dr P S Patheja
Dr P S Patheja
TestAndSet Synchronization Hardware
• Test and set (modify) the content of a word atomically (a
Boolean version):
function TestAndSet(var target:boolean) : boolean;
{
TestAndSet = target;
target = TRUE; }
1. This is Executed atomically (Uninterrupted).
2. Set the new value of passed parameter to “TRUE”. Initial value
of Lock = FALSE.
Dr P S Patheja
• The Boolean function represents the essence of the
corresponding machine instruction i.e. If 2 TestAndSet
instructions are executed simultaneously (each on
different CPU) they will be executed sequentially in
some arbitrary order.
Mutual Exclusion with TestAndSet
• The structure of Pi is shown as:
boolean lock = FALSE;
Process Pi
do { //Only first Pi who sets
// lock enters CS
while (TestAndSet(&lock)) do no-op;
Critical Section
lock = FALSE;
Dr P S Patheja
Remainder Section
} while (TRUE);
Problems with Semaphores
• Incorrect use of semaphore operations:
Dr P S Patheja
Monitors
• A high-level data abstraction (shulter to data) that provides a
convenient and effective mechanism or process synchronization
to control nature of operation performed on Global data.
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
procedure Pn (…) {……}
Dr P S Patheja
…
}
Monitor construct ensures that only One process at a time can be
active within the monitor.
Condition Variables
• Local variable of Monitor can be accessed by only
local procedures.
• Monitor do not have any built-in Synchronization
mechanism therefore they take help of tools like
Semaphores.
• condition x, y;
Two operations on a condition variable:
– x.wait () – a process that invokes the operation is
Dr P S Patheja
suspended.
– x.signal () – resumes one of processes (if any) that
invoked x.wait ()
• Monitor make critical data accessible Indirectly
and Exclusively via Publicly available Procedures.
• In Producer/Consumer Problem, the shared
Global Buffer can be declared as belonging to
Monitor and Prod./Cons. Do not have access to
it.
• A producer will call its function through Monitor
and items are passed in Arguments, which
Monitor will append in Buffer.
• Monitor thus handle Buffer Management and
Dr P S Patheja
Process Synchronization AND Code/Variables are
hidden by the user.
Solution to Dining Philosophers
monitor DP
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self [i].wait;
}
void putdown (int i) {
Dr P S Patheja
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}
Solution to Dining Philosophers (cont)
initialization_code() {
Dr P S Patheja
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Solution to Dining Philosophers (cont)
dp.pickup (i)
EAT
dp.putdown (i)
Dr P S Patheja