Introduction to Synchronization CS-3013 C-term 20081 Introduction to Synchronization CS-3013 Operating Systems (Slides include materials from Operating.

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
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.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013 Operating Systems (Slides include materials from Modern Operating.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013, Operating Systems A-term 2009 (Slides include materials from.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
ConcurrencyCS-3013 A-term Introduction to Concurrency ( Processes, Threads, Interrupts, etc.) CS-3013, Operating Systems A-term 2009 (Slides include.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
Concurrency & Synchronization CS-502 Fall Concurrency (continued) and Synchronization CS-502 Operating Systems Fall 2007 (Slides include materials.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
ConcurrencyCS-502 Fall Concurrency CS-502 Operating Systems Fall 2007 (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz,
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Introduction to Concurrency CS-502 (EMC) Fall Introduction to Concurrency ( Processes, Threads, Interrupts, etc.) CS-502, Operating Systems Fall.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
CS 3013 & CS 502 Summer 2006 Concurrency & Processes1 CS3013 & CS502 Operating Systems.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
CS703 – Advanced Operating Systems
Chapter 5: Process Synchronization
Processes in Unix, Linux, and Windows
Chapter 5: Process Synchronization
Processes in Unix, Linux, and Windows
Concurrency CS-3013 Operating Systems C-Term 2008 Assumptions:
Processes in Unix, Linux, and Windows
Topic 6 (Textbook - Chapter 5) Process Synchronization
Jonathan Walpole Computer Science Portland State University
Lecture 2 Part 2 Process Synchronization
Critical section problem
Chapter 6: Process Synchronization
Introduction to Synchronization
Introduction to Synchronization
Processes in Unix, Linux, and Windows
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Concurrency & Processes
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Introduction to Synchronization CS-3013 C-term Introduction to Synchronization CS-3013 Operating Systems (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne and from Modern Operating Systems, 2 nd ed., by Tanenbaum)

Introduction to Synchronization CS-3013 C-term Challenge How can we help processes synchronize with each other? E.g., how does one process “know” that another has completed a particular action? E.g, how do separate processes “keep out of each others’ way” with respect to some shared resource E.g., how do process share the load of particularly long computations

Introduction to Synchronization CS-3013 C-term Digression (thought experiment) static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return y; }

Introduction to Synchronization CS-3013 C-term Digression (thought experiment) static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return y; } Upon completion of main, y == 1

Introduction to Synchronization CS-3013 C-term Process 1 int main(int argc, char **argv) { extern int y; y = y + 1; return y; } Process 2 int main2(int argc, char **argv) { extern int y; y = y - 1; return y; } Assuming processes run “at the same time,” what are possible values of y after both terminate? Thought experiment (continued) static int y = 0;

Introduction to Synchronization CS-3013 C-term Another Abstraction – Atomic Operation An operation that either happens entirely or not at all No partial result is visible or apparent Appears to be non-interruptible If two atomic operations happen “at the same time” Effect is as if one is first and the other is second (Usually) don’t know which is which

Introduction to Synchronization CS-3013 C-term Hardware Atomic Operations On (nearly) all computers, reading and writing operations of machine words can be considered as atomic Non-interruptible It either happens or it doesn’t Not in conflict with any other operation When two attempts to read or write the same data, one is first and the other is second Don’t know which is which! No other guarantees (unless we take extraordinary measures)

Introduction to Synchronization CS-3013 C-term Definitions Definition: race condition When two or more concurrent activities are trying to do something with the same variable resulting in different values Random outcome Critical Region (aka critical section) One or more fragments of code that operate on the same data, such that at most one activity at a time may be permitted to execute anywhere in that set of fragments.

Introduction to Synchronization CS-3013 C-term Synchronization – Critical Regions

Introduction to Synchronization CS-3013 C-term Class Discussion How do we keep multiple computations from being in a critical region at the same time? Especially when number of computations is > 2 Remembering that read and write operations are atomic example

Introduction to Synchronization CS-3013 C-term Possible ways to protect critical section Without OS assistance — Locking variables & busy waiting –Peterson’s solution (p ) –Atomic read-modify-write – e.g. Test & Set With OS assistance — abstract synchronization operations Single processor Multiple processors

Introduction to Synchronization CS-3013 C-term Requirements – Controlling Access to a Critical Section –Only one computation in critical section at a time –Symmetrical among n computations –No assumption about relative speeds –A stoppage outside critical section does not lead to potential blocking of others –No starvation — i.e. no combination of timings that could cause a computation to wait forever to enter its critical section

Introduction to Synchronization CS-3013 C-term Non-solution static int turn = 0; Process 1 while (TRUE) { while (turn !=0) /*loop*/; critical_region(); turn = 1; noncritical_region1() ; }; Process 2 while (TRUE) { while (turn !=1) /*loop*/; critical_region(); turn = 0; noncritical_region2() ; };

Introduction to Synchronization CS-3013 C-term Non-solution static int turn = 0; Process 1 while (TRUE) { while (turn !=0) /*loop*/; critical_region(); turn = 1; noncritical_region1() ; }; Process 2 while (TRUE) { while (turn !=1) /*loop*/; critical_region(); turn = 0; noncritical_region2() ; }; What is wrong with this approach?

Introduction to Synchronization CS-3013 C-term Peterson’s solution (2 processes) static int turn = 0; static int interested[2]; void enter_region(int process) { int other = 1 - process; interested[process] = TRUE; turn = process; while (turn == process && interested[other] == TRUE) /*loop*/; }; void leave_region(int process) { interested[process] = FALSE; };

Introduction to Synchronization CS-3013 C-term Another approach: Test & Set Instruction (built into CPU hardware) static int lock = 0; extern int TestAndSet(int &i); /* sets the value of i to 1 and returns the previous value of i. */ void enter_region(int &lock) { while (TestAndSet(lock) == 1) /* loop */ ; }; void leave_region(int &lock) { lock = 0; };

Introduction to Synchronization CS-3013 C-term Test & Set Instruction (built into CPU hardware) static int lock = 0; extern int TestAndSet(int &i); /* sets the value of i to 1 and returns the previous value of i. */ void enter_region(int &lock) { while (TestAndSet(lock) == 1) /* loop */ ; }; void leave_region(int &lock) { lock = 0; }; What about this solution?

Introduction to Synchronization CS-3013 C-term Variations Compare & Swap (a, b) temp = b b = a a = temp return(a == b) … A whole mathematical theory about efficacy of these operations All require extraordinary circuitry in processor memory, and bus to implement atomically

Introduction to Synchronization CS-3013 C-term Protecting a Critical Section with OS Assistance Implement an abstraction:– –A data type called semaphore Non-negative integer values. –An operation wait_s(semaphore &s) such that if s > 0, atomically decrement s and proceed. if s = 0, block the computation until some other computation executes post_s(s). –An operation post_s(semaphore &s):– If one or more computations are blocked on s, allow precisely one of them to unblock and proceed. Otherwise, atomically increment s and continue

Introduction to Synchronization CS-3013 C-term Critical Section control with Semaphore static semaphore mutex = 1; Process 1 while (TRUE) { wait_s(mutex); critical_region(); post_s(mutex); noncritical_region1(); }; Process 2 while (TRUE) { wait_s(mutex); critical_region(); post_s(mutex); noncritical_region2(); };

Introduction to Synchronization CS-3013 C-term Critical Section control with Semaphore static semaphore mutex = 1; Process 1 while (TRUE) { wait_s(mutex); critical_region(); post_s(mutex); noncritical_region1(); }; Process 2 while (TRUE) { wait_s(mutex); critical_region(); post_s(mutex); noncritical_region2(); }; Does this meet the requirements for controlling access to critical sections?

Introduction to Synchronization CS-3013 C-term Semaphores – History Introduced by E. Dijkstra in wait_s() was called P() Initial letter of a Dutch word meaning “test” post_s() was called V() Initial letter of a Dutch word meaning “increase”

Introduction to Synchronization CS-3013 C-term Semaphores – History Introduced by E. Dijkstra in wait_s() was called P() Initial letter of a Dutch word meaning “test” post_s() was called V() Initial letter of a Dutch word meaning “increase” In Linux kernel (and other modern systems) wait_s() is called down post_s() is called up

Introduction to Synchronization CS-3013 C-term Abstractions The semaphore is an example of a powerful abstraction defined by OS I.e., a data type and some operations that add a capability that was not in the underlying hardware or system. Any program can use this abstraction to control critical sections and to create more powerful forms of synchronization among computations.

Introduction to Synchronization CS-3013 C-term Process and semaphore data structures class State { long int PSW; long int regs[R]; /*other stuff*/ } class PCB { PCB *next, prev, queue; State s; PCB (…); /*constructor*/ ~PCB(); /*destructor*/ } class Semaphore { int count; PCB *queue; friend wait_s(Semaphore &s); friend post_s(Semaphore &s); Semaphore(int initial); /*constructor*/ ~Semaphore(); /*destructor*/ }

Introduction to Synchronization CS-3013 C-term Implementation Ready queue PCB Semaphore A count = 0 PCB Semaphore B count = 2

Introduction to Synchronization CS-3013 C-term Implementation Action – dispatch a process to CPU Remove first PCB from ReadyQueue Load registers and PSW Return from interrupt or trap Action – interrupt a process Save PSW and registers in PCB If not blocked, insert PCB into ReadyQueue (in some order) Take appropriate action Dispatch same or another process from ReadyQueue Ready queue PCB

Introduction to Synchronization CS-3013 C-term Implementation – Semaphore actions Action – wait_s(Semaphore &s) Implement as a Trap (with interrupts disabled) if (s.count == 0) –Save registers and PSW in PCB –Queue PCB on s.queue –Dispatch next process on ReadyQueue else –s.count = s.count – 1; –Re-dispatch current process Ready queue PCB Event wait

Introduction to Synchronization CS-3013 C-term Implementation – Semaphore actions Action – post_s(Semaphore &s) Implement as a Trap (with interrupts disabled) if (s.queue != null) –Save current process in ReadyQueue –Move first process on s.queue to ReadyQueue –Dispatch some process on ReadyQueue else –s.count = s.count + 1; –Re-dispatch current process Ready queue PCB Semaphore A count = 0 PCB Event completion

Introduction to Synchronization CS-3013 C-term Interrupt Handling (Quickly) analyze reason for interrupt Execute equivalent post_s to appropriate semaphore as necessary Implemented in device-specific routines Real work of interrupt handler is done in a separate task-like entity in the kernel More about interrupt handling later in the course

Introduction to Synchronization CS-3013 C-term Complications for Multiple Processors Disabling interrupts is not sufficient for atomic operations Semaphore operations must themselves be implemented in critical sections Queuing and dequeuing PCB’s must also be implemented in critical sections Other control operations need protection These problems all have solutions but need deeper thought!

Introduction to Synchronization CS-3013 C-term Synchronization in Multiple Processors (continued) Typical solution – spinlocks –Kernel process (or interrupt handler) trying to enter a kernel critical section does busy waiting –Test & Set or equivalent Constraint –Critical sections are very short – a few nanoseconds! –Process holding a spinlock may not be pre-empted or rescheduled by any other process or kernel routine –Process may not sleep, take page fault, or wait for any reason while holding a spinlock

Introduction to Synchronization CS-3013 C-term Synchronization in Multiple Processors (continued) Typical solution – spinlocks –Kernel process (or interrupt handler) trying to enter a kernel critical section does busy waiting –Test & Set or equivalent Constraint –Critical sections are very short – a few nanoseconds! –Process holding a spinlock may not be pre-empted or rescheduled by any other process or kernel routine –Process may not sleep, take page fault, or wait for any reason while holding a spinlock

Introduction to Synchronization CS-3013 C-term Summary Interrupts transparent to processes Can be used to simulate atomic actions wait_s() and post_s() behave as if they are atomic Useful for synchronizing among processes

Introduction to Synchronization CS-3013 C-term Semaphores – Epilogue A way for generic processes to synchronize with each other Not the only way Not even the best way in most cases More later in the course See Ch. 6 of Silbershatz

Introduction to Synchronization CS-3013 C-term Questions?

Introduction to Synchronization CS-3013 C-term Process States