1 / 34

Process Synchronization

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:

Télécharger la présentation

Process Synchronization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

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

  4. 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++; }

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

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

  7. Example: Producer/Consumer load r1, counterload r2, counteradd r1, onesub r2, onestore r1, counterstore 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?

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

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

  14. Solution to Critical-Section Problem 1. Mutual Exclusion - If process Pi 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

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

  16. Initial Attempts to Solve Problem • Only 2 processes, Piand Pj • 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

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

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

  19. 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 Pi is ready!

  20. Solution? #3 • Combined shared variables of solutions #1 and #2 Process Pi Process Pj do { do{ flag[i] = true; flag[j] = true; turn = j; turn = i; while (flag[j] and turn == j); while (flag[i] and turn == i); critical sectioncritical section flag[i] = false; flag[j] = false; remainder section remainder section } while (TRUE); } 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

  22. 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 Piand Pjreceive the same number, if i <j, then Pi is served first; else Pj is served first • The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...

  23. 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

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

  25. Time Person A Person B 3:00 Look in Fridge. Out of milk 3:05 Leave for store 3:10 Arrive at store Look in Fridge. Out of milk 3:15 Buy milk Leave for store 3:20 Arrive home, put milk away Arrive at store 3:25 Buy milk 3:30 Arrive home, put milk away 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:

  26. 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

  27. “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

  28. “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…

  29. “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

  30. “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

  31. “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

  32. “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

  33. “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();

  34. 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 Programs Higher-level API Hardware Shared Programs Locks Semaphores Monitors Load/Store Disable Ints Test&Set Comp&Swap

More Related