Académique Documents
Professionnel Documents
Culture Documents
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
Process 2
Read/write
Shared data
Process 3
Chapter 7
Y. W. Leung
2.
Sometimes certain processes must be properly synchronized. Example: Four processes must execute four functions in the following order:
Solution: We will study some concurrency control methods to solve the above problems.
Chapter 7 5 Y. W. Leung
Chapter 7
Y. W. Leung
Read account balance ($150) Add $50 to the account balance Save the new balance $200 time
Chapter 7 7 Y. W. Leung
Chapter 7
Y. W. Leung
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
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.
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.
Chapter 7
16
Y. W. Leung
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
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
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
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
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
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
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
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
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
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.
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().
6.
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