Lecture07 Sync
Lecture07 Sync
Synchronization Principles
• Background
– Concurrent access to shared data may result in data
inconsistency.
– Maintaining data consistency requires mechanisms
to ensure the orderly execution of cooperating
Synchronization Principles processes.
• The Critical-Section Problem
– Pure software solution
CS 256/456 – With help from the hardware
Dept. of Computer Science, University • Synchronization without busy waiting (with the support of
of Rochester process/thread scheduler)
– Semaphore
– Mutex lock
– Condition variables
9/20/2018 CSC 2/456 1 9/20/2018 CSC 2/456 2
CSC 256/456 1
Operating Systems 9/20/2018
CSC 256/456 2
Operating Systems 9/20/2018
Algorithm 2 Algorithm 3
• Shared variables:
• Combine shared variables of algorithms 1 and 2.
– boolean flag[2];
initially flag[0] = flag[1] = false; • Process Pi
– flag[i]==true Pi ready to enter its critical do {
section flag[i] = true;
turn = j;
• Process Pi while (flag[j] && turn==j) ;
do { critical section
flag[i] = true; flag[i] = false;
while (flag[j]) ; remainder section
critical section } while (1);
flag[i] = false;
remainder section • Meets all three requirements; solves the critical-section
} while (1); problem for two processes. called Peterson’s algorithm.
CSC 256/456 3
Operating Systems 9/20/2018
CSC 256/456 4
Operating Systems 9/20/2018
CSC 256/456 5
Operating Systems 9/20/2018
CSC 256/456 6
Operating Systems 9/20/2018
CSC 256/456 7
Operating Systems 9/20/2018
CSC 256/456 8
Operating Systems 9/20/2018
Disclaimer
• Parts of the lecture slides contain original work from Gary
Nutt, Andrew S. Tanenbaum, and Kai Shen. The slides are
intended for the sole purpose of instruction of operating
systems at the University of Rochester. All copyrighted
materials belong to their original owner(s).
CSC 256/456 9