SlideShare ist ein Scribd-Unternehmen logo
1 von 55
SYNCHRONIZATION
Presented by:
Anwesha Bhowmik(29)
Atanu Deb(44)
Information Technology(3rd Year)
Jalpaiguri Government Engineering College
11/24/2015 SYNCHRONIZATION 1 JGEC-IT
OBJECTIVES
• To present both software and hardware
solutions of the critical-section problem
• To introduce the critical-section problem,
whose solutions can be used to ensure the
consistency of shared data
• To introduce the concept of an atomic
transaction and describe mechanisms to
ensure atomicity
SYNCHRONIZATION 2 JGEC-IT
PROCESS SYNCHRONIZATION
• Background
• The Critical-Section Problem
• Peterson’s Solution
• Semaphores
• Classic Problems of Synchronization
• Monitors
• Synchronization Examples
• Atomic Transactions
SYNCHRONIZATION 3 JGEC-IT
BACKGROUND
• 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.
SYNCHRONIZATION 4 JGEC-IT
Producer
while (true) {
/* produce an item and put in nextProduced */
while (count == BUFFER_SIZE)
; // do nothing
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
count++;
}
SYNCHRONIZATION 5 JGEC-IT
Consumer
while (true) {
while (count == 0)
; // do nothing
/* consume the item in nextConsumed */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
}
SYNCHRONIZATION 6 JGEC-IT
Race Condition
• count++ could be implemented as (producer)
register1 = count
register1 = register1 + 1
count = register1
• count-- could be implemented as (consumer)
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}
SYNCHRONIZATION 7 JGEC-IT
Critical Section Problem
• Consider system of n processes { P0 , P1 , …, Pn-1 }
• Each process has a critical section segment of code
– Process may be changing common variables, updating table,
writing file, etc.
– When one process is in its critical section, no other may be
executing in its critical section
• Critical-section problem is to design a protocol to solve this
• Each process must ask permission to enter its critical section in
entry section, may follow critical section with exit section, the
remaining code is in its remainder section
SYNCHRONIZATION 8 JGEC-IT
Critical Section(cntd.)
SYNCHRONIZATION 9 JGEC-IT
Strict Alternation Method
Critical Section(cntd.)
SYNCHRONIZATION 10 JGEC-IT
Critical Section(cntd.)
• General structure of process Pi is
SYNCHRONIZATION 11 JGEC-IT
Solution to Critical-Section Problem
Mutual Exclusion - If process Pi is executing in its critical section, then no
other processes can be executing in their critical sections
2. Progress - If no process is executing in its critical section and there exist
some processes that wish to enter their critical section, then the
selection of the processes that will enter the critical section next cannot
be postponed indefinitely
SYNCHRONIZATION 12 JGEC-IT
Solution to Critical-Section Problem(cntd.)
SYNCHRONIZATION 13 JGEC-IT
Solution to Critical-Section Problem(cntd.)
3. Bounded Waiting - A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is
granted
 Assume that each process executes at a nonzero speed
 No assumption concerning relative speed of the N processes
SYNCHRONIZATION 14 JGEC-IT
Solution to Critical-Section Problem(cntd.)
SYNCHRONIZATION 15 JGEC-IT
Peterson’s Solution
• Two process solution
• Assume that the LOAD and STORE instructions are atomic; that is, cannot be
interrupted.
• The two processes share two variables:
– int turn;
– Boolean flag[2]
• The variable turn indicates whose turn it is to enter the critical section.
• The flag array is used to indicate if a process is ready to enter the critical section.
flag[i] = true implies that process Pi is ready!
SYNCHRONIZATION 16 JGEC-IT
Algorithm for Process Pi
do {
flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = FALSE;
remainder section
} while (TRUE);
• Provable that
1. Mutual exclusion is preserved
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
SYNCHRONIZATION 17 JGEC-IT
Peterson’s Solution(cntd.)
SYNCHRONIZATION 18 JGEC-IT
Semaphore
• Two atomic operations
• Integer variable, with initial non-negative value
– wait
– signal (not the UNIX signal() call…)
– p & v (proberen & verhogen), up and down,
etc
• wait
– wait for semaphore to become positive,
then decrement by 1
• signal
– increment semaphore by 1
SYNCHRONIZATION 19 JGEC-IT
Reason For Using Semaphore
SYNCHRONIZATION 20 JGEC-IT
Mutual exclusion using semaphores
• Use wait and signal like lock and unlock
– bracket critical sections in the same manner
• Semaphore value should never be greater than 1
– This is a binary semaphore
• Depends on correct order of calls by programmer
• All mutex problems apply (deadlock…)
SYNCHRONIZATION 21 JGEC-IT
Mutual exclusion using semaphores(cntd.)
SYNCHRONIZATION 22 JGECT-IT
Semaphore Usage
• Counting semaphore – integer value can range over an unrestricted
domain
• Binary semaphore – integer value can range only between 0 and 1
– Then equivalent to a mutex lock
• Can solve various synchronization problems
• Consider P1 and P2 that require S1 to happen before S2
P1:
S1;
signal(synch); //sync++ has added one
resource
P2:
wait(synch); //executed only when
synch is > 0
S2;
SYNCHRONIZATION 23 JGEC-IT
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 codes are placed in the critical 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
SYNCHRONIZATION 24 JGEC-IT
Semaphore Implementation with no Busy waiting
o With each semaphore there is an associated waiting queue. Each entry in
a waiting queue has two data items:
 an integer value
 a list of processes
 Two operations:
– block – place a process into a waiting queue associated with the
semaphore.
– wakeup – changes the process from the waiting state to ready state
SYNCHRONIZATION 25 JGEC-IT
Semaphore Implementation with no Busy waiting (Cont.)
• Implementation of wait:
acquire () {
value--;
if (value < 0) {
add this process to list;
block(); }
}
• Implementation of signal:
release () {
value++;
if (value <= 0) {
remove a process P from list;
wakeup(P); }
}
SYNCHRONIZATION 26 JGEC-IT
Classical Problems of Synchronization
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
SYNCHRONIZATION 27 JGEC-IT
Bounded-Buffer Problem
• N buffers, each can hold one item
• Semaphore mutex initialized to the value 1
• Semaphore full initialized to the value 0
• Semaphore empty initialized to the value N.
SYNCHRONIZATION 28 JGEC-IT
Bounded Buffer Problem (Cont.)
SYNCHRONIZATION 29 JGEC-IT
Bounded Buffer Problem (Cont.)
• The structure of the producer process
do {
// produce an item in nextp
wait (empty);
wait (mutex);
// add the item to the buffer
signal (mutex);
signal (full);
} while (TRUE);
SYNCHRONIZATION 30 JGEC-IT
Bounded Buffer Problem (Cont.)
• The structure of the consumer process
do {
wait (full);
wait (mutex);
// remove an item from buffer to
nextc
signal (mutex);
signal (empty);
// consume the item in nextc
} while (TRUE);
SYNCHRONIZATION 31 JGEC-IT
Bounded Buffer Problem (Cont.)
SYNCHRONIZATION 32 JGEC-IT
Producer-Consumer Problem
SYNCHRONIZATION 33 JGEC-IT
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
• 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)
SYNCHRONIZATION 34 JGEC-IT
Readers-Writers Problem (Cont.)
• The structure of a writer process
do {
wait (wrt) ;
// writing is performed
signal (wrt) ;
} while (TRUE);
SYNCHRONIZATION 35 JGEC-IT
Readers-Writers Problem (Cont.)
• The structure of a reader process
do {
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0)
signal (wrt) ;
signal (mutex) ;
} while (TRUE);
SYNCHRONIZATION 36 JGEC-IT
The Readers-Writers Problem(cntd.)
SYNCHRONIZATION 37 JGEC-IT
Readers-Writers Problem (Cont.)
SYNCHRONIZATION 38 JGECT-IT
Deadlock and Starvation
• Deadlock – two or more processes are waiting indefinitely
for an event that can be caused by only one of the waiting
processes
• Let S and Q be two semaphores initialized to 1
P0 P1
S.acquire(); Q.acquire();
Q.acquire(); S.acquire();
. .
. .
. .
S.release(); Q.release();
Q.release(); S.release();
SYNCHRONIZATION 39 JGEC-IT
Deadlock and Starvation(cntd.)
• P0 executes S.acquire() and then P1 executes
Q.acquire().
• When P0 executes Q.acquire(), it must wait until
P1 executes Q.release().
• Similarly, when P1 execute S.acquire(), it must
wait until P0 executes S.release()
• Since these signal operations cannot be executed,
P0 and P1 are deadlocked.
SYNCHRONIZATION 40 JGEC-IT
Deadlock and Starvation(cntd.)
SYNCHRONIZATION 41 JGEC-IT
Deadlock and Starvation(cntd.)
SYNCHRONIZATION 42 JGEC-IT
Deadlock and Starvation(cntd.)
• Starvation – indefinite blocking :
• A process may never be removed from the semaphore queue in which it is
suspended.
SYNCHRONIZATION 43 JGEC-IT
Dining-Philosophers Problem
SYNCHRONIZATION 44 JGEC-IT
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
Dining-Philosophers Problem (Cont.)
• The structure of Philosopher i:
do {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with the above?
SYNCHRONIZATION 45 JGEC-IT
Dining-Philosophers Problem (Cont.)
SYNCHRONIZATION 46 JGEC-IT
Picked upWaiting
A deadlock occurs!
If all five philosophers grabs her left chopsticks simultaneously.
The Structure of Philosopher i
 Philosopher i
while ( true ) {
// get left chopstick
chopStick[i].acquire();
// get right chopstick
chopStick[(i + 1) % 5].acquire();
eating();
//return left chopstick
chopStick[i].release( );
// return right chopstick
chopStick[(i + 1) % 5].release( );
thinking();
}
SYNCHRONIZATION 47 JGEC-IT
Dining Philosophers Problem(cntd.)
SYNCHRONIZATION 48 JGEC-IT
Possible Remedies
• Placing restrictions on the philosophers
– Allow at most four philosophers to be sitting
simultaneously at the table
– Allow a philosopher to pick up her chopsticks only
if both are available
– An odd philosopher picks up first her left
chopsticks and even philosopher picks up her right
chopstick and then right
SYNCHRONIZATION 49 JGEC-IT
Dining Philosophers Problem(cntd.)
• Simultaneous semaphore :
PHILOSOPHER CHOPSTICK
0 0,1
1 1,2
2 2,3
3 3,4
4 4,0
SYNCHRONIZATION 50 JGEC-IT
Monitors
SYNCHRONIZATION 51 JGEC-IT
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
monitor monitor-name
// shared variable declarations
procedure P1 (…) { …. }
…
procedure Pn (…) {……}
Initialization code ( ….) { … }
…
}
}
Schematic view of a Monitor
SYNCHRONIZATION 52 JGEC-IT
Synchronization Examples
SYNCHRONIZATION 53 JGECT-IT
Solaris
Windows XP
Linux
Pthreads
Bibliography
Operating System- by GALVIN.
Google.
SYNCHRONIZATION 54 JGEC-IT
SYNCHRONIZATION
THANK YOU……
SYNCHRONIZATION 55 JGEC-IT

Weitere ähnliche Inhalte

Was ist angesagt?

deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
wahab13
 

Was ist angesagt? (20)

process creation OS
process creation OSprocess creation OS
process creation OS
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
 
Memory organization (Computer architecture)
Memory organization (Computer architecture)Memory organization (Computer architecture)
Memory organization (Computer architecture)
 
Interrupts
InterruptsInterrupts
Interrupts
 
Deadlock
DeadlockDeadlock
Deadlock
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
SYNCHRONIZATION IN MULTIPROCESSING
SYNCHRONIZATION IN MULTIPROCESSINGSYNCHRONIZATION IN MULTIPROCESSING
SYNCHRONIZATION IN MULTIPROCESSING
 
Difference Program vs Process vs Thread
Difference Program vs Process vs ThreadDifference Program vs Process vs Thread
Difference Program vs Process vs Thread
 
Interconnection Network
Interconnection NetworkInterconnection Network
Interconnection Network
 
Scheduling
SchedulingScheduling
Scheduling
 
Memory organization in computer architecture
Memory organization in computer architectureMemory organization in computer architecture
Memory organization in computer architecture
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
 
cpu scheduling
cpu schedulingcpu scheduling
cpu scheduling
 
Timing and control
Timing and controlTiming and control
Timing and control
 
Computer architecture input output organization
Computer architecture input output organizationComputer architecture input output organization
Computer architecture input output organization
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 

Andere mochten auch

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 

Andere mochten auch (20)

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Operating Systems - Synchronization
Operating Systems - SynchronizationOperating Systems - Synchronization
Operating Systems - Synchronization
 
Producer Consumer Problem
Producer Consumer Problem  Producer Consumer Problem
Producer Consumer Problem
 
Parker Office Synch
Parker Office SynchParker Office Synch
Parker Office Synch
 
Os
OsOs
Os
 
Operating system
Operating systemOperating system
Operating system
 
os lacture
os lactureos lacture
os lacture
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMS
 
Seminar on synchronization to infinite bus
Seminar on synchronization to infinite busSeminar on synchronization to infinite bus
Seminar on synchronization to infinite bus
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Process synchronization in operating system
Process synchronization in operating systemProcess synchronization in operating system
Process synchronization in operating system
 
My SQl
My SQlMy SQl
My SQl
 
sql
sqlsql
sql
 
Mysql
MysqlMysql
Mysql
 
Saftey
SafteySaftey
Saftey
 
IP Addressing
IP AddressingIP Addressing
IP Addressing
 
Guide
GuideGuide
Guide
 
Shell Script
Shell ScriptShell Script
Shell Script
 
I/O Management
I/O ManagementI/O Management
I/O Management
 
1. review jurnal effect dwi hastho
1. review jurnal effect dwi hastho1. review jurnal effect dwi hastho
1. review jurnal effect dwi hastho
 

Ähnlich wie SYNCHRONIZATION

Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
Amanuelmergia
 
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.pptBIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
Kadri20
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
C.U
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 

Ähnlich wie SYNCHRONIZATION (20)

CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
MODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptxMODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptx
 
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.pptBIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
 
Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdf
 
Ch5 process synchronization
Ch5   process synchronizationCh5   process synchronization
Ch5 process synchronization
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Shared Memory
Shared MemoryShared Memory
Shared Memory
 
Os3
Os3Os3
Os3
 
OSCh7
OSCh7OSCh7
OSCh7
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
Analytical Modeling of End-to-End Delay in OpenFlow Based Networks
Analytical Modeling of End-to-End Delay in OpenFlow Based NetworksAnalytical Modeling of End-to-End Delay in OpenFlow Based Networks
Analytical Modeling of End-to-End Delay in OpenFlow Based Networks
 

SYNCHRONIZATION

  • 1. SYNCHRONIZATION Presented by: Anwesha Bhowmik(29) Atanu Deb(44) Information Technology(3rd Year) Jalpaiguri Government Engineering College 11/24/2015 SYNCHRONIZATION 1 JGEC-IT
  • 2. OBJECTIVES • To present both software and hardware solutions of the critical-section problem • To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data • To introduce the concept of an atomic transaction and describe mechanisms to ensure atomicity SYNCHRONIZATION 2 JGEC-IT
  • 3. PROCESS SYNCHRONIZATION • Background • The Critical-Section Problem • Peterson’s Solution • Semaphores • Classic Problems of Synchronization • Monitors • Synchronization Examples • Atomic Transactions SYNCHRONIZATION 3 JGEC-IT
  • 4. BACKGROUND • 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. SYNCHRONIZATION 4 JGEC-IT
  • 5. Producer while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; } SYNCHRONIZATION 5 JGEC-IT
  • 6. Consumer while (true) { while (count == 0) ; // do nothing /* consume the item in nextConsumed */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; } SYNCHRONIZATION 6 JGEC-IT
  • 7. Race Condition • count++ could be implemented as (producer) register1 = count register1 = register1 + 1 count = register1 • count-- could be implemented as (consumer) 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} SYNCHRONIZATION 7 JGEC-IT
  • 8. Critical Section Problem • Consider system of n processes { P0 , P1 , …, Pn-1 } • Each process has a critical section segment of code – Process may be changing common variables, updating table, writing file, etc. – When one process is in its critical section, no other may be executing in its critical section • Critical-section problem is to design a protocol to solve this • Each process must ask permission to enter its critical section in entry section, may follow critical section with exit section, the remaining code is in its remainder section SYNCHRONIZATION 8 JGEC-IT
  • 9. Critical Section(cntd.) SYNCHRONIZATION 9 JGEC-IT Strict Alternation Method
  • 11. Critical Section(cntd.) • General structure of process Pi is SYNCHRONIZATION 11 JGEC-IT
  • 12. Solution to Critical-Section Problem Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely SYNCHRONIZATION 12 JGEC-IT
  • 13. Solution to Critical-Section Problem(cntd.) SYNCHRONIZATION 13 JGEC-IT
  • 14. Solution to Critical-Section Problem(cntd.) 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted  Assume that each process executes at a nonzero speed  No assumption concerning relative speed of the N processes SYNCHRONIZATION 14 JGEC-IT
  • 15. Solution to Critical-Section Problem(cntd.) SYNCHRONIZATION 15 JGEC-IT
  • 16. Peterson’s Solution • Two process solution • Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. • The two processes share two variables: – int turn; – Boolean flag[2] • The variable turn indicates whose turn it is to enter the critical section. • The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready! SYNCHRONIZATION 16 JGEC-IT
  • 17. Algorithm for Process Pi do { flag[i] = TRUE; turn = j; while (flag[j] && turn == j); critical section flag[i] = FALSE; remainder section } while (TRUE); • Provable that 1. Mutual exclusion is preserved 2. Progress requirement is satisfied 3. Bounded-waiting requirement is met SYNCHRONIZATION 17 JGEC-IT
  • 19. Semaphore • Two atomic operations • Integer variable, with initial non-negative value – wait – signal (not the UNIX signal() call…) – p & v (proberen & verhogen), up and down, etc • wait – wait for semaphore to become positive, then decrement by 1 • signal – increment semaphore by 1 SYNCHRONIZATION 19 JGEC-IT
  • 20. Reason For Using Semaphore SYNCHRONIZATION 20 JGEC-IT
  • 21. Mutual exclusion using semaphores • Use wait and signal like lock and unlock – bracket critical sections in the same manner • Semaphore value should never be greater than 1 – This is a binary semaphore • Depends on correct order of calls by programmer • All mutex problems apply (deadlock…) SYNCHRONIZATION 21 JGEC-IT
  • 22. Mutual exclusion using semaphores(cntd.) SYNCHRONIZATION 22 JGECT-IT
  • 23. Semaphore Usage • Counting semaphore – integer value can range over an unrestricted domain • Binary semaphore – integer value can range only between 0 and 1 – Then equivalent to a mutex lock • Can solve various synchronization problems • Consider P1 and P2 that require S1 to happen before S2 P1: S1; signal(synch); //sync++ has added one resource P2: wait(synch); //executed only when synch is > 0 S2; SYNCHRONIZATION 23 JGEC-IT
  • 24. 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 codes are placed in the critical 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 SYNCHRONIZATION 24 JGEC-IT
  • 25. Semaphore Implementation with no Busy waiting o With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items:  an integer value  a list of processes  Two operations: – block – place a process into a waiting queue associated with the semaphore. – wakeup – changes the process from the waiting state to ready state SYNCHRONIZATION 25 JGEC-IT
  • 26. Semaphore Implementation with no Busy waiting (Cont.) • Implementation of wait: acquire () { value--; if (value < 0) { add this process to list; block(); } } • Implementation of signal: release () { value++; if (value <= 0) { remove a process P from list; wakeup(P); } } SYNCHRONIZATION 26 JGEC-IT
  • 27. Classical Problems of Synchronization • Bounded-Buffer Problem • Readers and Writers Problem • Dining-Philosophers Problem SYNCHRONIZATION 27 JGEC-IT
  • 28. Bounded-Buffer Problem • N buffers, each can hold one item • Semaphore mutex initialized to the value 1 • Semaphore full initialized to the value 0 • Semaphore empty initialized to the value N. SYNCHRONIZATION 28 JGEC-IT
  • 29. Bounded Buffer Problem (Cont.) SYNCHRONIZATION 29 JGEC-IT
  • 30. Bounded Buffer Problem (Cont.) • The structure of the producer process do { // produce an item in nextp wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (TRUE); SYNCHRONIZATION 30 JGEC-IT
  • 31. Bounded Buffer Problem (Cont.) • The structure of the consumer process do { wait (full); wait (mutex); // remove an item from buffer to nextc signal (mutex); signal (empty); // consume the item in nextc } while (TRUE); SYNCHRONIZATION 31 JGEC-IT
  • 32. Bounded Buffer Problem (Cont.) SYNCHRONIZATION 32 JGEC-IT
  • 34. 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 • 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) SYNCHRONIZATION 34 JGEC-IT
  • 35. Readers-Writers Problem (Cont.) • The structure of a writer process do { wait (wrt) ; // writing is performed signal (wrt) ; } while (TRUE); SYNCHRONIZATION 35 JGEC-IT
  • 36. Readers-Writers Problem (Cont.) • The structure of a reader process do { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; } while (TRUE); SYNCHRONIZATION 36 JGEC-IT
  • 39. Deadlock and Starvation • Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes • Let S and Q be two semaphores initialized to 1 P0 P1 S.acquire(); Q.acquire(); Q.acquire(); S.acquire(); . . . . . . S.release(); Q.release(); Q.release(); S.release(); SYNCHRONIZATION 39 JGEC-IT
  • 40. Deadlock and Starvation(cntd.) • P0 executes S.acquire() and then P1 executes Q.acquire(). • When P0 executes Q.acquire(), it must wait until P1 executes Q.release(). • Similarly, when P1 execute S.acquire(), it must wait until P0 executes S.release() • Since these signal operations cannot be executed, P0 and P1 are deadlocked. SYNCHRONIZATION 40 JGEC-IT
  • 43. Deadlock and Starvation(cntd.) • Starvation – indefinite blocking : • A process may never be removed from the semaphore queue in which it is suspended. SYNCHRONIZATION 43 JGEC-IT
  • 44. Dining-Philosophers Problem SYNCHRONIZATION 44 JGEC-IT Shared data Bowl of rice (data set) Semaphore chopstick [5] initialized to 1
  • 45. Dining-Philosophers Problem (Cont.) • The structure of Philosopher i: do { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE); What is the problem with the above? SYNCHRONIZATION 45 JGEC-IT
  • 46. Dining-Philosophers Problem (Cont.) SYNCHRONIZATION 46 JGEC-IT Picked upWaiting A deadlock occurs! If all five philosophers grabs her left chopsticks simultaneously.
  • 47. The Structure of Philosopher i  Philosopher i while ( true ) { // get left chopstick chopStick[i].acquire(); // get right chopstick chopStick[(i + 1) % 5].acquire(); eating(); //return left chopstick chopStick[i].release( ); // return right chopstick chopStick[(i + 1) % 5].release( ); thinking(); } SYNCHRONIZATION 47 JGEC-IT
  • 49. Possible Remedies • Placing restrictions on the philosophers – Allow at most four philosophers to be sitting simultaneously at the table – Allow a philosopher to pick up her chopsticks only if both are available – An odd philosopher picks up first her left chopsticks and even philosopher picks up her right chopstick and then right SYNCHRONIZATION 49 JGEC-IT
  • 50. Dining Philosophers Problem(cntd.) • Simultaneous semaphore : PHILOSOPHER CHOPSTICK 0 0,1 1 1,2 2 2,3 3 3,4 4 4,0 SYNCHRONIZATION 50 JGEC-IT
  • 51. Monitors SYNCHRONIZATION 51 JGEC-IT 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 monitor monitor-name // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……} Initialization code ( ….) { … } … } }
  • 52. Schematic view of a Monitor SYNCHRONIZATION 52 JGEC-IT
  • 53. Synchronization Examples SYNCHRONIZATION 53 JGECT-IT Solaris Windows XP Linux Pthreads
  • 54. Bibliography Operating System- by GALVIN. Google. SYNCHRONIZATION 54 JGEC-IT