# Critical Section
## Critical-Section Problem
### Purpose
- 一protocol以讓processes去合作
### Problem description
- 多個processes競爭使用shared data
- 每個process有一個==code segment==,稱為critical section,在這裡shared data被存取
- 需要確保當有一個行程正在執行他的critical section,其他的行程不允許執行他自己的CS ---> ==mutually exclusive==
### General code section structure
```java
do {
entry section // get entry permission
critical section // modify shared data
exit section // release entry permission
remainder section
} while (1);
```
---
## Critical Section Requirements
1. Mutual Exclusion
- 當有process P正在執行他的CS,其他process不能執行他們的CS
2. Progress
- 如果沒有process正在執行他的CS和有些process想要執行他們的CS,這些processes不能被無限期地postponed
3. Bounded Waiting
- 跑完之後,若想連續進去,如果有其他的已經在排隊,不可以插隊,要去隊伍最後面排隊
- ==A bound== must exist on the number of times that ==other processes are allowed to enter their CS== after a process has made a request to enter its CS