🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Ch6 Synchronization.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

Princess Nora University Computer Sciences Department Operating Systems CS 340 1 Term 1 - 1446 Chapter-6 Synchronization (covered in chapter 5 of textbook) Introduction Objectives  To present the concept of process synchronizatio...

Princess Nora University Computer Sciences Department Operating Systems CS 340 1 Term 1 - 1446 Chapter-6 Synchronization (covered in chapter 5 of textbook) Introduction Objectives  To present the concept of process synchronization.  To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data  To present both software and hardware solutions of the critical-section problem  To examine several classical process-synchronization problems 3 Background  Processes can execute concurrently  Concurrent access to shared data may result in data inconsistency …(Problem)  Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. …(Solution) Data consistency is the accuracy, completeness, and correctness of data stored.  Illustration of the problem:  Suppose that we wanted to provide a solution to the consumer- producer 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. 4 Producer (P1) while (true) { while (counter == BUFFER_SIZE) ; If the array is full, then buffer[in] = next_produced; WAIT in = (in + 1) % BUFFER_SIZE; counter++; } 5 Consumer (P2) while (true) { while (counter == 0) If the array ; is empty, then WAIT next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; } 6 Race Condition Situation like this, where several processes manipulate the  counter++ could be implemented as same data concurrently and the outcome of the execution register1 = counter register1 = register1 + 1 depends on the particular order counter = register1 in which the access takes place, is called a race  counter-- could be implemented as condition. 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} 7 Critical section segment related to perform WRITE Critical Section Problem operation on data  Consider system of n processes {p0, p1, … pn-1}  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 protocol to solve this.  Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section. 8 Critical Section  General structure of process Pi 9 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. 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. 10 Critical-Section Handling in OS  At a given point in time, many kernel-mode processes may be active in the operating system.  As a result, the code implementing an operating system (kernel code) is subject to several possible race conditions.  Two approaches are used to handle critical sections in OSs:  Preemptive – allows preemption of process when running in kernel mode.  Non-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPU.  Essentially free of race conditions in kernel mode. 11 Synchronization Hardware  Many systems provide hardware support for implementing the critical section code.  All solutions below based on idea of locking.  Protecting critical regions via locks  Uniprocessors – could disable interrupts while a shared variable was being modified.  Currently running code would execute without preemption.  Generally, too inefficient on multiprocessor systems.  Operating systems using this not broadly scalable.  Modern machines provide special atomic hardware instructions An atomic instruction is a  Atomic = non-interruptible computer instruction that  Either test memory word and set value. can be executed as a single and indivisible operation,  Or swap contents of two memory words meaning that it cannot be interrupted by another instruction or process.. 12 Solution to Critical-section Problem Using Locks do { acquire lock // apply lock on data critical section release lock // release lock on data remainder section } while (TRUE); 13 Mutex is short for mutual exclusion Mutex Locks Previous solutions are complicated. 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 technique in which a process waits and constantly checks for a spinlocks use a condition to be satisfied busy-wait method. before proceeding with its execution. 14 acquire() and release() acquire() { while (!available) ; available = false; } ------------------------------------------------------------------------------------------------------------------------------------------------------------------- release() { available = true; } ------------------------------------------------------------------------------------------------------------------------------------------------------------------- do { acquire lock // apply lock on data critical section release lock // release lock on data remainder section } while (true); 15 Semaphores are integer variables that are used to solve the critical section problem by using two Semaphore atomic operations, wait and signal that are used for process synchronization.  Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities.  Semaphore S – integer variable shared between processes  Semaphore can only be accessed via two indivisible (atomic) operations  wait() and signal() Semaphore (S ) is initialized to  Originally called P() and V() value (1) If S=0→ then all resources are being used. //Process (P1): If a process wants to use a resource → then is will be // some code blocked until S>0. Wait (s); // apply lock on data Critical section will be critical section surrounded by wait & signal Signal (s); // release lock on data methods 16 Semaphore  wait() operation If data is already locked wait(S) { Just wait ; while (S

Use Quizgecko on...
Browser
Browser