Concurrency Control PDF
Document Details
Uploaded by Deleted User
null
Silberschatz, Galvin and Gagne
Tags
Summary
This document provides a presentation on operating systems concepts, focusing on concurrency control, the critical section problem, and synchronization mechanisms like semaphores and mutex locks. It includes explanations, diagrams, and examples related to fundamental operating system concepts.
Full Transcript
Concurrency Control Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009 Overview The Critical-Section Problem Peterson’s Solution Synchronization Hardware...
Concurrency Control Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009 Overview The Critical-Section Problem Peterson’s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization Monitors Operating System Concepts – 8th Edition 3.2 Silberschatz, Galvin and Gagne ©2009 Interprocess Communication Processes within a system may be independent or cooperating Cooperating process can affect or be affected by other processes, including sharing data Reasons for cooperating processes: Information sharing Computation speedup Modularity Convenience Cooperating processes need interprocess communication (IPC) Two models of IPC Shared memory Message passing Operating System Concepts – 8th Edition 3.3 Silberschatz, Galvin and Gagne ©2009 Communications Models Operating System Concepts – 8th Edition 3.4 Silberschatz, Galvin and Gagne ©2009 Synchronization ▪ Message passing may be either blocking or non-blocking ▪ Blocking is considered synchronous Blocking send: the sender block until the message is received Blocking receive: the receiver block until a message is available ▪ Non-blocking is considered asynchronous Non-blocking send: the sender send message and continue Non-blocking receive: the receiver receive a valid message or null Operating System Concepts – 8th Edition 3.5 Silberschatz, Galvin and Gagne ©2009 Producer-Consumer Problem ▪ Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size Operating System Concepts – 8th Edition 3.6 Silberschatz, Galvin and Gagne ©2009 Producer-Consumer Problem There are two processes: producer process produces information that is consumed by a consumer process with bounded-buffer –We wanted to provide a solution to this problem that fills all the buffers. –We can do so by having an integer counter that keeps track of the number of full buffers. –Initially, counter is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer. Operating System Concepts – 8th Edition 3.7 Silberschatz, Galvin and Gagne ©2009 Producer-Consumer Problem (Unbounded Buffer Size) Operating System Concepts – 8th Edition 3.8 Silberschatz, Galvin and Gagne ©2009 Producer-Consumer Problem (bounded Buffer Size) Producer Consumer while (true) { while (true) { while (counter == 0) ; while (counter == BUFFER_SIZE) ; next_consumed = buffer[out]; buffer[in] = next_produced; out = (out + 1) % BUFFER_SIZE; in = (in + 1) % BUFFER_SIZE; counter--; counter++; } } Race Condition? Operating System Concepts – 8th Edition 3.9 Silberschatz, Galvin and Gagne ©2009 Race Condition counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 counter-- could be implemented as register2 = counter register2 = register2 - 1 counter = register2 Consider this execution interleaving with “counter = 5” initially: S0: producer execute register1 = counter {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = counter {register2 = 5} S3: consumer execute register2 = register2 – 1 {register2 = 4} S4: producer execute counter = register1 {counter = 6 } S5: consumer execute counter = register2 {counter = 4} Operating System Concepts – 8th Edition 3.10 Silberschatz, Galvin and Gagne ©2009 Race Condition Values are in the range: -1 to 1 Operating System Concepts – 8th Edition 3.11 Silberschatz, Galvin and Gagne ©2009 Race Condition Find the min and max value of x? In other words due to race condition value of x are in the range to ? Operating System Concepts – 8th Edition 3.12 Silberschatz, Galvin and Gagne ©2009 Race Condition Find the min and max value of x? Operating System Concepts – 8th Edition 3.13 Silberschatz, Galvin and Gagne ©2009 Race Condition Operating System Concepts – 8th Edition 3.14 Silberschatz, Galvin and Gagne ©2009 Critical Section Problem Consider system of n processes {p0, p1, … pn-1} General structure of process Pi Each process has critical section segment of code –Process may be changing common variables, updating table, writing file, etc –When one process in critical section, no other may be in its critical section Critical section problem is to design a protocol that the processes can use to synchronize their activity so as to cooperatively share data. Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section Operating System Concepts – 8th Edition 3.15 Silberschatz, Galvin and Gagne ©2009 Algorithm for Process Pi Operating System Concepts – 8th Edition 3.16 Silberschatz, Galvin and Gagne ©2009 Solution to Critical-Section Problem A solution to the critical-section problem must satisfy the following three requirements: 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 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 Operating System Concepts – 8th Edition 3.17 Silberschatz, Galvin and Gagne ©2009 Algorithm 1 Operating System Concepts – 8th Edition 3.18 Silberschatz, Galvin and Gagne ©2009 Algorithm 1 ✔ Satisfies Mutual execution, but not progress Operating System Concepts – 8th Edition 3.19 Silberschatz, Galvin and Gagne ©2009 Algorithm 1 Operating System Concepts – 8th Edition 3.20 Silberschatz, Galvin and Gagne ©2009 Algorithm 2 Operating System Concepts – 8th Edition 3.21 Silberschatz, Galvin and Gagne ©2009 Algorithm 2 ✔ Satifies mutual exclusion ✔ If flag=flag=true 🡪 infinite loop Operating System Concepts – 8th Edition 3.22 Silberschatz, Galvin and Gagne ©2009 Peterson’s Solution (Algorithm 3) No guaranteed to work on modern architectures! (But good algorithm ) Peterson’s solution is restricted to two processes Assume that the load and store machine-language instructions are atomic; that is, cannot be interrupted. The two processes share two variables: –int turn; // The variable turn indicates whose turn it is to enter the critical section –Boolean flag; // 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! Operating System Concepts – 8th Edition 3.23 Silberschatz, Galvin and Gagne ©2009 Peterson’s Solution Algorithm for Process Pi Operating System Concepts – 8th Edition 3.24 Silberschatz, Galvin and Gagne ©2009 Peterson’s Solution ✔ Meets all three requirements Operating System Concepts – 8th Edition 3.25 Silberschatz, Galvin and Gagne ©2009 Peterson’s Solution (Cont.) Provable that the three CS requirement are met: 1. Mutual exclusion is preserved Pi enters CS only if: either flag[j] = false or turn = i 2. Progress requirement is satisfied 3. Bounded-waiting requirement is met Operating System Concepts – 8th Edition 3.26 Silberschatz, Galvin and Gagne ©2009 Synchronization Hardware Hardware is faster than the software & can have better efficiency Many systems provide hardware support for implementing the critical section code. Uniprocessors – could disable interrupts –Currently running code would execute without pre-emption until it invokes an OS service or until it is interrupted –Disabling interrupts guarantees mutual exclusion –Processor is limited in its ability to interleave programs -Operating systems using this not broadly scalable Solution to Critical-section Problem Using Locks Operating System Concepts – 8th Edition 3.27 Silberschatz, Galvin and Gagne ©2009 ▪ Generally too efficient on Multiprocessor systems ▪ Disabling interrupt in microprocessor machine is time consuming, as the message pass to all the processors. This message delays entry into each critical section and system efficiency decreases ▪ The system clock functionality may be disturbed, if the clock is kept updated by the interrupt ▪ Modern machines provide special atomic (non- interruptable) hardware instructions ✔ Performed in a single instruction cycle ✔ Access to the memory location is blocked for any other instructions ✔ Either test memory word and set value ✔ Or swap contents of two memory words Operating System Concepts – 8th Edition 3.28 Silberschatz, Galvin and Gagne ©2009 Hardware Instructions ▪ Special hardware instructions that allow us to either test-and-modify the content of a word, or to swap the contents of two words atomically ▪ Test-and-set Instruction ▪ Compare-and-Swap Instruction Operating System Concepts – 8th Edition 3.29 Silberschatz, Galvin and Gagne ©2009 test_and_set Instructions Definition: boolean test_and_set (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } Executed atomically Returns the original value of passed parameter Set the new value of passed parameter to “TRUE”. Operating System Concepts – 8th Edition 3.30 Silberschatz, Galvin and Gagne ©2009 Solution using test_and_set Instructions Shared Boolean variable lock, initialized to FALSE Solution: do { while (test_and_set(&lock)) ; lock = false; } while (true); Operating System Concepts – 8th Edition 3.31 Silberschatz, Galvin and Gagne ©2009 compare_and_swap Instruction Definition: int compare _and_swap(int *value, int expected, int new_value) { int temp = *value; if (*value == expected) *value = new_value; return temp; } Executed atomically Returns the original value of passed parameter “value” Set the variable “value” the value of the passed parameter “new_value” but only if “value” ==“expected”. That is, the swap takes place only under this condition. Operating System Concepts – 8th Edition 3.32 Silberschatz, Galvin and Gagne ©2009 Working Example Operating System Concepts – 8th Edition 3.33 Silberschatz, Galvin and Gagne ©2009 Soluition using compare_and_swap Instruction Shared integer “lock” initialized to 0; Solution: do { while (compare_and_swap(&lock, 0, 1) != 0) ; lock = 0; } while (true); Operating System Concepts – 8th Edition 3.34 Silberschatz, Galvin and Gagne ©2009 Mutual Exclusion Machine Instruction Operating System Concepts – 8th Edition 3.35 Silberschatz, Galvin and Gagne ©2009 Bounded-waiting Mutual Exclusion with test_and_set do { waiting[i] = true; key = true; while (waiting[i] && key) key = test_and_set(&lock); waiting[i] = false; j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j + 1) % n; if (j == i) lock = false; else waiting[j] = false; } while (true); Operating System Concepts – 8th Edition 3.36 Silberschatz, Galvin and Gagne ©2009 Mutex Locks Previous solutions are complicated and generally inaccessible to application programmers OS designers build software tools to solve critical section problem Simplest is mutex lock Protect a critical section by first acquire() a lock then release() the lock Boolean variable indicating if lock is available or not Calls to acquire() and release() must be atomic –Usually implemented via hardware atomic instructions But this solution requires busy waiting –This lock therefore called a spinlock Operating System Concepts – 8th Edition 3.37 Silberschatz, Galvin and Gagne ©2009 acquire() and release() acquire() { while (!available) ; available = false; } release() { available = true; } do { acquire lock critical section release lock remainder section } while (true); Operating System Concepts – 8th Edition 3.38 Silberschatz, Galvin and Gagne ©2009 Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer variable Can only be accessed via two indivisible (atomic) operations wait() and signal() Originally called P() and V() Definition of the wait() operation wait(S) { while (S value--; S->value++; if (S->value < 0) { if (S->value list; remove a process P from S->list; block(); wakeup(P); } } } } Operating System Concepts – 8th Edition 3.45 Silberschatz, Galvin and Gagne ©2009 Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 Starvation – indefinite blocking A process may never be removed from the semaphore queue in which it is suspended Priority Inversion – Scheduling problem when lower-priority process holds a lock needed by higher-priority process –Solved via priority-inheritance protocol Operating System Concepts – 8th Edition 3.46 Silberschatz, Galvin and Gagne ©2009 Classical Problems of Synchronization Classical problems used to test newly-proposed synchronization schemes –Bounded-Buffer Problem –Readers and Writers Problem –Dining-Philosophers Problem Operating System Concepts – 8th Edition 3.47 Silberschatz, Galvin and Gagne ©2009 Bounded-Buffer Problem n buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value n Bounded-Buffer Problem The structure of the producer process do {...... wait(empty); wait(mutex);...... signal(mutex); signal(full); } while (true); Bounded-Buffer Problem The structure of the consumer process do { wait(full); wait(mutex);...... signal(mutex); signal(empty);...... } while (true); Readers-Writers Problem A data set is shared among a number of concurrent processes –Readers – only read the data set; they do not perform any updates –Writers – can both read and write Problem – allow multiple readers to read at the same time –Only one single writer can access the shared data at the same time ✔Reader and writer cannot access simultaneously ✔No other processes are allowed to enter in the critical section when a writer is executing the criritical section. Shared Data –Data structure –Semaphore rw_mutex initialized to 1 –Semaphore mutex initialized to 1 –Integer read_count initialized to 0 Operating System Concepts – 8th Edition 3.51 Silberschatz, Galvin and Gagne ©2009 Readers-Writers Problem (Cont.) The structure of a writer process do { wait(rw_mutex);...... signal(rw_mutex); } while (true); Operating System Concepts – 8th Edition 3.52 Silberschatz, Galvin and Gagne ©2009 Readers-Writers Problem (Cont.) The structure of a reader process do { wait(mutex); read_count++; if (read_count == 1) wait(rw_mutex); signal(mutex);...... wait(mutex); read count--; if (read_count == 0) signal(rw_mutex); signal(mutex); } while (true); Operating System Concepts – 8th Edition 3.53 Silberschatz, Galvin and Gagne ©2009 Readers-Writers Problem Variations First variation – no reader kept waiting unless writer has permission to use shared object Second variation – once writer is ready, it performs the write ASAP Both may have starvation leading to even more variations Problem is solved on some systems by kernel providing reader-writer locks Dining-Philosophers Problem Philosophers spend their lives alternating thinking and eating Don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl –Need both to eat, then release both when done In the case of 5 philosophers –Shared data Bowl of rice (data set) Semaphore chopstick initialized to 1 Dining-Philosophers Problem Algorithm The structure of Philosopher i do { wait (chopstick[i] ); wait (chopStick[ (i + 1) % 5] ); // eat signal (chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE); What is the problem with this algorithm? Dining-Philosophers Problem Algorithm Deadlock handling –Allow at most 4 philosophers to be sitting simultaneously at the table. –Allowa philosopher to pick up the forks only if both are available (picking must be done in a critical section. –Use an asymmetric solution -- an odd-numbered philosopher picks up first the left chopstick and then the right chopstick. Even-numbered philosopher picks up first the right chopstick and then the left chopstick. Problems with Semaphores Incorrect use of semaphore operations: – signal (mutex) …. wait (mutex) – wait (mutex) … wait (mutex) – Omitting of wait (mutex) or signal (mutex) (or both) Deadlock and starvation are possible. Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Abstract data type, internal variables only accessible by code within the procedure Only one process may be active within the monitor at a time But not powerful enough to model some synchronization schemes monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } procedure Pn (…) {……} Initialization code (…) { … } } Schematic view of a Monitor Condition Variables condition x, y; Two operations are allowed on a condition variable: –x.wait() – a process that invokes the operation is suspended until x.signal() –x.signal() – resumes one of processes (if any) that invoked x.wait() –If no x.wait() on the variable, then it has no effect on the variable Monitor with Condition Variables Condition Variables Choices If process P invokes x.signal(), and process Q is suspended in x.wait(), what should happen next? –Both Q and P cannot execute in paralel. If Q is resumed, then P must wait Options include –Signal and wait – P waits until Q either leaves the monitor or it waits for another condition –Signal and continue – Q waits until P either leaves the monitor or it waits for another condition –Both have pros and cons – language implementer can decide –Monitors implemented in Concurrent Pascal compromise –P executing signal immediately leaves the monitor, Q is resumed –Implemented in other languages including Mesa, C#, Java Monitor Solution to Dining Philosophers monitor DiningPhilosophers { enum { THINKING; HUNGRY, EATING) state ; condition self ; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } Solution to Dining Philosophers (Contd.) void test (int i) { if ((state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } } Solution to Dining Philosophers (Contd.) Each philosopher i invokes the operations pickup() and putdown() in the following sequence: DiningPhilosophers.pickup(i); EAT DiningPhilosophers.putdown(i); No deadlock, but starvation is possible