The Critical Section Problem

Slides:



Advertisements
Similar presentations
Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
Advertisements

Operating Systems Part III: Process Management (Process Synchronization)
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem.
CPSC 668Set 7: Mutual Exclusion with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
The Critical-Section Problem
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
Shared Memory Coordination We will be looking at process coordination using shared memory and busy waiting. –So we don't send messages but read and write.
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Bakery Algorithm - Proof
Concurrency in Distributed Systems: Mutual exclusion.
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
1 Synchronization (1) Dave Eckhardt
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
1 Chapter 10 Distributed Algorithms. 2 Chapter Content This and the next two chapters present algorithms designed for loosely-connected distributed systems.
THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem.
3.1. Concurrency, Critical Sections, Semaphores
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion Using Atomic Registers Lecturer: Netanel Dahan Instructor: Prof. Yehuda Afek B.Sc. Seminar on Distributed Computation Tel-Aviv University.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Synchronicity Introduction to Operating Systems: Module 5.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Homework-6 Questions : 2,10,15,22.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Bakery Algorithm - Proof
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Background on the need for Synchronization
Chapter 6-7: Process Synchronization
ICS 143 Principles of Operating Systems
Concurrent Distributed Systems
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
The Critical-Section Problem
Introduction to Cooperating Processes
Lecture 20 Syed Mansoor Sarwar
Lecture 2 Part 2 Process Synchronization
Grades.
Lecture 21 Syed Mansoor Sarwar
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Chapter 6: Synchronization Tools
Presentation transcript:

The Critical Section Problem

Concurrent Software Systems 2 Problem Description Informally, a critical section is a code segment that accesses shared variables and has to be executed as an atomic action. The critical section problem refers to the problem of how to ensure that at most one process is executing its critical section at a given time. Important: Critical sections in different threads are not necessarily the same code segment! Concurrent Software Systems 2

Concurrent Software Systems 3 Problem Description Formally, the following requirements should be satisfied: Mutual exclusion: When a thread is executing in its critical section, no other threads can be executing in their critical sections. Progress: If no thread is executing in its critical section, and if there are some threads that wish to enter their critical sections, then one of these threads will get into the critical section. Bounded waiting: After a thread makes a request to enter its critical section, there is a bound on the number of times that other threads are allowed to enter their critical sections, before the request is granted. Concurrent Software Systems 3

Concurrent Software Systems 4 Problem Description In discussion of the critical section problem, we often assume that each thread is executing the following code. It is also assumed that (1) after a thread enters a critical section, it will eventually exit the critical section; (2) a thread may terminate in the non-critical section. while (true) { entry section critical section exit section non-critical section } Concurrent Software Systems 4

Concurrent Software Systems 5 Solution 1 In this solution, lock is a global variable initialized to false. A thread sets lock to true to indicate that it is entering the critical section. boolean lock = false; T0: while (true) { while (lock) { ; } (1) lock = true; (2) critical section (3) lock = false; (4) non-critical section (5) } T1: while (true) { while (lock) { ; } (1) lock = true; (2) critical section (3) lock = false; (4) non-critical section (5) } Concurrent Software Systems 5

Concurrent Software Systems 6 Solution 1 is incorrect! T0 T1 Comments context switch (1) (2) (3) T0 exits the while loop T1 exits the while loop lock is set to true T1 enters the critical section T0 enters the critical section Concurrent Software Systems 6

Concurrent Software Systems 7 Solution 2 The threads use a global array intendToEnter to indicate their intention to enter the critical section. boolean intendToEnter[] = {false, false}; T0: while (true) { while (intendToEnter[1]) { ; } (1) intendToEnter[0] = true; (2) critical section (3) intendToEnter[0] = false; (4) non-critical section (5) } T1: while (true) { while (intendToEnter[0]) { ; } (1) intendToEnter[1] = true; (2) critical section (3) intendToEnter[1] = false; (4) non-critical section (5) } Concurrent Software Systems 7

Concurrent Software Systems 8 Solution 2 is incorrect! T0 T1 Comments context switch (1) (2) (3) T0 exits the while loop T1 exits the while loop intendtoEnter[1] is set to true T1 enters the critical section intendToEnter[0] is set to true T0 enters the critical section Concurrent Software Systems 8

Concurrent Software Systems 9 Solution 3 The global variable turn is used to indicate the next process to enter the critical section. The initial value of turn can be 0 or 1. int turn = 1; T0: while (true) { while (turn != 0) { ; } (1) critical section (2) turn = 1; (3) non-critical section (4) } T1: while (true) { while (turn != 1) { ; } (1) critical section (2) turn = 0; (3) non-critical section (4) } Concurrent Software Systems 9

Concurrent Software Systems 10 Solution 3 is incorrect! T0 T1 Comments context switch (1) (2) (3) (4) T1 exits the while loop T1 enters the critical section turn is set to 0 T1 terminates in non-critical section T0 exits the while loop T0 enters the critical section turn is set to 1 T0 executes non-critical section T0 repeats (1) forever Concurrent Software Systems 10

Concurrent Software Systems 11 Solution 4 When a thread finds that the other thread also intends to enter its critical section, it sets its own intendToEnter flag to false and waits until the other thread exits its critical section. boolean intendToEnter[] = {false, false}; T0: while (true) { intendToEnter[0] = true; (1) while (intendToEnter[1]) { (2) intendToEnter[0] = false; (3) while (intendToEnter[1]) {;} (4) intendToEnter[0] = true; (5) } critical section (6) intendToEnter[0] = false; (7) non-critical section (8) T1: while (true) { intendToEnter[1] = true; (1) while (intendToEnter[0]) { (2) intendToEnter[1] = false; (3) while (intendToEnter[0]) {;} (4) intendToEnter[1] = true; (5) } critical section (6) intendToEnter[1] = false; (7) non-critical section (8) Concurrent Software Systems 11

Concurrent Software Systems 12 Solution 4 is incorrect! T0 T1 Comments context switch (1) (2) (3) (4) (7) (8) (6) intendToEnter[0] is set to true T0 exits while loop T0 enters critical section; intendToEnter[0] is true repeat infinitely intendToEnter[1] is set to true T1 enters while (intendToEnter[0]) loop intendToEnter[1] is set to false T1 enters second while(intendToEnter[0]) loop intendToEnter[0] is set to false T0 executes the non-critical section T1 is still waiting for intendToEnter[0] to be false Concurrent Software Systems 12

Concurrent Software Systems 13 How to check a solution Informally, we should consider three important cases: One thread intends to enter its critical section, and the other thread is not in its critical section or in its entry section. One thread intends to enter its critical section, and the other thread is in its critical section. Both threads intend to enter their critical sections. Concurrent Software Systems 13

Concurrent Software Systems 14 Peterson’s algorithm Peterson’s algorithm is a combination of solutions (3) and (4). boolean intendToEnter[]= {false, false}; int turn; // no initial value for turn is needed. T0: while (true) { intendToEnter[0] = true; (1) turn = 1; (2) while (intendToEnter[1] && turn == 1) { ; } (3) critical section (4) intendToEnter[0] = false; (5) non-critical section (6) } T1: while (true) { intendToEnter[1] = true; (1) turn = 0; (2) while (intendToEnter[0] && turn == 0) { ; } (3) critical section (4) intendToEnter[1] = false; (5) non-critical section (6) } Concurrent Software Systems 14

Concurrent Software Systems 15 Peterson’s algorithm Informally, we consider the following cases: Assume that one thread, say T0, intends to enter its critical section and T1 is not in its critical section or its entry-section. Then intendToEnter[0] is true and intendToEnter[1] is false and T0 will enter the critical section immediately. Concurrent Software Systems 15

Concurrent Software Systems 16 Peterson’s algorithm Assume that thread T0 intends to enter its critical section and T1 is in its critical section. Since turn = 1, T0 loops at statement (3). After the execution of (5) by T1, if T0 resumes execution before T1 intends to enter again, T0 enters its critical section immediately; otherwise, see case (3). Concurrent Software Systems 16

Concurrent Software Systems 17 Peterson’s algorithm Assume both threads intend to enter the critical section, i.e. both threads have set their intendToEnter flags to true. The thread that first executes “turn = ...;” waits until the other thread executes “turn = ...;” and then enters its critical section. The other thread will be the next thread to enter the critical section. Concurrent Software Systems 17

Concurrent Software Systems 18 Peterson’s algorithm T0 T1 Comments (1) context switch intendToEnter[0] is set to true (1) (2) (3) intendToEnter[1] is set to true turn is set to 0 T1 enters the while loop context switch (2) (3) turn is set to 1 T0 enters the while loop context switch (3) (4) (5) (6) (1) (2) T1 exits while loop T1 enters the critical section intendToEnter[1] is set to false T1 executes the non-critical section intendToEnter[1] is set to true turn is set to 0 T1 enters while loop context switch (3) (4) (5) (6) T0 exits while loop T0 enters critical section intendToEnter[0] is set to false T0 executes the non-critical section context switch (3) (4) T1 exits the while loop T1 enters critical section Concurrent Software Systems 18

Concurrent Software Systems 19 Bakery algorithm Bakery algorithm is used to solve the n-process critical section problem. The main idea is the following: When a thread wants to enter a critical section, it gets a ticket. Each ticket has a number. Threads are allowed to enter the critical section in ascending order of their ticket numbers. Concurrent Software Systems 19

Concurrent Software Systems 20 Version 1 int number[n]; // array of ticket numbers, initially all elements of number is 0 while (true) { number[i] = max(number) + 1; (1) for (int j = 0; j < n; j ++) { (2) while (j != i && number[j] != 0 && (number[j], j) < (number[i], i)) { ;} (3) } critical section (4) number[i] = 0; (5) non-critical section (6) (a, b) < (c, d) if a < c or (a == c and b < d) max(number) is the max value of all the elements in number Concurrent Software Systems 20

Concurrent Software Systems 21 Version 1 is incorrect! T0 T1 Comments T0 executes max(number) + 1, switch occur before 1 is assigned to number[0] (1) (1) (2) (3) (4) T1 sets number[1] to 1 T1 starts for loop T1 exits while and for loop T1 enters critical section context switch (1) (2) (3) (4) T0 assigns 1 (not 2) to number[0] T0 starts for loop To exits the while and for loops T0 enters critical section Concurrent Software Systems 21

Concurrent Software Systems 22 Bakery algorithm int number[n]; // array of ticket numbers, initially all elements of number is 0 boolean choosing[n]; // initially all elements of choosing is false while (true) { choosing[i] = true; (1) number[i] = max(number) + 1; (2) choosing[i] = false; (3) for (int j = 0; j < n; j ++) { (4) while (choosing[j]) { ; } (5) while (j != i && number[j] != 0 && (number[j], j) < (number[i], i)) { ;} (6) } critical section (7) number[i] = 0; (8) non-critical section (9) (a, b) < (c, d) if a < c or (a == c and b < d) max(number) is the max value of all the elements in number Concurrent Software Systems 22

Concurrent Software Systems 23 Bakery algorithm Let us consider the following cases: One thread, say Ti, intends to enter its critical section and no other thread is in its critical section or entry section. Then number[i] = 1 and number[j], where j  i, is 0. Thus, Ti enters its critical section immediately. One thread, say Ti, intends to enter its critical section and Tk, k  i, is in its critical section. Then at (6), when j = k, number[j] < number[i]. Thus, Ti is delayed at (6) until Tk executes (8). Concurrent Software Systems 23

Concurrent Software Systems 24 Bakery algorithm Two or more threads intend to enter their critical sections and no other threads is in its critical section. Assume that Tk and Tm, where k < m, intend to enter. Consider the possible relationships between number[k] and number[m]: number[k] < number[m]. Tk enters its critical section since (number[m], m) > (number[k], k). number[k] == number[m]. Tk enters its critical section since (number[m], m) > (number[k], k). number[k] > number[m]. Tm enters its critical section since (number[k], k) > (number[m], m). Concurrent Software Systems 24