SlideShare a Scribd company logo
Chapter 5:
Process Synchronization
(UNIT II)
Chapter 5: Process Synchronization
• Background
• The Critical-Section Problem
• Peterson’s Solution
Background
• Processes can execute concurrently
– May be interrupted at any time, partially completing
execution
• Concurrent access to shared data may result in data
inconsistency
• Maintaining data consistency requires mechanisms to ensure
the orderly execution of cooperating processes
• Illustration of the problem:
Suppose that we wanted to provide a solution to the consumer-
producer problem that fills all the buffers. One possibility is to
add an integer variable counter, initialized to 0. Counter is
incremented every time we add a new item to the buffer and is
decremented every time we remove one item from the buffer
Producer
while (true) {
/* produce an item in next
produced */
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next
consumed */
}
Race Condition
• counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
• counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
• Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 – 1 {register2 = 4}
S4: producer execute counter = register1 {counter = 6 }
S5: consumer execute counter = register2 {counter = 4}
• A situation like this, where several processes access and manipulate the same data concurrently
and the outcome of the execution depends on the particular order in which the access takes place,
is called a race condition.
Critical Section Problem
• Consider system of n processes {p0, p1, … pn-1}
• Each process has critical section segment of code
– Process may be changing common variables, updating
table, writing file, etc
– When one process in critical section, no other may be in
its critical section
• Critical section problem is to design protocol to solve this
• Each process must ask permission to enter critical section
in entry section, may follow critical section with exit
section, then remainder section
Critical Section
• General structure of process Pi
Solution to Critical-Section Problem
1. 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
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
Critical-Section Handling in OS
Two approaches depending on if kernel is preemptive or
non- preemptive
– Preemptive – allows preemption of process when
running in kernel mode
– Non-preemptive – runs until exits kernel mode, blocks,
or voluntarily yields CPU
• Essentially free of race conditions in kernel mode

More Related Content

PPTX
Operating system 23 process synchronization
PPTX
794985751-Unit-3-Inter-Process-Communication.pptx
PDF
Lecture 5- Process Synchonization_revised.pdf
PPTX
Lecture 5- Process Synchronization (1).pptx
PPT
process syn.ppt
PPTX
Lecture 8.pptx Operating system lecture
PDF
Ch5 process synchronization
PPTX
Chapter 5 - Operating Synchronization.pptx
Operating system 23 process synchronization
794985751-Unit-3-Inter-Process-Communication.pptx
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchronization (1).pptx
process syn.ppt
Lecture 8.pptx Operating system lecture
Ch5 process synchronization
Chapter 5 - Operating Synchronization.pptx

Similar to Chapter 5-Process Synchronization (Unit 2).ppt (20)

PPTX
MODULE 3 process synchronizationnnn.pptx
PPTX
os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
PPTX
synchronization in operating system structure
PDF
Process Synchronization
PPTX
Synchronization Peterson’s Solution.pptx
PPTX
Mutual Exclusion using Peterson's Algorithm
PPTX
Chapter6 Synchronization in Operating systems.pptx
PPT
Lecture16-17.ppt
PPT
OS Unit 3 ProcessSyncronization in operation g system
PPTX
Critical Section Problem.pptx
PPT
Ipc feb4
PDF
PGConf APAC 2018 - Tale from Trenches
PPTX
Process management in Operating System_Unit-2
PDF
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
PPTX
Lecture 5 inter process communication
PPT
Producer Consumer Problem in C explained.ppt
PPTX
5 Inter Process note Communication.pptx
PPT
U3-PPT-1 (1).ppt
PPTX
Process synchronization
PPTX
UNIT-2 - Concurrency & Interprocess Communicatio.pptx
MODULE 3 process synchronizationnnn.pptx
os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
synchronization in operating system structure
Process Synchronization
Synchronization Peterson’s Solution.pptx
Mutual Exclusion using Peterson's Algorithm
Chapter6 Synchronization in Operating systems.pptx
Lecture16-17.ppt
OS Unit 3 ProcessSyncronization in operation g system
Critical Section Problem.pptx
Ipc feb4
PGConf APAC 2018 - Tale from Trenches
Process management in Operating System_Unit-2
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
Lecture 5 inter process communication
Producer Consumer Problem in C explained.ppt
5 Inter Process note Communication.pptx
U3-PPT-1 (1).ppt
Process synchronization
UNIT-2 - Concurrency & Interprocess Communicatio.pptx
Ad

More from ssuser09d6cd1 (8)

PPTX
Memory_Management_Diagrams and virtual memory.pptx
PPT
memory management and Virtual Memory.ppt
PPT
ch8 Memory management and Virtual Memory.ppt
PPT
ch9 memory Management and virtual memory.ppt
PPT
Chapter 5-Process Synchronization OS.ppt
PPT
Chapter 5-Process Synchronization (Unit 2).ppt
PPT
Chapter 5-Process Synchronization 22.ppt
PPT
Operating Systems Chapter-6 power PointT
Memory_Management_Diagrams and virtual memory.pptx
memory management and Virtual Memory.ppt
ch8 Memory management and Virtual Memory.ppt
ch9 memory Management and virtual memory.ppt
Chapter 5-Process Synchronization OS.ppt
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization 22.ppt
Operating Systems Chapter-6 power PointT
Ad

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Artificial Intelligence
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
composite construction of structures.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
OOP with Java - Java Introduction (Basics)
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
additive manufacturing of ss316l using mig welding
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Well-logging-methods_new................
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
introduction to datamining and warehousing
DOCX
573137875-Attendance-Management-System-original
Lecture Notes Electrical Wiring System Components
Mechanical Engineering MATERIALS Selection
CYBER-CRIMES AND SECURITY A guide to understanding
Artificial Intelligence
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
composite construction of structures.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Sustainable Sites - Green Building Construction
OOP with Java - Java Introduction (Basics)
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
additive manufacturing of ss316l using mig welding
Automation-in-Manufacturing-Chapter-Introduction.pdf
Well-logging-methods_new................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
introduction to datamining and warehousing
573137875-Attendance-Management-System-original

Chapter 5-Process Synchronization (Unit 2).ppt

  • 2. Chapter 5: Process Synchronization • Background • The Critical-Section Problem • Peterson’s Solution
  • 3. Background • Processes can execute concurrently – May be interrupted at any time, partially completing execution • Concurrent access to shared data may result in data inconsistency • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes • Illustration of the problem: Suppose that we wanted to provide a solution to the consumer- producer problem that fills all the buffers. One possibility is to add an integer variable counter, initialized to 0. Counter is incremented every time we add a new item to the buffer and is decremented every time we remove one item from the buffer
  • 4. Producer while (true) { /* produce an item in next produced */ while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; counter++; }
  • 5. Consumer while (true) { while (counter == 0) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /* consume the item in next consumed */ }
  • 6. Race Condition • counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 • counter-- could be implemented as register2 = counter register2 = register2 - 1 counter = register2 • Consider this execution interleaving with “count = 5” initially: S0: producer execute register1 = counter {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = counter {register2 = 5} S3: consumer execute register2 = register2 – 1 {register2 = 4} S4: producer execute counter = register1 {counter = 6 } S5: consumer execute counter = register2 {counter = 4} • A situation like this, where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place, is called a race condition.
  • 7. Critical Section Problem • Consider system of n processes {p0, p1, … pn-1} • Each process has critical section segment of code – Process may be changing common variables, updating table, writing file, etc – When one process in critical section, no other may be in its critical section • Critical section problem is to design protocol to solve this • Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section
  • 8. Critical Section • General structure of process Pi
  • 9. Solution to Critical-Section Problem 1. 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 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
  • 10. Critical-Section Handling in OS Two approaches depending on if kernel is preemptive or non- preemptive – Preemptive – allows preemption of process when running in kernel mode – Non-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPU • Essentially free of race conditions in kernel mode