0% found this document useful (0 votes)
19 views18 pages

OS Session3 U2

Uploaded by

1005thakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views18 pages

OS Session3 U2

Uploaded by

1005thakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Synchronization Hardware

• Many systems provide hardware support for critical


section code
• In Uniprocessors we could disable interrupts (in
threads) when shared variables are modified.
– i.e No preemption of CPU therefore No unexpected
modifications by other processes.
• Drawback of above :
– Clock can not be Interrupted
– Generally too inefficient on multiprocessor systems

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:

– signal (mutex) …. wait (mutex)

– wait (mutex) … wait (mutex)

– Omitting of wait (mutex) or signal (mutex) (or


both)

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 (…) {……}

Initialization code ( ….) { … }

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)

void test (int i) {


if ( (state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}

initialization_code() {

Dr P S Patheja
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Solution to Dining Philosophers (cont)

• Each philosopher I invokes the operations


pickup()
and putdown() in the following sequence:

dp.pickup (i)

EAT

dp.putdown (i)

Dr P S Patheja

You might also like