1 Synchronization Coordinating the Activity of Mostly Independent Entities.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems Part III: Process Management (Process Synchronization)
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
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.
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.
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
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.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
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.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Chapter 5: Process Synchronization
143a discussion session week 3
Critical section problem
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
Chapter 6 Synchronization Principles
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2009 Module 7 Synchronization
CSE 542: Operating Systems
Presentation transcript:

1 Synchronization Coordinating the Activity of Mostly Independent Entities

2 Independent vs. Cooperating Processes Independent –no shared state –deterministic –each process can proceed at arbitrary rate Cooperating –Shared state like a common variable –non-deterministic execution hard to reproduce, and subject to race conditions

3 A Two Process Example Who Wins? int A; thread_fork(doBlue, 0); thread_fork(doRed, 0); doBlue() { A = 1; if (A ==1) printf(“Black wins”); } doRed() {A = 2; if (A == 2) printf(“Red wins”); }

4 Programming with MUTUAL EXCLUSION Means –adding synchronization so you can reason about what your program is doing. Synchronized sections are called Critical Sections –An atomic operation guards access to the critical section. –Only one thread at a time can execute in the critical section. others are forced to wait on entry. Some atomic operation Critical section Some atomic operation

5 Our Two Process Example blueProcess() { if (redProcessNotInCriticalSection()) { blueProcessInCriticalSection(); /* DO CRITICAL SECTION */ blueProcessNotInCriticalSection(); } This “TEST AND SET” operation must execute atomically. Can not be interrupted.

6 Atomic Operations Problem: –Must know what is atomic before we can say anything about outcome. Defn: –An “Atomic” operation is one which executes as though it were not interrupted in either time and space. time is easier to think about but it is really space that we care about

7 Atomic Test And Set Test a value to see if it indicates “available” Set the value to indicate available Must execute indivisibly TestAndSet x int current = x; if (current == FREE) x = BUSY; return current; current = x x = FREE x = BUSY return “FREE” x = BUSY return “FREE”

8 Where do Atomic Operations Come From? The hardware is required to do something atomically. –More important to know what it is than for it to be something explicit atomic register swap test and set atomic memory load and store –x = 32; –x = y; –With this minimal atomic operation, can build others on top.

9 More Specifically Simple reads and writes of single words are generally atomic. –Consider a double word load... On a uniprocessor, anything between interrupts is atomic. Without SOME atomic operation, you can’t make others –can be as simple as an atomic bit set. Simple atomic operations can be used to build more complicated ones

10 Requirements of a Critical Section Mutual exclusion –it works –if one process is in the CS, then no other is. Progress –if some process P is not in the critical section, the P can not prevent some other process Q from entering the critical section. Bounded Waiting (No starvation) –if some process P is waiting on the critical section, then P will eventually enter the critical section. Performance –The overhead of entering and exiting the critical section is low with respect to the work being done within.

11 Higher Level Mechanisms for Building Critical Sections Semaphores –Very primitive. Almost specifying direct start and stop of threads. –Simple. Hard to program with, but easy to get the hang of. Monitors –Higher level than semaphores (language support). More abstract. Messages –Simple model of communication and synchronization based on atomic transfer of data across a channel. –Direct application to distributed systems –We’ll do this one last since it’s the easiest (once we see how the others work).

12 Foundations for Synchronization thread_stop() –takes the currently running thread, saves its state, and switches to another thread. thread_start(thread_t t) –takes the named thread and makes it runnable thread_switch(thread_t t1, thread_t t2) –takes the currently running thread (t1) and saves the current state in t1’s TCB. Restores the previous context from the TCB of t2. thread_switch thread_stop the scheduler thread_switch oldThread newThread

13 Semaphores Abstract data type with an internal counter that is accessed atomically. –P(s) wait for s.counter > 0; decrement s.counter. –V(s) increment s Semaphore_t s; int x;... P(s); X++ V(s); Example: Atomically increment a shared variable x

14 Binary Semaphores Counter can take on two values: 0,1 –initially, count is 1. –P(s) : if s.count > 0 then s.count = 0 if s.count == 0 then wait –V(sem): count(sem) = 1; Used for mutual exclusion –Only one process can access a critical section at a time. –To enter the critical section, P on the semaphore. –When leaving the critical section, V on the semaphore. –Don’t forget to V.

15 Counting semaphores One “resource” with the mutual exclusion example. –the resource is the critical section. When you have a lot of resources and you want to hand them out: –if there is one, you can have it. –otherwise, you must wait. Want to use COUNTING SEMAPHORES: [0..N] –initially, count == N. P(sem): if count(sem) > 0, count(sem) --; – if count(sem) == 0 wait; V(sem): count(sem)++;

16 Producer/Consumer Producer –generates new “things” (disk blocks, memory pages, processes, files, etc.) Consumer –consumes things generated by the producer Want to allow producer and consumer to run at different rates –no serialization of one behind the other –tasks are independent –easier to think about

17 Producer/Consumer Introduce an intermediate BUFFER to handle the mismatched rates. –Producer deposits items into the buffer –Consumer pulls items out of the buffer. Need to synchronize access to buffer. PRODUCER P(bufferSem); buffer = B; V(bufferSem); CONSUMER P(bufferSem); process buffer; V(bufferSem); Is this ok? buffer

18 Synchronizing on the buffer and the rate The previous example gave us mutually exclusive access to the buffer, but did not allow us to proceed at mismatched rates. –not much better than having one process. Introduce more buffers. –use counting semaphores, treat each buffer as a resource. Semaphores –empty: [0,N] --> are there any empty slots? –full: [0,N] --> are there any full slots? –bufferPool: [0,1] --> can I access the buffer pool now?

19 Buffer Pool PRODUCER P(empty); /* take one empty */ P(bufferPool); /* put new buffer into pool */ V(bufferPool); V(full); /* create one full */ CONSUMER P(full); P(bufferPool); /* get buffer from pool */ V(bufferPool); V(empty); Does order of V matter? Does order of P matter?

20 Summary Concurrent programs involve shared state between independent activities Some kind of concurrency control mechanism is required Higher level synchronization services can be built on top of lower level ones –test and set --> semaphores --> others??