Week#13 ch6
Week#13 ch6
Tools
Instructor: Dr. Farzana
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
▪ Background
▪ The Critical-Section Problem
▪ Peterson’s Solution
▪ Hardware Support for Synchronization
▪ Mutex Locks
▪ Semaphores
▪ Monitors
▪ Liveness
▪ Evaluation
Operating System Concepts – 10th Edition 6.2 Silberschatz, Galvin and Gagne ©2018
Objectives
▪ Describe the critical-section problem and illustrate a
race condition
▪ Illustrate hardware solutions to the critical-section
problem using memory barriers, compare-and-swap
operations, and atomic variables
▪ Demonstrate how mutex locks, semaphores,
monitors, and condition variables can be used to
solve the critical section problem
▪ Evaluate tools that solve the critical-section problem
in low-, Moderate-, and high-contention scenarios
Operating System Concepts – 10th Edition 6.3 Silberschatz, Galvin and Gagne ©2018
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
▪ We illustrated in chapter 4 the problem when we considered the
Bounded Buffer problem with use of a counter that is updated
concurrently by the producer and consumer,. Which lead to race
condition.
Operating System Concepts – 10th Edition 6.4 Silberschatz, Galvin and Gagne ©2018
Race Condition
▪ Processes P0 and P1 are creating child processes using the fork()
system call
▪ Race condition on kernel variable next_available_pid which
represents the next available process identifier (pid)
Operating System Concepts – 10th Edition 6.5 Silberschatz, Galvin and Gagne ©2018
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
Operating System Concepts – 10th Edition 6.6 Silberschatz, Galvin and Gagne ©2018
Critical Section
Operating System Concepts – 10th Edition 6.7 Silberschatz, Galvin and Gagne ©2018
Critical-Section Problem (Cont.)
Requirements for solution to critical-section problem
Operating System Concepts – 10th Edition 6.8 Silberschatz, Galvin and Gagne ©2018
Interrupt-based Solution
▪ Entry section: disable interrupts
▪ Exit section: enable interrupts
▪ Will this solve the problem?
• What if the critical section is code that runs for an hour?
• Can some processes starve – never enter their critical section.
• What if there are two CPUs?
If the second CPU process want to enter the critical section.Then 2nd CPU will remain idle.
Operating System Concepts – 10th Edition 6.9 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution
▪ 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:
Load and store for loading and storing the program
• int turn; from/to memory
• boolean flag[2]
Operating System Concepts – 10th Edition 6.10 Silberschatz, Galvin and Gagne ©2018
Algorithm for Process Pi
while (true){
flag[i] = true;
turn = j;
while (flag[j] && turn = = j)
;
/* critical section */
flag[i] = false;
/* remainder section */
Operating System Concepts – 10th Edition 6.11 Silberschatz, Galvin and Gagne ©2018
Correctness of Peterson’s Solution
Agr p1 and p2 ne request k hui ha to kitni dafa p1 k critical section mil skta h p2 k mile begair
Operating System Concepts – 10th Edition 6.12 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution and Modern Architecture
Operating System Concepts – 10th Edition 6.13 Silberschatz, Galvin and Gagne ©2018
Synchronization Hardware
2. Atomic variables
Operating System Concepts – 10th Edition 6.14 Silberschatz, Galvin and Gagne ©2018
Hardware Instructions
Operating System Concepts – 10th Edition 6.15 Silberschatz, Galvin and Gagne ©2018
The test_and_set Instruction
▪ Definition
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = true;
return rv:
}
▪ Properties
• Executed atomically
• Returns the original value of passed parameter
• Set the new value of passed parameter to true
Operating System Concepts – 10th Edition 6.16 Silberschatz, Galvin and Gagne ©2018
Solution Using test_and_set()
▪ Shared boolean variable lock, initialized to false
▪ Solution:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);
Operating System Concepts – 10th Edition 6.17 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 6.18 Silberschatz, Galvin and Gagne ©2018
Semaphores
Operating System Concepts – 10th Edition 6.19 Silberschatz, Galvin and Gagne ©2018
Wait and Signal (Cont.)
Operating System Concepts – 10th Edition 6.20 Silberschatz, Galvin and Gagne ©2018
Types of semaphores
Operating System Concepts – 10th Edition 6.21 Silberschatz, Galvin and Gagne ©2018