Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Synchronization. How to synchronize processes? – Need to protect access to shared data to avoid problems like race conditions – Typical example: Updating.
PROCESS SYNCHRONIZATION READINGS: CHAPTER 5. ISSUES IN COOPERING PROCESSES AND THREADS – DATA SHARING Shared Memory Two or more processes share a part.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
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
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
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.
The Critical-Section Problem
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 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
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.
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.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations, Locks September 10, 2012 Ion Stoica
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
CS703 – Advanced Operating Systems
Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
Chapter 5: Process Synchronization
143a discussion session week 3
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
The Critical-Section Problem
Lecture 19 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Grades.
Chapter 6: Process Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes we are going to have situations that are irreproducible and unpredictable

Example: ATM Bank Server ATM server problem: – Service a set of requests – Do so without corrupting database – Maintain correct balance – Don’t hand out too much money

Example: ATM Bank Server Deposit(acctId, amount) { acct = GetAccount(actId); acct->balance += amount; StoreAccount(acct); } Unfortunately, shared state can get corrupted: Process1Process 2 load r1, acct->balance load r2, acct->balance add r2, amount2 store r2, acct->balance add r1, amount1 store r1, acct->balance

Example: Producer/Consumer while (1) { while (counter == BUFFER_SIZE) ; // do nothing // produce an item and put it in nextProduced buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; }

Example: Producer/Consumer while (1) { while (counter == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; // consume the item in nextConsumed }

Example: Producer/Consumer load r1, counter counter++ add r1, one store r1, counter load r2, counter counter--sub r2, one store r2, counter

Example: Producer/Consumer load r1, counter load r2, counter add r1, one sub r2, one store r1, counter store r2, counter Consider this execution interleaving: S0: producer executes load r1, counter [ r1 = 5] S1:producer executes add r1, one [ r1 = 6] S2: consumer executes load r2, counter [ r2 = 5] S3: consumer executes sub r2, one [ r2 = 4] S4: producer executes store r1, counter [ counter = 6] S5: consumer executes store r2, counter [ counter = 4] What execution interleaving gives 5 or 6?

Race Condition A situation where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place

Atomic Operations Atomic Operation: an operation that always runs to completion or not at all – It is indivisible: it cannot be stopped in the middle and state cannot be modified by someone else in the middle – Fundamental building block – if no atomic operations, then have no way for processes to work together On most machines, memory references and assignments (i.e., loads and stores) of words are atomic. Many instructions are not atomic

Atomic Operations Bottom level indivisible operation is architecture dependent. Typically, it is whatever takes place in one CPU cycle. Everything else can be divided Lowest level atomic operation is called memory interlock or hardware arbiter. Everything else is built on top of that

Critical Section In order to avoid having these unpredictable situations we need some way of synchronizing (establishing order) processes at their point of interaction The critical section is the segment of code in which the process may be changing common variables, updating a table, writing a file, and so on (i.e., segment of code containing at least one shared variable) When one process is executing in its critical section, no other process should be allowed to execute in its critical section. That is, no two processes should be allowed to execute in their critical sections at the same time

Critical Section Critical sections are used to artificially create indivisible operations The critical section problem is to design a protocol that processes can use to cooperate. Each process must request permission to enter its critical section

General Structure of a Process do { [entry section] critical section [exit section] remainder section } while (TRUE);

Solution to Critical-Section Problem 1.Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2.Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely (i.e., it is not turn-taking) 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 Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes

Solution to Critical Section Problem Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes

Initial Attempts to Solve Problem Only 2 processes, P i and P j General structure of a process: do { [entry section] critical section [exit section] remainder section } while (TRUE); Processes may share some common variables to synchronize their actions

Solution? #1 Shared variables: – int turn ; initially turn = i – turn = i  P i can enter its critical section Process P i Process P j do { while (turn != i);while (turn != j); critical section critical section turn = j; turn = i; remainder section remainder section} while (TRUE); How good is this solution in terms of solution to CS problem requirements? (mutual exclusive yes, progress, no)

Solution? #2 Shared variables: – boolean flag[2]; initially flag[0] = flag[1] = false – flag[i] = true  P i ready to enter its critical section Process P i Process P j do { flag[i] = true; flag[j] = true; while (flag[j]);while (flag[i]); critical section critical section flag[i] = false;flag[j] = false; remainder section remainder section} while (TRUE); How good is this solution in terms of solution to CS problem requirements? (no progress)

Peterson’s Solution Two process solution The two processes share two variables: –int turn; –Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process P i is ready!

Solution? #3 Combined shared variables of solutions #1 and #2 Process P i Process P j do { flag[i] = true; flag[j] = true; turn = j;turn = i; while (flag[j] and turn == j);while (flag[i] and turn == i); critical section critical section flag[i] = false;flag[j] = false; remainder section remainder section} while (TRUE); How good is this solution in terms of solution to CS problem requirements?

21 Satisfying the properties Mutual exclusion – turn must be 0 or 1 => only one thread can be in CS Progress – only one thread trying to get into CS => flag[other] is false => will get in Bounded Waiting – spinning thread will not modify turn – thread trying to go back in will set turn equal to spinning thread

Bakery Algorithm Critical section problem for n processes Before entering its critical section, process receives a number. Holder of the smallest number enters the 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 first The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...

Bakery Algorithm Notation <  lexicographical order (ticket #, process id #) (a,b) < (c,d) if a < c or if a = c and b < d Shared data boolean choosing[n]; int number[n]; Data structures are initialized to false and 0 respectively

Bakery Algorithm Creating a number (first part of ticket) Awaiting for permission to enter CS

Motivation: “Too much milk” Great thing about OS’s – analogy between problems in OS and problems in real life – Help you understand real life problems better – But, computers are much stupider than people Example: People need to coordinate: Person BPerson ATime Arrive home, put milk away3:30 Buy milk3:25 Arrive at storeArrive home, put milk away3:20 Leave for storeBuy milk3:15 Leave for store3:05 Look in Fridge. Out of milk3:00 Look in Fridge. Out of milkArrive at store3:10

Lock Lock: prevents someone from doing something – Lock before entering critical section and before accessing shared data – Unlock when leaving, after accessing shared data – Wait if locked (Important idea: all synchronization involves waiting) For example: fix the milk problem by putting a key on the refrigerator – Lock it and take key if you are going to go buy milk – Fixes too much: roommate angry if only wants something else – Of Course – We don’t know how to make a lock yet

“ Too Much Milk ” Problem What are the correctness properties for the “ Too much milk ” problem??? – Never more than one person buys – Someone buys if needed Restrict ourselves to use only atomic load and store operations as building blocks

“ Too Much Milk ” Solution? #1 Use a note to avoid buying too much milk: – Leave a note before buying (kind of “ lock ” ) – Remove note after buying (kind of “ unlock ” ) – Don ’ t buy if note (wait) Suppose a computer tries this (remember, only memory read/write are atomic): if (noMilk) { if (noNote) { leave Note; buy milk; remove note; } } Result? – Still too much milk but only occasionally! – Process can get context switched after checking milk and note but before leaving note! Solution makes problem worse since fails intermittently – Makes it really hard to debug…

“ Too Much Milk ” Solution? #1½ Clearly the Note is not quite blocking enough – Let ’ s try to fix this by placing note first Another try at previous solution: leave Note; if (noMilk) { if (noNote) { buy milk; } } remove note; What happens here? – Well, with human, probably nothing bad – With computer: no one ever buys milk

“ Too Much Milk ” Solution? #2 How about labeled notes? – Now we can leave note before checking Process AProcess B leave note A;leave note B; if (noNote B) {if (noNote A) { if (noMilk) { if (noMilk) { buy Milk; buy Milk; } } }} remove note A;remove note B; Does this work? Possible for neither process to buy milk – Context switches at exactly the wrong times can lead each to think that the other is going to buy Extremely unlikely that this would happen, but will at worse possible time

“ Too Much Milk ” Solution? #3 Here is a possible two-note solution: Process AProcess B leave note A;leave note B; while (note B) { //X if (noNote A) { //Y do nothing; if (noMilk) { } buy milk; if (noMilk) { } buy milk;} }remove note B; remove note A; Does this work? Yes. Both can guarantee that: – It is safe to buy, or – Other will buy, ok to quit At X: – if no note B, safe for A to buy, – otherwise wait to find out what will happen At Y: – if no note A, safe for B to buy – Otherwise, A is either buying or waiting for B to quit

“ Too Much Milk ” Solution #3 Discussion Our solution protects a single CS piece of code for each process: if (noMilk) { buy milk; } Solution #3 works, but it ’ s really unsatisfactory – Really complex – even for this simple an example » Hard to convince yourself that this really works – A ’ s code is different from B ’ s – what if lots of processes? » Code would have to be slightly different for each process – While A is waiting, it is consuming CPU time » This is called “ busy-waiting ” There ’ s a better way – Have hardware provide better (higher-level) primitives than atomic load and store – Build even higher-level programming abstractions on this new hardware support

“ Too Much Milk ” Solution #4 Suppose we have some sort of implementation of a lock – Lock.Acquire() – wait until lock is free, then grab – Lock.Release() – Unlock, waking up anyone waiting – These must be atomic operations – if two processes are waiting for the lock and both see it ’ s free, only one succeeds to grab the lock Then, our milk problem is easy: milklock.Acquire(); if (nomilk) buy milk; milklock.Release();

Where are we going with synchronization? We are going to implement various higher-level synchronization primitives using atomic operations – Everything is pretty painful if only atomic primitives are load and store – Need to provide primitives useful at user-level Load/Store Disable Ints Test&Set Comp&Swap Locks Semaphores Monitors Shared Programs Hardware Higher-level API Programs