Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.

Slides:



Advertisements
Similar presentations
Module 6: Process Synchronization
Advertisements

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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Dr. Kalpakis CMSC 421, Operating Systems. Fall Process Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Background Shared-memory solution to bounded-buffer problem allows at most n – 1 items.
Synchronization Principles Gordon College Stephen Brinton.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Chapter 7: Process Synchronization
Process Synchronization
CIS 415 Process Synchronization Background The Critical-Section Problem Software Solutions Semaphores Hardware Support Classical Problems of Synchronization.
1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
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.
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.
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.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
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.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
7.1 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Process Synchronization 1 10CSE Process Synchronization With Permission and Copyrights Lecture Slides adapted from “Principles of Operating Systems”, Lecture.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
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 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of 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.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Chapter 5: Process Synchronization
Chapter 7: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Part II: Process Management Chapter 7 Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Chapter 5: Process Synchronization
Lecture 2 Part 2 Process Synchronization
Presentation transcript:

Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors

Background Concurrent access to Data may result in data inconsistency To maintain data consistency we should have orderly execution of co-operating processes In the bounded buffer example, what happens I we maintain a counter to keep track of number of items produced common to producer and consumer Concurrent access to Data may result in data inconsistency To maintain data consistency we should have orderly execution of co-operating processes In the bounded buffer example, what happens I we maintain a counter to keep track of number of items produced common to producer and consumer

Bounded Buffer Shared Data: buffer[n],in,out,counter Producer Process: repeat produce an item in nextp while counter==n wait buffer[in]=nextp;in=(in+1)%n;counter++; until false Shared Data: buffer[n],in,out,counter Producer Process: repeat produce an item in nextp while counter==n wait buffer[in]=nextp;in=(in+1)%n;counter++; until false

Bounded Buffer (continued) Consumer Process repeat while counter==0 do no-op nextc=buffer[out]; counter--; consume item nextc until false Statements counter++ and counter-- have to be done atomically - no interrupt Consumer Process repeat while counter==0 do no-op nextc=buffer[out]; counter--; consume item nextc until false Statements counter++ and counter-- have to be done atomically - no interrupt

Critical Section Problem n processes competing to use shared data Each process has a code segment called critical section in which shared data is accessed Issue: When one process is in critical section, no other process is in critical section n processes competing to use shared data Each process has a code segment called critical section in which shared data is accessed Issue: When one process is in critical section, no other process is in critical section

Structure of Process repeat Critical section Remainder section until false Remainder section until false

Solution to Critical Section Problem 1. Mutual Exclusion: No two processes are in the critical section at the same time. 2. Progress: If no process is in the critical section and a process wishes to enter, that process 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 cs and that process is granted. 1. Mutual Exclusion: No two processes are in the critical section at the same time. 2. Progress: If no process is in the critical section and a process wishes to enter, that process 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 cs and that process is granted.

Initial Attempts to solve Problem Only 2 Processes P_0 and P_1 General Structure of Process P_j (other is P_i) repeat entry section; critical section; exit section;remainder section; until false; Processes may share some common variables to synchronize their actions. Only 2 Processes P_0 and P_1 General Structure of Process P_j (other is P_i) repeat entry section; critical section; exit section;remainder section; until false; Processes may share some common variables to synchronize their actions.

Algorithm 1 Shared variables: int turn /* turn=j it P_jth turn*/ Process P_j repeat while (turn <> j) wait critical section; turn=I; remainder section; umtil false Shared variables: int turn /* turn=j it P_jth turn*/ Process P_j repeat while (turn <> j) wait critical section; turn=I; remainder section; umtil false

Algorithm 2 Shared variables: boolean flag[2] /*flag[j]=true means jth turn */ Process P_j entry section: flag[j]=true; while (flag[i]) wait; exit section: flag[j]=false; As before satisfies mutual exclusion but no progress. Shared variables: boolean flag[2] /*flag[j]=true means jth turn */ Process P_j entry section: flag[j]=true; while (flag[i]) wait; exit section: flag[j]=false; As before satisfies mutual exclusion but no progress.

Algorithm 3 Shared variables: flag[2], turn; Process P_j entry section: flag[j]=true; turn=j; while (flag[i] and turn==i) wait; exit section: flag[j]=false; Satisfies all three requirements- critical thing to note is turn is a shared variable. Shared variables: flag[2], turn; Process P_j entry section: flag[j]=true; turn=j; while (flag[i] and turn==i) wait; exit section: flag[j]=false; Satisfies all three requirements- critical thing to note is turn is a shared variable.

Bakery Algorithm Critical Section for n processors Before entering the critical section, process receives a number. Holder of the smallest number enters its critical section. If processes P_i and P_j receive the same number, if i<j, then P_i is served first else p_j is served. Numbering scheme always generates numbers in non-decreasing order. Critical Section for n processors Before entering the critical section, process receives a number. Holder of the smallest number enters its critical section. If processes P_i and P_j receive the same number, if i<j, then P_i is served first else p_j is served. Numbering scheme always generates numbers in non-decreasing order.

Bakery Algorithm (continued) We use lexicographic order. (a,b) < (c,d) is either a< c or a=c b< d. Shared data: Boolean choosing[n] int number[n] they are initialized to false and 0 resply. We use lexicographic order. (a,b) < (c,d) is either a< c or a=c b< d. Shared data: Boolean choosing[n] int number[n] they are initialized to false and 0 resply.

Bakery Algorithm (continued) Enter section: choosing[j] = true; number[j]=max of numbers +1 choosing[j]=false; for (k=0;k<n;k++) { while(choosing[k]) wait; while(number[k] <>0) and (number[k],k) <number[j],j) wait; }Leave Section: number[j]=0; Enter section: choosing[j] = true; number[j]=max of numbers +1 choosing[j]=false; for (k=0;k<n;k++) { while(choosing[k]) wait; while(number[k] <>0) and (number[k],k) <number[j],j) wait; }Leave Section: number[j]=0;

Synchronization Hardware Test and modify the content of a word atomically boolean Test-and-set(boolean target){ Test-and-set = target; target = true; } Test and modify the content of a word atomically boolean Test-and-set(boolean target){ Test-and-set = target; target = true; }

Mutual Exclusion with Test- and-set Shared data: boolean lock (initially false) Process P_j: enter section: while (Test-and-lock) wait; leave section: lock = false; Shared data: boolean lock (initially false) Process P_j: enter section: while (Test-and-lock) wait; leave section: lock = false;

Semaphore Synchronization tool that does not require busy wait Semaphore S - integer variable can only be accessed via two indivisible atomic operations wait(s): while (s <=0) no-op; s = s-1; signal(s): s=s+1; Synchronization tool that does not require busy wait Semaphore S - integer variable can only be accessed via two indivisible atomic operations wait(s): while (s <=0) no-op; s = s-1; signal(s): s=s+1;

Critical section for n processes Shared Variable: semaphore mutex; initial value of mutex=1; entersection: wait(mutex); leavesection: signal(mutex); Shared Variable: semaphore mutex; initial value of mutex=1; entersection: wait(mutex); leavesection: signal(mutex);

Semaphore Implementation Semaphore as a struct typedef semaphore struct { int value; ProcessList L } Two Simple operations: block - suspends the process that invokes it wakeup(P) resumes the execution of a blocked process P Semaphore as a struct typedef semaphore struct { int value; ProcessList L } Two Simple operations: block - suspends the process that invokes it wakeup(P) resumes the execution of a blocked process P

Semaphore Implementation (continued) Wait(S) : S.value--; if (S.value<0) { add this process to S.l; block;} Signal(S): S.value++; if (S.value<=0) {remove a process from P from S.L; wakeup(P);} Wait(S) : S.value--; if (S.value<0) { add this process to S.l; block;} Signal(S): S.value++; if (S.value<=0) {remove a process from P from S.L; wakeup(P);}

Implementation (continued) Wait(S) : S.value--; if (S.value<0) { add this process to S.l; block;} Signal(S): S.value++; if (S.value<=0) {remove a process from P from S.L; wakeup(P);} Wait(S) : S.value--; if (S.value<0) { add this process to S.l; block;} Signal(S): S.value++; if (S.value<=0) {remove a process from P from S.L; wakeup(P);}

Semaphore as General Synchronization Tool Execute B in P_j only after A is executed in P_i Use semaphore flag initialized to 0 code P_i: A ; signal(flag) Code P_j: wait(flag); B; Execute B in P_j only after A is executed in P_i Use semaphore flag initialized to 0 code P_i: A ; signal(flag) Code P_j: wait(flag); B;

Deadlock and Starvation Deadlock - two or more processes are waiting for indefinitely for an event that can be caused by only one of the waiting processes. S and Q are semaphores. P_0: wait(S);wait(Q);..signal(S);signal(Q); P_1:wait(Q);wait(S);..;signal(Q);signal(S); Starvation: A process may never be removed from the semaphore queue. Deadlock - two or more processes are waiting for indefinitely for an event that can be caused by only one of the waiting processes. S and Q are semaphores. P_0: wait(S);wait(Q);..signal(S);signal(Q); P_1:wait(Q);wait(S);..;signal(Q);signal(S); Starvation: A process may never be removed from the semaphore queue.

Two Types of Semaphores Counting Semaphore - integer value can range over an unrestricted domain Binary Semaphore - integer value can range from 0 to 1 - easier to implement. Can implement (simulate) a counting semaphore using a binary semaphore. Counting Semaphore - integer value can range over an unrestricted domain Binary Semaphore - integer value can range from 0 to 1 - easier to implement. Can implement (simulate) a counting semaphore using a binary semaphore.

Implementing S as a binary Semaphore Data Structures: binary_semaphore s1,s2,s3; int c; Initialization s3=s1=1; s2=0; c=initial value of semaphore Data Structures: binary_semaphore s1,s2,s3; int c; Initialization s3=s1=1; s2=0; c=initial value of semaphore

Implementing S (continued) Wait operation wait(s3);wait(s1);c=c-1; if (c<0) {signal(s1);wait(s2); else {signal(s1);} signal(s3); Signal Operation: wait(s1); c=c+1; if (c<=0) {signal(s2);} signal(s1); Wait operation wait(s3);wait(s1);c=c-1; if (c<0) {signal(s1);wait(s2); else {signal(s1);} signal(s3); Signal Operation: wait(s1); c=c+1; if (c<=0) {signal(s2);} signal(s1);

Classical Problems of Synchronization Bounded Buffer Problem Readers and writers Problem Dining Philosopher Bounded Buffer Problem Readers and writers Problem Dining Philosopher

Bounded Buffer Problem Shared Data String Item; String buffer[]; semaphore full,empty,mutex; item nextp,nextc; full=0;empty=n;mutex=1 Shared Data String Item; String buffer[]; semaphore full,empty,mutex; item nextp,nextc; full=0;empty=n;mutex=1

Producer Process Repeat { produce an item in nextp; wait(empty);wait(mutex); add nextp to buffer; signal(mutex); signal(full); } until false; Repeat { produce an item in nextp; wait(empty);wait(mutex); add nextp to buffer; signal(mutex); signal(full); } until false;

Consumer Process Repeat { wait(full);wait(mutex); remove an item from buffer in nextc; signal(mutex); signal(empty); consume an item in nextc; } until false; Repeat { wait(full);wait(mutex); remove an item from buffer in nextc; signal(mutex); signal(empty); consume an item in nextc; } until false;

Readers and Writers Problem Shared Data: Semaphore mutex,wrt;(=1) int readcount; Writer Process: wait(wrt); write; signa(wrt); Shared Data: Semaphore mutex,wrt;(=1) int readcount; Writer Process: wait(wrt); write; signa(wrt);

Reader Process Entersection: wait(mutex); readcount++; if (readcount==1) wait(wrt); signal(mutex); leavesection: wait(mutex);readcount-; if(reacount==0) signal(wrt); signal(mutex); Entersection: wait(mutex); readcount++; if (readcount==1) wait(wrt); signal(mutex); leavesection: wait(mutex);readcount-; if(reacount==0) signal(wrt); signal(mutex);

Dining Philosopher Problem Shared data: Semaphore chopstick[5](=1); Philosopher_j entersection: wait(chopstick[j]); wait(chopstick[(j+1)%5]); Leavesection: signal(chopstick[j]); signal(chopstick[(j+1)%5]); Shared data: Semaphore chopstick[5](=1); Philosopher_j entersection: wait(chopstick[j]); wait(chopstick[(j+1)%5]); Leavesection: signal(chopstick[j]); signal(chopstick[(j+1)%5]);