SlideShare a Scribd company logo
Synchronization Tools
Course Code: CSC 2209
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 08 Week No: 08 Semester:
Lecturer: Name & email
Course Title: Operating Systems
Lecture Outline
1. Background
2. The Critical-Section Problem
3. 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. We can do so by having an integer counter
that keeps track of the number of full buffers. Initially, counter 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.
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
 A race condition is an undesirable situation that occurs when a
device or system attempts to perform two or more operations
at the same time, but because of the nature of the device or
system, the operations must be done in the proper sequence to
be done correctly.
 A race condition occurs when two or more threads can access
shared data and they try to change it at the same time.
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}
Race Condition
 Processes P0 and P1 are creating child processs using the fork() system call
 Race condition on kernel variable next_available_pid which represents the next
available process identifier (pid)
 Unless there is mutual exclusion, the same pid could be assigned to two different processes!
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
Peterson’s Solution
 Not guaranteed to work on modern architectures! (But good algorithmic description
of solving the problem)
 Two process solution
 Assume that the load and store machine-language 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!
Algorithm for Process Pi
while (true){
flag[i] = true;
turn = j;
while (flag[j] && turn = = j)
;
/* critical section */
flag[i] = false;
/* remainder section */
}
Peterson’s Solution (cont’d)
 Provable that the three CS requirement are met:
1. Mutual exclusion is preserved
Pi enters CS only if:
either flag[j] = false or turn = i
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
Peterson’s Solution
 Although useful for demonstrating an algorithm, Peterson’s Solution is
not guaranteed to work on modern architectures.
 Understanding why it will not work is also useful for better
understanding race conditions.
 To improve performance, processors and/or compilers may reorder
operations that have no dependencies.
 For single-threaded this is ok as the result will always be the same.
 For multithreaded the reordering may produce inconsistent or
unexpected results!
Peterson’s Solution
 Two threads share the data:
boolean flag = false;
int x = 0;
 Thread 1 performs
while (!flag)
;
print x
 Thread 2 performs
x = 100;
flag = true
 What is the expected output?
Peterson’s Solution
 100 is the expected output.
 However, the operations for Thread 2 may be reordered:
flag = true;
x = 100;
 If this occurs, the output may be 0!
 The effects of instruction reordering in Peterson’s Solution
 This allows both processes to be in their critical section at the same time!
Books
 Operating Systems Concept
 Written by Galvin and Silberschatz
 Edition: 9th
References
 Operating Systems Concept
 Written by Galvin and Silberschatz
 Edition: 9th

More Related Content

PPTX
operating system notes about deadlock 3.pptx
PPT
Chapter 5-Process Synchronization (Unit 2).ppt
PPT
Chapter 5-Process Synchronization (Unit 2).ppt
PPTX
Lecture 5- Process Synchronization (1).pptx
PPTX
Operating system 23 process synchronization
PPTX
Chapter6 Synchronization in Operating systems.pptx
PPT
Chapter 5 Process Synchronization os.ppt
 
PPT
Process creation and Synchronisation.ppt
operating system notes about deadlock 3.pptx
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization (Unit 2).ppt
Lecture 5- Process Synchronization (1).pptx
Operating system 23 process synchronization
Chapter6 Synchronization in Operating systems.pptx
Chapter 5 Process Synchronization os.ppt
 
Process creation and Synchronisation.ppt

Similar to Lecture 8.pptx Operating system lecture (20)

PPT
CPU process control block (Chapter 4).pptx
PDF
Lecture 5- Process Synchonization_revised.pdf
PDF
CH05.pdf
PPT
ch5 [Autosaved].ppt
PPTX
794985751-Unit-3-Inter-Process-Communication.pptx
PPTX
Synchronization Peterson’s Solution.pptx
PDF
Ch5 process synchronization
PPTX
14- Process Synchronization.pptx
PPT
Operating Systems - "Chapter 5 Process Synchronization"
PPT
Lecture16-17.ppt
PPT
Process Synchronization
PDF
ch5-Process_Synchronization.pdf
PPT
chapter5 processes of synchronizatio ppt
PPT
ch5.ppt
PPT
ch5.ppt
PPT
ch5.ppt operating system
PDF
Process Synchronization
PPT
Operating System Process Synchronization
PPTX
Synchronization in os.pptx
PPT
Process synchronization(deepa)
CPU process control block (Chapter 4).pptx
Lecture 5- Process Synchonization_revised.pdf
CH05.pdf
ch5 [Autosaved].ppt
794985751-Unit-3-Inter-Process-Communication.pptx
Synchronization Peterson’s Solution.pptx
Ch5 process synchronization
14- Process Synchronization.pptx
Operating Systems - "Chapter 5 Process Synchronization"
Lecture16-17.ppt
Process Synchronization
ch5-Process_Synchronization.pdf
chapter5 processes of synchronizatio ppt
ch5.ppt
ch5.ppt
ch5.ppt operating system
Process Synchronization
Operating System Process Synchronization
Synchronization in os.pptx
Process synchronization(deepa)
Ad

More from ReelsShortVideo (6)

PPTX
Lecture 7.pptx Operating system lecture
PPTX
Bridges.pptx
PPTX
Ch9 SL3 ODE-BVP.pptx
PPTX
MIGRATION IN BANGLADESH.pptx
PPTX
bs presentation.pptx
PPTX
Water_Level_Indicator.pptx
Lecture 7.pptx Operating system lecture
Bridges.pptx
Ch9 SL3 ODE-BVP.pptx
MIGRATION IN BANGLADESH.pptx
bs presentation.pptx
Water_Level_Indicator.pptx
Ad

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Understanding Forklifts - TECH EHS Solution
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
medical staffing services at VALiNTRY
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
top salesforce developer skills in 2025.pdf
PPT
Introduction Database Management System for Course Database
PDF
System and Network Administration Chapter 2
PPTX
L1 - Introduction to python Backend.pptx
PDF
Nekopoi APK 2025 free lastest update
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
How to Migrate SBCGlobal Email to Yahoo Easily
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Wondershare Filmora 15 Crack With Activation Key [2025
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Understanding Forklifts - TECH EHS Solution
PTS Company Brochure 2025 (1).pdf.......
medical staffing services at VALiNTRY
Operating system designcfffgfgggggggvggggggggg
top salesforce developer skills in 2025.pdf
Introduction Database Management System for Course Database
System and Network Administration Chapter 2
L1 - Introduction to python Backend.pptx
Nekopoi APK 2025 free lastest update
Online Work Permit System for Fast Permit Processing
VVF-Customer-Presentation2025-Ver1.9.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Which alternative to Crystal Reports is best for small or large businesses.pdf
ISO 45001 Occupational Health and Safety Management System
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025

Lecture 8.pptx Operating system lecture

  • 1. Synchronization Tools Course Code: CSC 2209 Dept. of Computer Science Faculty of Science and Technology Lecturer No: 08 Week No: 08 Semester: Lecturer: Name & email Course Title: Operating Systems
  • 2. Lecture Outline 1. Background 2. The Critical-Section Problem 3. 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. We can do so by having an integer counter that keeps track of the number of full buffers. Initially, counter 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.
  • 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  A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the same time, but because of the nature of the device or system, the operations must be done in the proper sequence to be done correctly.  A race condition occurs when two or more threads can access shared data and they try to change it at the same time.
  • 7. 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}
  • 8. Race Condition  Processes P0 and P1 are creating child processs using the fork() system call  Race condition on kernel variable next_available_pid which represents the next available process identifier (pid)  Unless there is mutual exclusion, the same pid could be assigned to two different processes!
  • 9. 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
  • 10. Critical Section  General structure of process Pi
  • 11. 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
  • 12. 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
  • 13. Peterson’s Solution  Not guaranteed to work on modern architectures! (But good algorithmic description of solving the problem)  Two process solution  Assume that the load and store machine-language 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!
  • 14. Algorithm for Process Pi while (true){ flag[i] = true; turn = j; while (flag[j] && turn = = j) ; /* critical section */ flag[i] = false; /* remainder section */ }
  • 15. Peterson’s Solution (cont’d)  Provable that the three CS requirement are met: 1. Mutual exclusion is preserved Pi enters CS only if: either flag[j] = false or turn = i 2. Progress requirement is satisfied 3. Bounded-waiting requirement is met
  • 16. Peterson’s Solution  Although useful for demonstrating an algorithm, Peterson’s Solution is not guaranteed to work on modern architectures.  Understanding why it will not work is also useful for better understanding race conditions.  To improve performance, processors and/or compilers may reorder operations that have no dependencies.  For single-threaded this is ok as the result will always be the same.  For multithreaded the reordering may produce inconsistent or unexpected results!
  • 17. Peterson’s Solution  Two threads share the data: boolean flag = false; int x = 0;  Thread 1 performs while (!flag) ; print x  Thread 2 performs x = 100; flag = true  What is the expected output?
  • 18. Peterson’s Solution  100 is the expected output.  However, the operations for Thread 2 may be reordered: flag = true; x = 100;  If this occurs, the output may be 0!  The effects of instruction reordering in Peterson’s Solution  This allows both processes to be in their critical section at the same time!
  • 19. Books  Operating Systems Concept  Written by Galvin and Silberschatz  Edition: 9th
  • 20. References  Operating Systems Concept  Written by Galvin and Silberschatz  Edition: 9th