Operating Sytems: B.Tech Ii Yr (Term 08-09) Unit 3 PPT Slides Text Books
Operating Sytems: B.Tech Ii Yr (Term 08-09) Unit 3 PPT Slides Text Books
Operating Sytems: B.Tech Ii Yr (Term 08-09) Unit 3 PPT Slides Text Books
e
e
in
g
n
E
O
o
D
a
a
F
No. of slides: 42
INDEX
UNIT 3 PPT SLIDES
S.NO.
1.
2.
3.
4.
5.
6.
7.
8.
TOPIC
Process synchronization
critical- section prob,
Petersons Solution
synchronization Hardware
Semaphores
Monitors
atomic transactions
Case study
REVISION
L17
L18
g
n
e
e
in
E
O
a
a
F
o
D
c
.
rs
L19
L20
L21
L22
L23
m
o
L17.1 to L17.4
L18.1 to L18.3
L19.1 to L19.7
L20.1 to L20.11
L21.1 to L21.4
L22.1 to L22.8
L23.1 to L23.3
Process synchronization
Concurrent access to shared data may result in
data inconsistency
Maintaining data consistency requires mechanisms
to ensure the orderly execution of cooperating
processes
Suppose that we wanted to provide a solution to the
consumer-producer problem that fills all the buffers.
We can do so by having an integer count that keeps
track of the number of full buffers. Initially, count is
set to 0. It is incremented by the producer after it
produces a new buffer and is decremented by the
consumer after it consumes a buffer.
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Producer
while (true) {
m
o
c
/* produce an item and put.in
s
r
nextProduced */
e
e
n
i
while (count == BUFFER_SIZE)
g
n
E
; // do O
nothing
o
buffer
[in]
=
nextProduced;
D
a
a
F in = (in + 1) % BUFFER_SIZE;
count++;
}
Consumer
m
o
while (true) {
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D/* consume the item in
nextConsumed
}
Race Condition
count++ could be implemented as
register1 = count
register1 = register1 + 1
count = register1
count-- could be implemented as
m
o
e
e
in
c
.
rs
register2 = count
register2 = register2 - 1
count = register2
Consider this execution interleaving with count = 5 initially:
S0: producer execute register1 = count {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = count {register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
S4: producer execute count = register1 {count = 6 }
S5: consumer execute count = register2 {count = 4}
g
n
E
O
a
a
F
o
D
Solution to Critical-Section
Problem
1.Mutual Exclusion - If process Pi is executing in its critical section,
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Petersons Solution
Two process solution
Assume that the LOAD and STORE
m
instructions are atomic; that is, cannot
be
o
c
.
interrupted.
s
r
e variables:
The two processes share e
two
int turn;
Boolean flag[2]
n
i
g
n
E
O
o
The variable
turn indicates whose turn it is to
D
acritical section.
enter the
a
F
m
do {
o
c
.
flag[i] = TRUE;
s
r
e
e
turn = j;
n
i
g
while (flag[j]n&& turn == j);
E
O
critical
section
o
D
a
flag[i] = FALSE;
a
F
remainder section
} while (TRUE);
Synchronization Hardware
Many systems provide hardware support for
critical section code
Uniprocessors could disable interrupts
m
o
c
.
s
r
e
e
in
g
n
E
O
o
Modern machines
provide special atomic
D
hardware
aainstructions
F
Atomic = non-interruptable
TestAndndSet Instruction
Definition:
m
o
c
.
s
boolean TestAndSet (boolean
*target)
r
e
e
{
n
i
g
boolean rv =n*target;
E
*targeto=OTRUE;
D rv:
return
a
a
}F
m
o
c
.
rs
do {
e
e
in
g
n
E
O
critical section
o
D
a
a
F
lock = FALSE;
//
} while (TRUE);
remainder section
Swap Instruction
Definition:
c
.
rs
e
e
n
i
void Swap (boolean *a, boolean
*b)
g
n
{
E
O
boolean
temp
= *a;
o
*aa=D
*b;
a
F *b = temp:
}
m
o
c
.
rs
e
e
in
//
g
n
E
O
o
D
critical section
a
a
F
lock = FALSE;
//
} while (TRUE);
m
o
remainder section
e
e
in
c
.
rs
g
n
E
O
a
a
F
o
D
m
o
Semaphore
Synchronization tool that does not require busy waiting
Semaphore S integer variable
Two standard operations modify S: wait() and signal()
Originally called P() and V()
Less complicated
Can only be accessed via two indivisible (atomic)
operations
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Semaphore Implementation
Must guarantee that no two processes can execute wait () and
signal () on the same semaphore at the same time
Thus, implementation becomes the critical section problem
where the wait and signal code are placed in the crtical section.
Could now have busy waiting in critical section
implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections
and therefore this is not a good solution.
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
c
.
rs
e
e
in
g
n
E
O
Two operations:
o
D the process invoking the operation
block a
place
a
F appropriate waiting queue.
on the
wakeup remove one of processes in the
waiting queue and place it in the ready queue.
m
o
c
.
rs
e
e
in
g
n
E
O
o
D
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
a
a
F
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Bounded-Buffer Problem
n
i
g
n
E
O
o
D
a
a
c
.
rs
m
o
e
e
in
g
n
E
O
a
a
F
o
D
Readers-Writers Problem
A data set is shared among a number of concurrent processes
Readers only read the data set; they do not perform any
updates
Writers can both read and write
m
o
c
.
Problem allow multiple readers to readratsthe same time.
e data at the same
Only one single writer can access the
shared
e
n
i
time
g
n
Shared Data
E
O
Data set
o
D initialized to 1
Semaphoreamutex
a
Semaphore
F wrt initialized to 1
Integer readcount initialized to 0
Readers-Writers Problem
The structure of a writer process
do {
wait (wrt) ; // writing is performed
signal (wrt) ;
} while (TRUE);
The structure of a reader process
do {
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ;
signal (mutex) //reading is performed
readcount - - ;
if (readcount == 0)
signal (wrt) ;
signal (mutex) ;
} while (TRUE);
m
o
c
.
rs
e
e
in
g
n
E
O
aa
o
D
wait (mutex) ;
Dining-Philosophers Problem
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
do {
e
e
in
c
.
rs
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
E
O
g
n
o
signal (D
chopstick[i] );
a(chopstick[ (i + 1) % 5] );
signal
a
F
// think
} while (TRUE);
m
o
Monitors
A high-level abstraction that provides a convenient
and effective mechanism for process synchronization
Only one process may be active within the monitor at
a time
m
o
monitor monitor-name
{
// shared variable declarations
procedure P1 () { . }
g
n
e
e
in
E
O
o
D
a
a
F
procedure Pn () {}
Initialization code ( .) { }
c
.
rs
e
e
in
c
.
rs
g
n
E
O
a
a
F
o
D
m
o
wait(mutex);
body of F;
if (next_count > 0)
signal(next)
else
signal(mutex);
m
o
c
.
rs
e
e
in
g
n
E
O
o
D
a
a
F
Monitor Implementation
For each condition variable x, we have:
m
o
c
.
rs
e
e
in
g
n
E
O
x-count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem);
x-count--;
a
a
F
o
D
Atomic Transactions
m
System Model
o
c
.
Log-based Recovery
s
r
e
e
Checkpoints
n
i
g
n
Concurrent Atomic
E Transactions
O
o
D
a
a
System Model
Assures that operations happen as a single logical unit of
work, in its entirety, or not at all
Related to field of database systems
Challenge is assuring atomicity despite computer system
failures
Transaction - collection of instructions or operations that
performs single logical function
Here we are concerned with changes to stable storage
disk
Transaction is series of read and write operations
Terminated by commit (transaction successful) or abort
(transaction failed) operation
Aborted transaction must be rolled back to undo any
changes it performed
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Log-Based Recovery
Record to stable storage information about all modifications
by a transaction
Most common is write-ahead logging
Log on stable storage, each log record describes single
transaction write operation, including
Transaction name
Data item name
Old value
New value
<Ti starts> written to log when transaction Ti starts
<Ti commits> written when Ti commits
Log entry must reach stable storage before operation on data
occur
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Checkpoints
Log could become long, and recovery could take long
Checkpoints shorten log and recovery time.
Checkpoint scheme:
1. Output all log records currently in volatile storage to stable
storage
2. Output all modified data from volatile to stable storage
3. Output a log record <checkpoint> to the log on stable
storage
Now recovery only includes Ti, such that Ti started executing
before the most recent checkpoint, and all transactions after
Ti All other transactions already on stable storage
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Concurrent Transactions
Must be equivalent to serial execution
m
serializability
o
c
.
s
Could perform all transactions
in critical
r
e
e
section
n
i
g
Inefficient, too restrictive
n
E
O
Concurrency-control
algorithms provide
o
D
serializability
a
a
F
Serializability
Locking Protocol
Ensure serializability by associating lock with
each data item
m
o
c
.
rs
Locks
e
e
in
g
n
E
O
o
D
a
a
Require
Fevery transaction on item Q acquire
appropriate lock
If lock already held, new request may have to
wait
n
i
Growing obtainingg
locks
n
E
Shrinking releasing
locks
O
o deadlock
Does not D
prevent
a
a
F
Solaris Synchronization
Implements a variety of locks to support
multitasking, multithreading (including realtime threads), and multiprocessing om
c
.
s
Uses adaptive mutexes for efficiency
when
r
e
e
protecting data from shortncode segments
i
g
Uses condition variables
and readers-writers
n
E
locks when longer
sections of code need
O
o
D
access to data
a
a
F
Uses turnstiles
to order the list of threads
waiting to acquire either an adaptive mutex or
reader-writer lock
Windows XP Synchronization
Uses interrupt masks to protect access
to
m
o
c
global resources on uniprocessor
systems
.
s
r
e
Uses spinlocks on multiprocessor
systems
e
n
i
Also provides dispatcher
objects
which
g
n
E
may act as either
mutexes and
O
o
semaphores
D
a
a
Dispatcher
objects may also provide
F
events
An event acts much like a condition variable
Linux Synchronization
m
o
Linux:
c
.
rs
e
e
in
g
n
E
O
o
Linux provides:
D
a
a
semaphores
F
spin locks