Overview
Background
Consumer & Producer Problem
Concurrent Operations on counter
Instruction Interleaving
Race Condition
Critical Section
The Critical-Section Problem
Critical Section Requirements
Critical Section Solutions & Synchronization Tools
Software Solution
Algorithm for Two Processes
Peterson’s Solution for Two Processes
Proof of Peterson’s Solution
Producer/Consumer Problem
Bakery Algorithm (n processes)
Pthread Lock/Mutex Routines
Synchronization HW
Hardware Support
缺点:忙等,饥饿,死锁
当低优先级进程使用临界区,高优先级进程使用CPU时候,会产生死锁
Atomic TestAndSet()
Atomic Swap()
Semaphores
POSIX Semaphore
n-Process Critical Section Problem
Non-busy/waiting Implementation
wait操作:申请资源且可能阻塞自己 (s < 0), 在进入临界区之前调用
signal操作:释放资源并唤醒阻塞进程(s <= 0)
1到 n-1