目录
1.1 What is Process Synchronization? 什么是进程同步
2. The Critical-Section Problem 临界区问题
3. Solutions for critical-section problem 临界区问题解决办法
3.3 Mutex locks / Mutual exclusion 互斥锁
4. Classical Problems of Synchronization 同步的经典问题
4.1 The Bounded-Buffer / Producer-Consumer Problem 有限缓冲问题
4.2 The Readers–Writers Problem 读者-写者问题
4.3 The Dining-Philosophers Problem 哲学家进餐问题
1. Background 背景
1.1 What is Process Synchronization? 什么是进程同步
PS is the task of coordinating the execution of processes in a way that no two processes can have access to the same shared data and resources. 两个进程不能同时访问共享资源。
1.2 Race condition 竞争条件
Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. 对共享数据的并发访问可能导致数据的不一致。
Race condition: The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. 指的是几个进程并发访问和操作共享数据。共享数据的最终值取决于哪个进程最后完成。
To prevent race conditions, concurrent processes must be synchronized. 为了防止竞争,必须同步并发的进程
1.3 Syncronization 同步
同步发生在协作进程之间,基础思想是需要一个任何形式的锁。
2. The Critical-Section Problem 临界区问题
Each (concurrent) process has a code segment, called Critical Section (CS), in which the shared data is accessed. When using critical sections, the code can be broken down into the following sections:
The critical-section problem is to design a protocol that the processes can use to cooperate. Each process must request permission to enter its critical section.
- 启用这个请求的代码段叫做entry section
- entry section 后面会有exit section
- entry和exit中间的进程代码段叫做critical section
- 剩下的代码叫做remainder section
Race condition updating a variable
Critical section to prevent a race condition
Multiprogramming allows logical parallelism, uses devices efficiently but we lose correctness when there is a race condition. So, we forbid/deny logical parallelism inside critical section, so we lose some parallelism, but we regain correctness.
A solution to the critical-section problem must satisfy the following three requirements (临界区的三个原则)
- Mutual exclusion. 互斥 If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. 如果已有进程在临界区执行,其他进程不能在其临界区执行。
- Progress. 前进 If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in deciding which will enter its critical section next, and this selection cannot be postponed indefifinitely. 如果没有进程在临界区,其他进程应该被允许进入临界区。
- Bounded waiting. 有效等待 There exists a bound, or limit, 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. 进程做出进入临界区请求后,其他进程进入临界区的次数是有上限的,进程发出请求后等待允许的时间有限。
Types of solutions to CS problem
- Software solutions: algorithms whose correctness relies only on the assumption that only one process/thread at a time can access a memory location/resource.
- Hardware solutions: rely on special machine instructions for “locking”.
- Operating System and Programming Language solutions (e.g.