Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Process Synchronization Copyright © 2008.

Similar presentations


Presentation on theme: "Chapter 6 Process Synchronization Copyright © 2008."— Presentation transcript:

1 Chapter 6 Process Synchronization Copyright © 2008

2 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.2Operating Systems, by Dhananjay Dhamdhere2 Introduction What is Process Synchronization? Race Conditions Critical Sections Control Synchronization and Indivisible Operations Synchronization Approaches Structure of Concurrent Systems

3 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.3Operating Systems, by Dhananjay Dhamdhere3 Introduction (continued) Classic Process Synchronization Problems Algorithmic Approach to Implementing Critical Sections Semaphores Monitors Case Studies of Process Synchronization

4 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.4Operating Systems, by Dhananjay Dhamdhere4 What is Process Synchronization? The term process is a generic term for both a process and a thread Processes that do not interact are independent processes Process synchronization is a generic term for the techniques used to delay and resume processes to implement process interactions

5 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.5Operating Systems, by Dhananjay Dhamdhere5

6 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.6Operating Systems, by Dhananjay Dhamdhere6 Race Conditions Uncoordinated accesses to shared data may affect consistency of data Consider processes P i and P j that update the value of d s through operations a i and a j, respectively: Operation a i : d s := d s + 10; Let f i (d s ) represent its result Operation a j : d s := d s + 5; Let f j (d s ) represent its result –What situation could arise if they execute concurrently?

7 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.7Operating Systems, by Dhananjay Dhamdhere7 An Example of Race Condition Race conditions in cases 2 and 3

8 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.8Operating Systems, by Dhananjay Dhamdhere8 Race Conditions (continued) Race conditions in Figure 6.2 are prevented by ensuring that operations a i and a j of Definition 6.2 do not execute concurrently –This is called mutual exclusion Data access synchronization is coordination of processes to implement mutual exclusion over shared data

9 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.9Operating Systems, by Dhananjay Dhamdhere9 Critical Sections Mutual exclusion is implemented by using critical sections (CS) of code Using CSs causes delays in operation of processes –Process must not execute for too long inside CS –Process must not make system calls while in the CS that might put it in blocked state –Kernel must not preempt a process that is engaged in executing a CS We use a dashed box in the code to mark a CS

10 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.10Operating Systems, by Dhananjay Dhamdhere10 Critical Sections (continued)

11 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.11Operating Systems, by Dhananjay Dhamdhere11 Properties of a Critical Section Implementation The progress and bounded wait properties together prevent starvation

12 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.12Operating Systems, by Dhananjay Dhamdhere12 Control Synchronization and Indivisible Operations Interacting processes need to coordinate their execution with respect to one another, to perform their actions in a desired order –Requirement met through control synchronization –Signaling is a general technique of control synchronization

13 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.13Operating Systems, by Dhananjay Dhamdhere13 Control Synchronization and Indivisible Operations (continued)

14 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.14Operating Systems, by Dhananjay Dhamdhere14 Control Synchronization and Indivisible Operations (continued) Naive signaling in previous example does not work –P i may face indefinite blocking in some situations Use indivisible or atomic operations instead

15 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.15Operating Systems, by Dhananjay Dhamdhere15 Control Synchronization and Indivisible Operations (continued)

16 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.16Operating Systems, by Dhananjay Dhamdhere16 Synchronization Approaches Looping versus Blocking Hardware Support for Process Synchronization Algorithmic Approaches, Synchronization Primitives, and Concurrent Programming Constructs

17 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.17Operating Systems, by Dhananjay Dhamdhere17 Looping versus Blocking Busy wait: A busy wait has many consequences –Cannot provide the bounded wait property –System performance degradation due to looping –Deadlock –Priority inversion Typically addressed through the priority inheritance protocol

18 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.18Operating Systems, by Dhananjay Dhamdhere18 Looping versus Blocking (continued) To avoid busy waits, a process waiting for entry to a CS is put in blocked state –Changed to ready only when it can enter the CS Process decides to loop or block –Decision is subject to race conditions. Avoided through Algorithmic approach Use of computer hardware features

19 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.19Operating Systems, by Dhananjay Dhamdhere19 Hardware Support for Process Synchronization Indivisible instructions –Avoid race conditions on memory locations Used with a lock variable to implement CS and indivisible operations –entry_test performed with indivisible instruction Test-and-set (TS) instruction Swap instruction

20 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.20Operating Systems, by Dhananjay Dhamdhere20 Hardware Support for Process Synchronization (continued)

21 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.21Operating Systems, by Dhananjay Dhamdhere21 Algorithmic Approaches, Synchronization Primitives, and Concurrent Programming Constructs Algorithmic Approaches –For implementing mutual exclusion –Independent of hardware or software platform Busy waiting for synchronization Synchronization Primitives –Implemented using indivisible instructions –E.g., wait and signal of semaphores Problem: can be used haphazardly Concurrent Programming Constructs –Monitors

22 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.22Operating Systems, by Dhananjay Dhamdhere22 Structure of Concurrent Systems Three key components: –Shared data Application data used and manipulated by processes Synchronization data –Operations on shared data Convenient unit of code which accesses and manipulates shared data –A synchronization operation is on synchronization data –Interacting processes A snapshot of a concurrent system is a view of the system at a specific time instant

23 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.23Operating Systems, by Dhananjay Dhamdhere23 Structure of Concurrent Systems (continued)

24 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.24Operating Systems, by Dhananjay Dhamdhere24 Example: Snapshots of a Concurrent System (a) P i performs operation check_aj (b) P i is blocked; P j performs operation post_aj (c) post_aj activates P i ; P j exits post_aj

25 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.25Operating Systems, by Dhananjay Dhamdhere25 Classic Process Synchronization Problems A solution to a process synchronization problem should meet three important criteria: –Correctness –Maximum concurrency –No busy waits Some classic problems: –Producers-Consumers with Bounded Buffers –Readers and Writers –Dining Philosophers

26 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.26Operating Systems, by Dhananjay Dhamdhere26 Producers-Consumers with Bounded Buffers A solution must satisfy the following: 1.A producer must not overwrite a full buffer 2.A consumer must not consume an empty buffer 3.Producers and consumers must access buffers in a mutually exclusive manner 4.(Optional) Information must be consumed in the same order in which it is put into the buffers

27 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.27Operating Systems, by Dhananjay Dhamdhere27 Producers-Consumers with Bounded Buffers (continued) Suffers from two problems: –Poor concurrency and busy waits

28 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.28Operating Systems, by Dhananjay Dhamdhere28 Producers-Consumers with Bounded Buffers (continued)

29 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.29Operating Systems, by Dhananjay Dhamdhere29 Producers-Consumers with Bounded Buffers (continued)

30 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.30Operating Systems, by Dhananjay Dhamdhere30 Readers and Writers A solution must satisfy the following: 1.Many readers can perform reading concurrently 2.Reading is prohibited while a writer is writing 3.Only one writer can perform writing at any time 4.(optional) A reader has a nonpreemptive priority over writers ─ Called readers preferred readers–writers system

31 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.31Operating Systems, by Dhananjay Dhamdhere31 Readers and Writers (continued)

32 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.32Operating Systems, by Dhananjay Dhamdhere32 Dining Philosophers Design processes to represent the philosophers so that each philosopher can eat when hungry and none dies of hunger –Solution shouldn’t suffer from deadlocks or livelocks

33 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.33Operating Systems, by Dhananjay Dhamdhere33 Dining Philosophers (continued) Prone to deadlocks and race conditions, unless: –If right fork is unavailable, release left fork, retry later It suffers from livelocks

34 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.34Operating Systems, by Dhananjay Dhamdhere34 Dining Philosophers (continued) Problem: loop causes a busy wait condition

35 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.35Operating Systems, by Dhananjay Dhamdhere35 Algorithmic Approach to Implementing Critical Sections Two-Process Algorithms n-Process Algorithms

36 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.36Operating Systems, by Dhananjay Dhamdhere36 Two-Process Algorithms Violates the progress condition

37 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.37Operating Systems, by Dhananjay Dhamdhere37 Two-Process Algorithms (continued) Violates mutual exclusion condition Can lead to deadlock

38 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.38Operating Systems, by Dhananjay Dhamdhere38 Turn is effective only when Both processes try to enter CS at the same time

39 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.39Operating Systems, by Dhananjay Dhamdhere39 Two-Process Algorithms (continued)

40 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.40Operating Systems, by Dhananjay Dhamdhere40 n-Process Algorithms Contains a certain form of unfairness since processes do not enter their CSs in the same order in which they requested entry to a CS.

41 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.41Operating Systems, by Dhananjay Dhamdhere41 (number[j],j) < number[i],i) if number[j] < number[i], or number[j] = number[i] and j < i

42 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.42Operating Systems, by Dhananjay Dhamdhere42 Semaphores Also called counting semaphores due to operations on S

43 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.43Operating Systems, by Dhananjay Dhamdhere43 Uses of Semaphores in Concurrent Systems

44 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.44Operating Systems, by Dhananjay Dhamdhere44 Uses: Mutual Exclusion

45 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.45Operating Systems, by Dhananjay Dhamdhere45 Uses: Bounded Concurrency Implemented by initializing a semaphore sem_c to c Every process wishing to perform op i performs a wait(sem_c) before performing op i and a signal(sem_c) after performing it Up to c processes can concurrently perform op i

46 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.46Operating Systems, by Dhananjay Dhamdhere46 Uses: Signaling between Processes Race conditions cannot arise because the wait and signal operations are indivisible Binary semaphore: Can have values 0 and 1 only

47 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.47Operating Systems, by Dhananjay Dhamdhere47 Producers-Consumers Using Semaphores Avoids busy waits since semaphores are used to check for empty or full buffers Total concurrency in system is 1

48 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.48Operating Systems, by Dhananjay Dhamdhere48 Example: Producers-Consumers with a Single Buffer through Semaphores

49 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.49Operating Systems, by Dhananjay Dhamdhere49 Producers-Consumers Using Semaphores (continued) Solution to n-buffer producers–consumers problem:

50 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.50Operating Systems, by Dhananjay Dhamdhere50 Readers-Writers Using Semaphores Significance of counters: – runread: Number of readers reading – totread: Number of readers wishing to read or reading – Similarly runwrite and totwrite

51

52 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.52Operating Systems, by Dhananjay Dhamdhere52 Implementation of Semaphores Implementations of wait and signal: Kernel-Level: Through system calls User-Level: System calls only to block/activate Hybrid: Combination of the two

53 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.53Operating Systems, by Dhananjay Dhamdhere53 Monitors A monitor type resembles a class in a language like C++ or Java –Contains declarations of shared data –Its procedures encode operations that manipulate shared data and implement process synchronization A condition is a situation of interest in a monitor A condition variable is associated with a condition A process executing a wait operation on condition variable is blocked until some process performs a signal operation In a concurrent system, processes share data by creating a monitor object –We call it simply monitor

54 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.54Operating Systems, by Dhananjay Dhamdhere54 Example: Monitor Implementation of a Binary Semaphore

55 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.55Operating Systems, by Dhananjay Dhamdhere55 Example: Producers-Consumers Using Monitors

56 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.56Operating Systems, by Dhananjay Dhamdhere56 Monitors in Java A Java class becomes a monitor type when the attribute synchronized is associated with one or more methods in the class An object of such a class is a monitor Each monitor contains a single unnamed condition variable –Can lead to busy waits in an application that has many conditions

57 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.57Operating Systems, by Dhananjay Dhamdhere57 Case Studies of Process Synchronization Synchronization of POSIX Threads Process Synchronization in Unix Process Synchronization in Linux Process Synchronization in Solaris Process Synchronization in Windows

58 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.58Operating Systems, by Dhananjay Dhamdhere58 Synchronization of POSIX Threads POSIX threads provide: –Mutexes for mutual exclusion A mutex is a binary semaphore –Condition variables for control synchronization between processes An OS may implement POSIX threads as kernel-level threads or user-level threads

59 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.59Operating Systems, by Dhananjay Dhamdhere59 Process Synchronization in Unix Unix system V provides a kernel-level implementation of semaphores –Name of a semaphore is called a key Key is associated with an array of semaphores Processes share a semaphore by using the same key Unix SVR4 keeps track of all operations performed by a process on each semaphore used by it –Performs an undo on them when process terminates –It helps to make programs more reliable Unix 4.4BSD places a semaphore in shared memory areas and uses a hybrid implementation

60 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.60Operating Systems, by Dhananjay Dhamdhere60 Process Synchronization in Linux Linux has Unix-like semaphore for user processes Also provides semaphores for use by the kernel: –Conventional semaphore Uses efficient kernel-level implementation scheme –Reader–writer semaphore Kernels older than 2.6 implemented mutual exclusion in the kernel space through system calls 2.6 kernel has a fast user space mutex called futex –Uses a hybrid implementation –Provides time-bound wait operation

61 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.61Operating Systems, by Dhananjay Dhamdhere61 Process Synchronization in Solaris Interesting features: –Reader–writer semaphores Analogous to the one in Linux –Adaptive mutexes Useful in a multiprocessor OS –Uses the priority inheritance protocol to reduce synchronization delays –Data structure called a turnstile Holds information concerning threads that are blocked on a mutex or reader–writer semaphore –Used for synchronization and priority inheritance

62 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.62 Process synchronization in Windows Dispatcher object is embedded in every object over which synchronization is needed –It can be in signaled/nonsignaled state –Dispatcher objects used in process, file, etc. (next slide) Provides spinlock, queued spinlock, fast mutex and push locks Vista provides a reader-writer lock Operating Systems, by Dhananjay Dhamdhere62

63 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.63Operating Systems, by Dhananjay Dhamdhere63 Process Synchronization in Windows

64 Operating Systems, by Dhananjay Dhamdhere Copyright © 20086.64Operating Systems, by Dhananjay Dhamdhere64 Summary Process synchronization is a generic term for data access synchronization and control synchronization A race condition occurs when actions of concurrent processes may have unexpected consequences –Avoided through mutual exclusion Avoidance of race conditions is a primary issue in process synchronization Critical section: section of code that accesses some shared data in a mutually exclusive manner Synchronization achieved through: indivisible instructions, semaphores, monitors


Download ppt "Chapter 6 Process Synchronization Copyright © 2008."

Similar presentations


Ads by Google