Vous êtes sur la page 1sur 49

Chapter 7: Concurrency Control

Chapter 7

Y. W. Leung

1. Introduction
In this chapter, a process refers to any basic unit of execution, and all program segments are given as pseudo code. Background 1: Some OS processes may share data.
Example: An OS records the data (information) about all the opened files. When multiple OS processes are operating on these files, they share the above data.

Chapter 7

Y. W. Leung

Background 2: Different processes may have different and unpredictable speed of execution.
Example 1: If a process involves more disk I/O operations, its execution speed is slower.

Example 2: If a process involves more users inputs, its execution speed is more unpredictable.

Chapter 7

Y. W. Leung

Concurrency control problems:


1. When multiple concurrent processes read and write shared data, the data may become incorrect (see the example in section
1.1).
Process 1
Rea d/w rite

Process 2

Read/write

Shared data

Process 3

Problem 1: How to ensure that the shared data is correctly maintained?

Chapter 7

Y. W. Leung

2.

Sometimes certain processes must be properly synchronized. Example: Four processes must execute four functions in the following order:

Problem 2: How to synchronize the processes?

Solution: We will study some concurrency control methods to solve the above problems.
Chapter 7 5 Y. W. Leung

1.1 Example of Incorrect Data Update


Suppose two users deposit money into a bank account as follows.

User A Deposit $50 Run process A to update the balance

User B Deposit $50 Run process B to update the balance

Chapter 7

Y. W. Leung

Case 1: Sequential Processes


If processes A and B are executed sequentially as follows, the resulting account balance is $200 which is correct.
Process B

Read account balance ($150) Add $50 to the account balance Save the new balance $200 time
Chapter 7 7 Y. W. Leung

Case 2: Concurrent Processes


If processes A and B are executed concurrently as follows, the resulting account balance is $150 which is incorrect.

Chapter 7

Y. W. Leung

2. Critical Section Problem


Daily Life Analogy
Multiple people share a toilet. The toilet is a critical section. To ensure privacy, only one people can use the toilet at a time. Problem: When one people is using the toilet, how to ensure that no other people can use the toilet?

Chapter 7

Y. W. Leung

Critical Section
Suppose multiple processes share certain data. Each process has a code segment, called a critical section, in which the process reads or writes the shared data. Example: Suppose balance is a shared data. The following shows a critical section. { ... balance = balance + 1; ... }
This code segment accesses the shared data, so it is a critical section.

Chapter 7

10

Y. W. Leung

Critical Section Problem


To ensure data correctness, only one process can execute its critical section at a time. In this manner, the shared data can be correctly maintained and updated. Critical section problem: When one process is executing its critical section, how to ensure that no other process can execute its critical section?

Chapter 7

11

Y. W. Leung

For convenience of explanation, a program and its critical section are presented in the following form:
do { entry section critical section exit section remainder section } while (TRUE) ;

Chapter 7

12

Y. W. Leung

Atomic Instruction
Special hardware/software support is needed to solve the critical section problem. An atomic instruction has the following properties:
1. Once an atomic instruction is executed, it is executed to completion and its execution is not interrupted.

Chapter 7

13

Y. W. Leung

2.

If two processes execute the same atomic instruction on two processors, the execution is sequential (i.e., one processor
executes the atomic instruction, and then the other processor executes the atomic instruction).

In the following, we will study how atomic instructions can solve the critical section problem.

Chapter 7

14

Y. W. Leung

3. Test-and-Set
Main Idea Daily Life Analogy
The toilet has a hardware lock. A user uses the toilet as follows:
Wait until the lock of the toilet is not locked. Lock the lock.

Use the toilet.


Unlock the lock.

Chapter 7

15

Y. W. Leung

Main Idea
The computer provides a hardware lock. A process executes its critical section as follows:

Wait until the lock of the critical section is not locked. Lock the lock.

Execute the critical section.


Unlock the lock.

Execute the remainder section.

Chapter 7

16

Y. W. Leung

3.1 Test-and-Set Instruction


TestAndSet is a hardware atomic instruction to manage

the lock.

If lock=TRUE, this means that the lock is locked, otherwise, the lock is not locked.
TestAndSet(lock) provides two functions: 1. 2. Test: If the lock is not locked (i.e., lock=FALSE), the function returns FALSE; otherwise, it returns TRUE. Set: If the lock is not locked, lock the lock (i.e., set lock to TRUE).

Chapter 7

17

Y. W. Leung

A process uses the TestAndSet instruction to solve the critical section problem as follows (the initial value of lock is FALSE): do { while ( TestAndSet(lock) ) ; critical section; lock = FALSE ; remainder section; } while (TRUE)

Chapter 7

18

Y. W. Leung

3.2 Solution with Bounded Waiting Time


Unbounded Waiting Time Problem Daily Life Analogy

Toilet is free

Peter finds that the toilet is locked, so he goes back to his office to wait. In between, the toilet is released. Then Mary uses it. Peter comes back but finds that the toilet is locked, so he goes back to his office to wait. In this manner, Peter may wait indefinitely.
19 Y. W. Leung

Chapter 7

Unbounded Waiting Time Problem


Using the previous TestAndSet solution, when a process wants to execute its critical section, it may wait indefinitely. Example

Critical section is being executed by process B

Process A finds that the lock is locked, so it waits. In between, the lock is unlocked. Then another process B locks it. Process A checks again but finds that the lock is locked, so it waits. In this manner, process A may wait indefinitely.
20 Y. W. Leung

Chapter 7

Solution with Bounded Waiting Time Daily Life Analogy


There are 5 users and 1 toilet. Post the following notice on the toilet door:
User user 0 user 1 user 2 user 3 user 4 Waiting for toilet? false false false false false

Suppose user 2 is using the toilet. When users 1 and 4 want to use the toilet, they update the notice to:
User user 0 user 1 user 2 user 3 user 4 Waiting for toilet? false true false false true
21 Y. W. Leung

Chapter 7

When user 2 has finished using the toilet, he selects the next waiting user in a round robin order (i.e., selects user 4):
User user 0 user 1 user 2 user 3 user 4 Waiting for toilet? false true false false true User 2 finishes and selects the next waiting user in round robin order

In the worst case when all other users have used the toilet, a user can certainly use the toilet. The waiting time is bounded.

Chapter 7

22

Y. W. Leung

Solution with Bounded Waiting Example


There are 5 processes and 1 critical section. Suppose process P2 is executing the critical section. When processes P1 and P4 want to execute the critical section, the system maintains the following information:

Chapter 7

23

Y. W. Leung

When process P2 has completed executing the critical section, the system selects the next waiting process in a round robin order (i.e., selects process P4):

In the worst case when all other processes have executed the critical section, a process can certainly execute the critical section. The waiting time is bounded.

Chapter 7

24

Y. W. Leung

Solution with Bounded Waiting


When process Pi wants to execute its critical section, it sets waiting[i]=true. When process Pi has completed executing its critical section, it sets waiting[i]=false, and it selects the next waiting process in a round robin order such that the selected process can execute its critical section.

Chapter 7

25

Y. W. Leung

4. Semaphore
Daily Life Analogy
To use a toilet, a key must be used. Let S be the number of available keys for the toilet. Wait operation: If no key is available (i.e., S0), a user waits; otherwise, he gets this key (i.e., SS 1) to use the toilet. Signal operation: After using the toilet, the user returns the key (i.e., SS + 1) and informs (signals) the waiting users.

Chapter 7

26

Y. W. Leung

A semaphore S is an integer variable which can only be accessed by two atomic operations (apart from initialization): 1. wait(S): while ( S <= 0 ) ; S=S1; 2. signal(S): S=S+1; When one process modifies S, no other process can modify S simultaneously.
Chapter 7 27 Y. W. Leung

4.1 Solving Critical Section Problem


Daily Life Analogy
Toilet is a critical section and it has one key. Each user enters this critical section in the following manner, so that at most one user can use the toilet at a time.
do { A user waits until getting the key; Enter and use the toilet; The user returns the key and signals the other users; Do other things; } while (TRUE)
Chapter 7 28 Y. W. Leung

Semaphore for Solving Critical Section Problem


Let S be a binary semaphore defined as follows:
S=1: no process is executing its critical section. S=0: one process is executing its critical section.

S is initialized to 1. Each process enters the critical section in the following manner, so that at most one process can execute the critical section at a time. do { wait(S); critical section; signal(S); remainder section; } while ( TRUE ) ;
Chapter 7 29 Y. W. Leung

4.2 Process Synchronization


In some applications, certain processes must be properly synchronized. Example Four processes must execute four functions in the following order:

Chapter 7

30

Y. W. Leung

Daily Life Analogy Problem: How to ensure the following usage order?
First Susan uses the toilet. Then Peter uses the toilet.

Solution: Initially, the key is given to Susan. Susan does the following: Use the toilet; Signal to Peter and return the key; Peter does the following: Wait until getting the key; Use the toilet;

Chapter 7

31

Y. W. Leung

Semaphores can be applied for process synchronization. To illustrate, we apply semaphores to solve the following process synchronization problem: Problem for illustration: How to ensure the following execution order? First, process P1 executes the function SolveEquation1() until completion. Then process P2 executes the function SolveEquation2().

Chapter 7

32

Y. W. Leung

Solution:
Let S be a binary semaphore. It is initialized to 0. Process P1 executes the following statements: SolveEquation1(); signal(S); Process P2 executes the following statements: wait(S); SolveEquation2;

Chapter 7

33

Y. W. Leung

4.3 Resource Sharing


Daily Life Analogy Problem: How can N users share M toilets? Solution: Let there be M keys for the M respective toilets. A user acquires one of the M toilets as follows:
Wait until getting one of the M keys; Acquire the corresponding toilet;

The user uses the toilet and then releases it as follows:


Use the toilet until completion; Signal to other users and release the key of this toilet;

Chapter 7

34

Y. W. Leung

Problem: How can N processes share M units of resources? Solution: Let S be a semaphore. It is initialized to M. A process acquires one unit of resource as follows: wait(S); Acquire one unit of resource; A process uses the resource and then releases it as follows: Use one unit of resource; signal(S);

Chapter 7

35

Y. W. Leung

4.4 Producer-Consumer Problem


Some interprocess communication problems can be modeled as follows: A producer produces data and writes it to a buffer. A consumer reads & removes this data from the shared buffer and consumes it.

Chapter 7

36

Y. W. Leung

Problem: Suppose the shared buffer can store N data items. How can the producer transfer more than N data items to the consumer? Solution Example: Suppose a buffer can store 2 data items and the producer is going to transfer 3 data items (a[0], a[1], a[2]) to the consumer.

Chapter 7

37

Y. W. Leung

Initially, the buffer has free space, so the producer writes one data item a[0] to the buffer:

Then the buffer still has free space, the producer writes another data item a[1] to the buffer:

Chapter 7

38

Y. W. Leung

Now the buffer is full, so the producer cannot write data to it. The consumer reads one data item a[0] from the buffer:

d Re a

a dat e n o

ite

[0] a m

The buffer has free space again, so the producer can write the next data item a[2] to the buffer:

Chapter 7

39

Y. W. Leung

Then the consumer reads a[1] and a[2] from the buffer:

d Re a

a dat e n o

ite m

] a[1

dat ne o d Re a

] a[2 m e a it

Chapter 7

40

Y. W. Leung

Solution Main Ideas: The buffer is operated as a circular buffer. Writing: When the buffer is not full, the producer can write a data item to the buffer; otherwise, it waits. Reading: When the buffer is not empty, the consumer can read and remove a data item from the buffer.

Chapter 7

41

Y. W. Leung

Solution Using Semaphore: To solve the producer-consumer problem, three semaphores are used:
1. 2. 3. mutex: This semaphore supports mutual exclusion for accessing the shared buffers. It is initialized to 1. empty: This semaphore counts the number of empty buffers. It is initialized to the buffer size. occupied: This semaphore counts the number of occupied buffers. It is initialized to 0.

Chapter 7

42

Y. W. Leung

Producer: do { Produce a data item and store it in DataItem; wait ( empty ) ; wait ( mutex ) ; Write DataItem to buffer; signal ( mutex ) ; signal ( occupied ) ; } while (TRUE)

Chapter 7

43

Y. W. Leung

Consumer: do { wait ( occupied ) ; wait ( mutex ) ; Read and remove a data item from buffer and store it in DataItem; signal ( mutex ) ; signal ( empty ) ; Consume the data item in DataItem; } while (TRUE)

Chapter 7

44

Y. W. Leung

Tutorial Problems
1. In a bank, multiple processes may modify the balance of an account (e.g., the balance is increased upon deposit or decreased upon withdrawal). How should the processes access the account balance to ensure data correctness? Suppose there are N processes and 2 critical sections where each critical section involves one distinct set of shared data. a) Apply TestAndSet() to tackle this problem. b) Apply semaphores to tackle this problem.

2.

Chapter 7

45

Y. W. Leung

3.

A computer runs a multi-user OS and it has a software with N licenses. The license count is updated as follows:
When this software is run, the license count is decremented by one. When this software is terminated, the license count is incremented by one. When the license count is 0, no user can run this software.

Since multiple users may want to run or terminate the software, multiple processes may read and write the license count. Suggest how you can ensure that the license count is correctly maintained.

Chapter 7

46

Y. W. Leung

4.

Suppose processes A, B and C must be synchronized as follows:


First, process A executes X1(). Then process B executes X2(). Then process C executes X3().

Apply semaphores to solve the above synchronization problem. 5. Suppose processes A, B, C and D must be synchronized as follows:
First, process A executes Y1(). Then process B executes Y2() and process C executes Y3() concurrently. Then process D executes Y4().

Apply semaphore to solve the above synchronization problem


Chapter 7 47 Y. W. Leung

6.

Suppose processes A and B must be synchronized as follows:


First, process A executes SolveEquation1(); Then process B executes SolveEquation2(); Then process A executes SolveEquation3(); Then process B executes SolveEquation4();

Apply semaphore to solve the above synchronization problem.

Chapter 7

48

Y. W. Leung

7.

Process A transfers money from account X1 to account X2 and process B transfers money from account X2 to account X1 as follows:
Process A Process B

Lock account X1; Lock account X2; Transfer $ from X1 to X2; Release account X1; Release account X2;

Lock account X2; Lock account X1; Transfer $ from X2 to X1; Release account X2; Release account X1;

a) Apply semaphores to realize the above money transfer. b) Discuss the problem in the above process coordination.
Chapter 7 49 Y. W. Leung

Vous aimerez peut-être aussi