Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.

Slides:



Advertisements
Similar presentations
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Advertisements

Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
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.
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.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
BASIC PRINCIPLES OF SYNCHRONISATION. MAIN CONCEPTS  SYNCHRONISATION  CRITICAL SECTION  DEAD LOCK.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
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.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
Slide 8-1 Copyright © 2004 Pearson Education, Inc. Basic Synchronization Principles.
Basic Synchronization Principles. Concurrency Value of concurrency – speed & economics But few widely-accepted concurrent programming languages (Java.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
© 2004, D. J. Foreman 1 Mutual Exclusion © 2004, D. J. Foreman 2  Mutual exclusion ■ Critical sections ■ Primitives  Implementing it  Dekker's alg.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Classical Synchronization Problems. Announcements CS 414 grades and solutions available in CMS soon. –Average 74.3 –High of 95. –Score out of 100 pts.
1 Lecture : Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2008.
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.
Dining Philosophers, Monitors, and Condition Variables CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Concurrency, Mutual Exclusion and Synchronization.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
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.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1.  Independent process  Cannot affect or be affected by the other processes in the system  Does not share any data with other processes  Interacting.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
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.
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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Basic Synchronization Principles 1. 2  Independent process  Cannot affect or be affected by the other processes in the system  Does not share any.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Concurrency: Mutual Exclusion and Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
More on Classic Synchronization Problems, Monitors, and Deadlock
Chapter 5: Process Synchronization
Basic Synchronization Principles
143a discussion session week 3
Basic Synchronization Principles
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Steve’s Concurrency Slides
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
CSE 153 Design of Operating Systems Winter 2019
Outline Announcements Basic Synchronization Principles – continued
Chapter 6: Synchronization Tools
Outline Please turn in Homework #2 Announcements
Steve’s Concurrency Slides
Process/Thread Synchronization (Part 2)
Presentation transcript:

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-2 Copyright © 2004 Pearson Education, Inc. Basic Synchronization Principles 8

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-3 Copyright © 2004 Pearson Education, Inc. Concurrency Value of concurrency – speed & economics But few widely-accepted concurrent programming languages (Java is an exception) Few concurrent programming paradigm –Each problem requires careful consideration –There is no common model OS tools to support concurrency tend to be “low level”

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-4 Copyright © 2004 Pearson Education, Inc. Command Execution Another Command? Execute Command No Exit Loop Yes Enter Loop Another Command? No Exit Loop Yes Enter Loop Wait for Child to Terminate Execute Command Execute Command … (a) UNIX Shell (b) Windows Command Launch fork() code CreateProcess() code

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-5 Copyright © 2004 Pearson Education, Inc. Synchronizing Multiple Threads with a Shared Variable … Wait runTime seconds Initialize CreateThread(…) runFlag=FALSE Terminate Thread Work Exit runFlag? TRUE FALSE TRUE FALSE TRUE FALSE

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-6 Copyright © 2004 Pearson Education, Inc. Traffic Intersections

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-7 Copyright © 2004 Pearson Education, Inc. Critical Sections shared double balance; Code for p 1 Code for p balance = balance + amount;balance = balance - amount;... balance+=amountbalance-=amount balance

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-8 Copyright © 2004 Pearson Education, Inc. Critical Sections (2) … load R1, balance load R2, amount … load R1, balance load R2, amount sub R1, R2 store R1, balance … add R1, R2 store R1, balance … Timer interrupt Execution of p 1 Execution of p 2

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-9 Copyright © 2004 Pearson Education, Inc. Critical Sections (3) Mutual exclusion: Only one process can be in the critical section at a time There is a race to execute critical sections The sections may be defined by different code in different processes –  cannot easily detect with static analysis Without mutual exclusion, results of multiple execution are not determinate Need an OS mechanism so programmer can resolve races

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-10 Copyright © 2004 Pearson Education, Inc. Some Possible Solutions Disable interrupts Software solution – locks Transactions FORK(), JOIN(), and QUIT() [Chapter 2] –Terminate processes with QUIT() to synchronize –Create processes whenever critical section is complete –See Figure 8.7 … something new …

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-11 Copyright © 2004 Pearson Education, Inc. Disabling Interrupts shared double balance; Code for p 1 Code for p 2disableInterrupts(); balance = balance + amount;balance = balance - amount;enableInterrupts(); Interrupts could be disabled arbitrarily long Really only want to prevent p 1 and p 2 from interfering with one another; this blocks all p i Try using a shared “lock” variable

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-12 Copyright © 2004 Pearson Education, Inc. Using a Lock Variable shared boolean lock = FALSE; shared double balance; Code for p 1 Code for p 2/* Acquire the lock */ L1: while(lock) ;L5: while(lock) ; L2: lock = TRUE;L6: lock = TRUE;/* Execute critical sect */ L3: balance = balance + amount;L7: balance = balance - amount;/* Release lock */ L4: lock = FALSE;L8: lock = FALSE;

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-13 Copyright © 2004 Pearson Education, Inc. Busy Wait Condition shared boolean lock = FALSE; shared double balance; Code for p 1 Code for p 2/* Acquire the lock */ while(lock) ; lock = TRUE;/* Execute critical sect */ balance = balance + amount; balance = balance - amount;/* Release lock */ lock = FALSE; p1p1 p2p2 Blocked at while lock = TRUE lock = FALSE Interrupt

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-14 Copyright © 2004 Pearson Education, Inc. Unsafe “Solution” shared boolean lock = FALSE; shared double balance; Code for p 1 Code for p 2/* Acquire the lock */ while(lock) ; lock = TRUE;/* Execute critical sect */ balance = balance + amount; balance = balance - amount;/* Release lock */ lock = FALSE; Worse yet … another race condition … Is it possible to solve the problem?

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-15 Copyright © 2004 Pearson Education, Inc. Atomic Lock Manipulation enter(lock) {exit(lock) { L1: disableInterrupts();L7: disableInterrupts(); /* Loop until lock is TRUE */L8: lock = FALSE; L2: while(lock) {L9: enableInterrupts(); /* Let interrupts occur */} L3: enableInterrupts(); L4: disableInterrupts(); } L5: lock = TRUE; L6: enableInterrupts(); } Bound the amount of time that interrupts are disabled Can include other code to check that it is OK to assign a lock … but this is still overkill …

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-16 Copyright © 2004 Pearson Education, Inc. Fig 8-9: Deadlocked Pirates

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-17 Copyright © 2004 Pearson Education, Inc. Deadlock (2) shared boolean lock1 = FALSE; shared boolean lock2 = FALSE; shared list L; Code for p 1 Code for p 2... /* Enter CS to delete elt *//* Enter CS to update len */ L1: enter(lock1);L8: enter(lock2); L2: ; L9: ; L3: ;L10: /* Enter CS to update len *//* Enter CS to add elt */ L4: enter(lock2); L11: enter(lock1); L5: ;L12: ;/* Exit both CS */ L6: exit(lock1);L13: exit(lock2); L7: exit(lock2);L14: exit(lock1);...

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-18 Copyright © 2004 Pearson Education, Inc. Processing Two Components shared boolean lock1 = FALSE; shared boolean lock2 = FALSE; shared list L; Code for p 1 Code for p 2... /* Enter CS to delete elt *//* Enter CS to update len */ enter(lock1); enter(lock2); ; ;/* Exit CS */ exit(lock1); exit(lock2); ; /* Enter CS to update len *//* Enter CS to add elt */ enter(lock2); enter(lock1); ; ;/* Exit CS */ exit(lock2); exit(lock1);...

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-19 Copyright © 2004 Pearson Education, Inc. Transactions A transaction is a list of operations –When the system begins to execute the list, it must execute all of them without interruption, or –It must not execute any at all Example: List manipulator –Add or delete an element from a list –Adjust the list descriptor, e.g., length Too heavyweight – need something simpler

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-20 Copyright © 2004 Pearson Education, Inc. A Semaphore

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-21 Copyright © 2004 Pearson Education, Inc. Dijkstra Semaphore Invented in the 1960s Conceptual OS mechanism, with no specific implementation defined (could be enter() / exit() ) Basis of all contemporary OS synchronization mechanisms

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-22 Copyright © 2004 Pearson Education, Inc. Solution Constraints Processes p 0 & p 1 enter critical sections Mutual exclusion: Only one process at a time in the CS Only processes competing for a CS are involved in resolving who enters the CS Once a process attempts to enter its CS, it cannot be postponed indefinitely After requesting entry, only a bounded number of other processes may enter before the requesting process

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-23 Copyright © 2004 Pearson Education, Inc. Notation Let fork(proc, N, arg 1, arg 2, …, arg N ) be a command to create a process, and to have it execute using the given N arguments Canonical problem: Proc_0() {proc_1() { while(TRUE) { while(TRUE { <compute section>; <critical section>; }} fork(proc_0, 0); fork(proc_1, 0);

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-24 Copyright © 2004 Pearson Education, Inc. Solution Assumptions Memory read/writes are indivisible (simultaneous attempts result in some arbitrary order of access) There is no priority among the processes Relative speeds of the processes/processors is unknown Processes are cyclic and sequential

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-25 Copyright © 2004 Pearson Education, Inc. Dijkstra Semaphore Definition V(s): [s = s + 1] P(s): [while(s == 0) {wait}; s = s - 1] Classic paper describes several software attempts to solve the problem (see problem 4, Chapter 8) Found a software solution, but then proposed a simpler hardware-based solution A semaphore, s, is a nonnegative integer variable that can only be changed or tested by these two indivisible functions:

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-26 Copyright © 2004 Pearson Education, Inc. Solving the Canonical Problem Proc_0() {proc_1() { while(TRUE) { while(TRUE { <compute section>; P(mutex); P(mutex); <critical section>; V(mutex); V(mutex); }} semaphore mutex = 1; fork(proc_0, 0); fork(proc_1, 0);

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-27 Copyright © 2004 Pearson Education, Inc. Shared Account Balance Problem Proc_0() {proc_1() {.../* Enter the CS */ P(mutex); P(mutex); balance += amount; balance -= amount; V(mutex); V(mutex);...} semaphore mutex = 1; fork(proc_0, 0); fork(proc_1, 0);

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-28 Copyright © 2004 Pearson Education, Inc. Sharing Two Variables proc_A() { L1: while(TRUE) { L2: ; L3: update(x); /* Signal proc_B */ L4: V(s1); L5: ; /* Wait for proc_B */ L6: P(s2); L7: retrieve(y); } semaphore s1 = 0; semaphore s2 = 0; fork(proc_A, 0); fork(proc_B, 0); proc_B() { L8: while(TRUE) { /* Wait for proc_A */ L9: P(s1); L10: retrieve(x); L11: ; L12: update(y); /* Signal proc_A */ L13: V(s2); L14: ; }

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-29 Copyright © 2004 Pearson Education, Inc. Device Controller Synchronization The semaphore principle is logically used with the busy and done flags in a controller Driver signals controller with a V(busy), then waits for completion with P(done) Controller waits for work with P(busy), then announces completion with V(done) Check Figure 8.17 of the textbook

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-30 Copyright © 2004 Pearson Education, Inc. Bounded Buffer Problem Producer Consumer Empty Pool Full Pool

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-31 Copyright © 2004 Pearson Education, Inc. Bounded Buffer Problem (3) producer() { buf_type *next, *here; L1 while(TRUE) { L2 produce_item(next); /* Claim an empty */ L3 P(empty); L4 P(mutex); L5 here = obtain(empty); L6 V(mutex); L7 copy_buffer(next, here); L8 P(mutex); L9 release(here, fullPool); L10 V(mutex); /* Signal a full buffer */ L11 V(full); } semaphore mutex = 1; semaphore full = 0; /* A general (counting) semaphore */ semaphore empty = N; /* A general (counting) semaphore */ buf_type buffer[N]; fork(producer, 0); fork(consumer, 0); consumer() { buf_type *next, *here; L12 while(TRUE) { /* Claim full buffer */ L13 P(full); L14 P(mutex); L15 here = obtain(full); L16 V(mutex); L17 copy_buffer(here, next); L18 P(mutex); L19 release(here, emptyPool); L20 V(mutex); /* Signal an empty buffer */ L21 V(empty); L22 consume_item(next); }

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-32 Copyright © 2004 Pearson Education, Inc. Readers-Writers Problem Readers Writers

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-33 Copyright © 2004 Pearson Education, Inc. Readers-Writers Problem (2) Reader Shared Resource Reader Writer

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-34 Copyright © 2004 Pearson Education, Inc. Readers-Writers Problem (3) Reader Shared Resource Reader Writer

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-35 Copyright © 2004 Pearson Education, Inc. Readers-Writers Problem (4) Reader Shared Resource Reader Writer

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-36 Copyright © 2004 Pearson Education, Inc. First Solution reader() { L1 while(TRUE) { L2 ; L3 P(mutex); L4 readCount++; L5 if(readCount == 1) L6 P(writeBlock); L7 V(mutex); /* Critical section */ L8 access(resource); L9 P(mutex); L10 readCount--; L11 if(readCount == 0) L12 V(writeBlock); L13 V(mutex); } resourceType *resource; int readCount = 0; semaphore mutex = 1; semaphore writeBlock = 1; fork(reader, 0); fork(writer, 0); writer() { L14 while(TRUE) { L15 ; L16 P(writeBlock); /* Critical section */ L17 access(resource); L18 V(writeBlock); } First reader competes with writers Last reader signals writers

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-37 Copyright © 2004 Pearson Education, Inc. First Solution (2) reader() { while(TRUE) { ; P(mutex); readCount++; if(readCount == 1) P(writeBlock); V(mutex); /* Critical section */ access(resource); P(mutex); readCount--; if(readCount == 0) V(writeBlock); V(mutex); } resourceType *resource; int readCount = 0; semaphore mutex = 1; semaphore writeBlock = 1; fork(reader, 0); fork(writer, 0); writer() { while(TRUE) { ; P(writeBlock); /* Critical section */ access(resource); V(writeBlock); } First reader competes with writers Last reader signals writers Any writer must wait for all readers Readers can starve writers “Updates” can be delayed forever May not be what we want

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-38 Copyright © 2004 Pearson Education, Inc. Implementing Semaphores Minimize effect on the I/O system Processes are only blocked on their own critical sections (not critical sections that they should not care about) If disabling interrupts, be sure to bound the time they are disabled

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-39 Copyright © 2004 Pearson Education, Inc. Implementing Semaphores: enter() & exit() class semaphore { int value; public: semaphore(int v = 1) { value = v;}; P(){ disableInterrupts(); while(value == 0) { enableInterrupts(); disableInterrupts(); } value--; enableInterrupts(); }; V(){ disableInterrupts(); value++; enableInterrupts(); };

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-40 Copyright © 2004 Pearson Education, Inc. Implementing Semaphores: Test and Set Instruction FALSE m Primary Memory … R3 … Data Register CC Register (a)Before Executing TS TRUE m Primary Memory FALSE R3 =0 Data Register CC Register (b) After Executing TS TS(m): [Reg_i = memory[m]; memory[m] = TRUE;]

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-41 Copyright © 2004 Pearson Education, Inc. Using the TS Instruction boolean s = FALSE;... while(TS(s)) ; s = FALSE;... semaphore s = 1;... P(s) ; V(s);...

Operating Systems: A Modern Perspective, Chapter 8 Slide 8-42 Copyright © 2004 Pearson Education, Inc. Implementing the General Semaphore struct semaphore { int value = ; boolean mutex = FALSE; boolean hold = TRUE; }; shared struct semaphore s; P(struct semaphore s) { L1 while(TS(s.mutex)) ; L2 s.value--; L3 if(s.value < 0) ( L4 s.mutex = FALSE; L5 while(TS(s.hold)) ; } else L6 s.mutex = FALSE; } V(struct semaphore s) { L7 while(TS(s.mutex)) ; L8 s.value++; L9 if(s.value <= 0) ( L10 while(!s.hold) ; L11 s.hold = FALSE; } L12 s.mutex = FALSE; }