Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.

Slides:



Advertisements
Similar presentations
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Advertisements

Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Synchronization Principles Gordon College Stephen Brinton.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
Classical Problems of Concurrency
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
CIS 415 Process Synchronization Background The Critical-Section Problem Software Solutions Semaphores Hardware Support Classical Problems of Synchronization.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Synchronization Solutions
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrency, Mutual Exclusion and Synchronization.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
3.1. Concurrency, Critical Sections, Semaphores
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
7.1 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
CSE Operating System Principles Synchronization.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
Chapter 5: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Synchronization Hank Levy 1.
Critical section problem
Synchronization Hank Levy 1.
Presentation transcript:

Process Synchronization CS 360

Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization

Slide 3 CS 360, WSU Vancouver Background Concurrent access to shared data may result in data inconsistency or non- deterministic behavior Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Race condition: 4 The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. 4 To prevent race conditions, concurrent processes must be synchronized.

Slide 4 CS 360, WSU Vancouver The Critical-Section Problem n A resource that allows only one user at a time to use is called a critical resource. n Processes compete for the use of critical resources n Each process has a code segment, called a critical section, in which the concerned critical resource is used. n Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in the critical section with the critical resource.

Slide 5 CS 360, WSU Vancouver Definitions 1. Mutual Exclusion: At most one process can be inside its critical section at a time. 2. Progress: If no process is executing in its critical section, no process outside its critical section should block other processes indefinitely from entering their critical section. 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. 4. No process can remain inside the critical section indefinitely. 5. No assumption concerning relative speed of the n processes or the number of CPUs can be made. 6. Deadlock is when two or more processes are waiting for an event to occur and that event can never occur. 7. Processes not deadlocked could wait for events that may never occur because of biases in the resource scheduling policies of a system. This is called starvation or indefinite postponement.

Slide 6 CS 360, WSU Vancouver Hardware Support Any implementation must ultimately rely on mutual exclusion provided by hardware. All computers offer a basic form of mutual exclusion called storage interlock. It prohibits the concurrent execution of two instructions that access the same main storage location. In the Intel processors: 4 BTS – Bit test and set (80386+) 4 BTR – Bit test and reset (80386+) 4 CMPSWAP – compare swap (80486+)

Slide 7 CS 360, WSU Vancouver Synchronization Hardware Test and set Instruction This technique requires hardware locks, which can be as simple as a single bit Boolean flag. Test and modify the content of a word atomically. boolean TestAndSet(boolean *target) { boolean rv = target; target = true; return rv; }

Slide 8 CS 360, WSU Vancouver Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process P i do { while (TestAndSet (&lock) ) ; /*  “busy” loop */ critical section lock = false; remainder section }

Slide 9 CS 360, WSU Vancouver Mutual Exclusion with Swap Swap Instruction Atomically swap the contents of two words. void Swap(boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp; }

Slide 10 CS 360, WSU Vancouver Mutual Exclusion with Swap Shared data (initialized to false): boolean lock; Process P i do { key = true; while (key == true) Swap(&lock,&key) ; /*  “busy” loop */ critical section lock = false; remainder section }

Slide 11 CS 360, WSU Vancouver Only 2 processes, P 0 and P 1 General structure of process P 0 (other process P j ) do { entry section critical section exit section remainder section } while (1); Processes must share some variables in common to synchronize their actions. Initial Attempts to Solve Problem

Slide 12 CS 360, WSU Vancouver Semaphores Semaphores are primitives used in dealing with n-process critical-section problems or solving various synchronization problems. A semaphore can be defined as a record: typedef struct { int value; struct process *L; } semaphore; 4 The integer variable is a counter which indicates how many instances of the resource are currently available. 4 The list of processes indicates the processes waiting on the semaphore.

Slide 13 CS 360, WSU Vancouver Semaphores can only be accessed via two indivisible (atomic) operations: P, or wait, and V, or signal (proceed). The P and V notation is due to Dijkstra used his native Dutch to get P from verlagen (to try to decrease) and V from verhagen (to increase). In English, P(S) is usually wait() and V(S) is usually signal(S): wait (S): while S  0 do no-op; S--; signal (S): S++;

Slide 14 CS 360, WSU Vancouver Two Types of Semaphores Counting semaphore – integer value can range over an unrestricted domain. Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. Can implement a counting semaphore S as a binary semaphore.

Slide 15 CS 360, WSU Vancouver Mutual Exclusion of n Processes Shared data: semaphore mutex; //initially mutex = 1 Process P i : do { wait(mutex); critical section signal(mutex); remainder section } while (1);

Slide 16 CS 360, WSU Vancouver Semaphore as a General Synchronization Tool Execute B in P j only after A executed in P i Use semaphore flag initialized to 0 Code: P i P j  Await(flag) signal(flag)B

Slide 17 CS 360, WSU Vancouver Classical Problems of Synchronization Bounded Buffer Problem Readers and Writers Problem Dining Philosophers Problem Sleepy Barber Problem

Slide 18 CS 360, WSU Vancouver Bounded Buffer Problem This is a producer/consumer problem The producer creates a product for use by the consumer. They act concurrently, synchronization is required since nothing can be consumed unless it has first been produced. There is a finite amount of resource (memory) in which to store the products produced but not yet consumed.

Slide 19 CS 360, WSU Vancouver Readers and Writers Problem There are two kinds of processes (readers / writers) attempting to use a common resource. No process may use the resource concurrently with a writer. The problem is to coordinate access for the many readers and writers without allowing deadlock or starvation.

Slide 20 CS 360, WSU Vancouver Dining Philosophers Problem Five philosophers (processes) are seated at a round table as in the following figure. Five chopsticks (resources) are distributed as shown Each philosopher alternates between eating and thinking. In order to eat, a philosopher must have possession of the two chopsticks on his immediate left and right. The problem is to synchronize the philosophers' acquisition of the chopsticks to prevent deadlock and indefinite starvation.

Slide 21 CS 360, WSU Vancouver Dining Philosophers Problem Shared data semaphore chopstick[5];// Initially all values are 1

Slide 22 CS 360, WSU Vancouver Sleepy Barber Problem The barber's shop consists of a fixed number of chairs in a waiting room and a separate room holding the barber chair. The barber sleeps when no customers are present. An entering customer acts in the following manner: 4 If all chairs are full, the customer leaves. 4 Otherwise, if the barber is busy, the customer takes a chair. 4 Otherwise, the customer wakes up the barber and sits in the barber chair for a haircut. The goal is to synchronize the actions of the barber and the customers.

Slide 23 CS 360, WSU Vancouver Semaphore Implementation Semaphore operations now defined as wait(S): S.value- -; if (S.value < 0) { add this process to S.L; block this process; } signal(S): S.value++; if (S.value > 0) { S.value--; remove a process P from S.L; wakeup(P); }

Slide 24 CS 360, WSU Vancouver Deadlock and Starvation Deadlock 4 Example: Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(S);wait(Q); wait(Q);wait(S);  signal(S);signal(Q); signal(Q)signal(S); Starvation 4 Example: A process may never be removed from the semaphore queue in which it is suspended.

Slide 25 CS 360, WSU Vancouver Bounded-Buffer Problem Shared data semaphore full, empty, mutex; /* define three semaphores */ Initially: full = 0, empty = n, mutex = 1

Slide 26 CS 360, WSU Vancouver Bounded-Buffer Problem Producer Process do { … produce an item in nextp … wait(empty); wait(mutex); … add nextp to buffer … signal(mutex); signal(full); } while (1);

Slide 27 CS 360, WSU Vancouver Bounded-Buffer Problem Consumer Process do { wait(full) wait(mutex); … remove an item from buffer to nextc … signal(mutex); signal(empty); … consume the item in nextc … } while (1);

Slide 28 CS 360, WSU Vancouver Readers-Writers Problem Shared data semaphore mutex, wrt; integer readcount; Initially mutex = 1, wrt = 1, readcount = 0

Slide 29 CS 360, WSU Vancouver Readers-Writers Problem: Writer Process wait(wrt); … writing is performed … signal(wrt);

Slide 30 CS 360, WSU Vancouver Readers-Writers Problem: Reader Process wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex);