Synchronisation
แนะนำใหม่
6:44
ระบบปฏิบัติการ
An operating system turns one processor into many machines. The first three units build the illusions of the processor: processes, taking turns, and safe…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| interleaving/ˌɪntəˈliːvɪŋ/ | the order in which the steps of concurrent threads happen to mix |
| race condition/reɪs kənˈdɪʃn/ | a bug in which the result depends on the timing of threads that share data |
| Mutual exclusion/ˈmjuːtʃuːəl eksˈkluːʒn/ | |
| spin/spɪn/ | การหมุนวน |
| semaphore/ˈseməfɔː/ | a counter with wait and signal operations, used to control access to shared resources |
| mutex/ˈmjuːteks/ | a lock that lets only one thread at a time into a critical section |
| Deadlock/ˈdedlɒk/ | a state in which each process waits for a resource held by another, so none can continue |
| condition variable/kənˈdɪʃn ˈveərɪəbl/ | A synchronisation object a thread waits on, releasing its lock, until another thread signals that a condition may now be true. |
| banker's algorithm/ˈbæŋkəz ˈælɡərɪθəm/ | A deadlock-avoidance method that grants a resource request only if some order still lets every process finish. |
The lost update
- Two threads run
counter = counter + 1withcounter = 5. Each one reads, adds and writes. The interleaving 交错执行 R1 R2 W1 W2 leaves $6$, not $7$: one update is lost. - With four increments in each thread, starting from $0$, every final value from $2$ to $8$ can happen: six of the eight increments can vanish. This is a race condition 竞态条件.

Two threads each add 1 to counter = 5 with read-then-write. The unlocked interleaving R1 R2 W1 W2 leaves —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Both reads see 5; both writes store 6 — one increment lost.
What a lock must guarantee
- A lock must guarantee three things. Mutual exclusion 互斥: only one thread inside at a time. Progress: if the lock is free, someone gets in. Bounded waiting: no thread waits for ever.
- Peterson's algorithm meets all three in software for two threads; a basic
test-and-setspinlock provides mutual exclusion but does not guarantee bounded waiting. Both spin 自旋: they burn the CPU while waiting. - A semaphore 信号量 or mutex 互斥锁 adds a queue in the OS: a blocked thread sleeps instead of spinning. Real systems combine both.
A spinning lock burns CPU; a mutex queue avoids it by —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The blocked waiter sleeps in the OS queue instead of spinning.
The four conditions for deadlock
- A holds L1 and wants L2; B holds L2 and wants L1. Deadlock 死锁 needs all four of mutual exclusion, hold-and-wait, no preemption and circular wait. Break any one and it cannot happen.
- Engineers usually break circular wait by giving the locks an order (always L1 before L2). Adding a sleep does not fix a race; it only makes the window smaller.
A machine with a single core can still have races: a switch between the read and the write is enough.
Ordering all lock acquisitions breaks —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A fixed order makes a cycle impossible — the cheapest condition to break.
Producer and consumer
- A bounded buffer of $4$ slots uses three semaphores:
empty = 4,full = 0,mutex = 1. Each side waits for its resource first, then the lock:
producer: consumer:
wait(empty) wait(full)
wait(mutex) wait(mutex)
put item take item
signal(mutex) signal(mutex)
signal(full) signal(empty)
- The order matters. If the producer takes
mutexbeforeemptywhile the buffer is full, it sleeps holding the lock, and the consumer can never get in to make room: deadlock. Counting semaphores track resources; the mutex protects only the inside of the buffer.
The 4-slot buffer starts empty. The producer puts 3 items and the consumer takes 1. What is the value of empty?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
empty = 4 - 3 + 1 = 2 free slots.
The producer calls wait(mutex) ก่อน wait(empty). What can happen when the buffer is full?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The consumer needs the mutex to remove an item, but the sleeping producer holds it.
Put the producer's steps for a bounded buffer in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Waiting on empty before taking mutex matters: a producer that sleeps on a full buffer while holding mutex locks the consumer out for ever.
Waiting for a condition
- A condition variable 条件变量 lets a thread sleep until something is true. It releases the lock while it waits and takes it back when woken:
while (count == 0) wait(not_empty, lock); - It must be a
while, not anif. Another thread may take the item between the signal and the wake-up, and a thread can also wake for no reason, so check the condition again every time.
Checking the condition once with if ก่อน wait is enough.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The condition can change before the woken thread runs; loop with while.
Worked example: the banker's safe state
- There are ten units. Each process declares the most it may need; 3 + 2 + 2 = 7 are held, so 3 are free.
| Process | May need | Holds | Still needs |
|---|---|---|---|
| P0 | 9 | 3 | 6 |
| P1 | 4 | 2 | 2 |
| P2 | 7 | 2 | 5 |
- Look for a process whose need fits in the free units. P1 (2 ≤ 3) takes 2 more, finishes and returns all 4: 1 + 4 = 5 free. P2 (5 ≤ 5) does the same: 7 free. P0 (6 ≤ 7) finishes. The order P1, P2, P0 exists, so the state is safe.
- If P0 asks for one more unit, 2 remain: P1 can finish (4 free), but P0 needs 5 and P2 needs 5. No order completes, so the banker's algorithm 银行家算法 refuses the request before deadlock can happen.
In the safe state above, which process can finish first?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
P1 needs only 2 more and 3 are free.
The banker's algorithm grants P0's request for one more unit.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
It would leave 2 free and no completion order: an unsafe state, so the request waits.
Marks that slip away
- Sleeping to fix a race. A sleep only makes the window smaller; the bad interleaving can still happen.
- Locks in different orders. A holds L1 and wants L2 while B holds L2 and wants L1: deadlock. Always take L1 first.
wait(mutex)beforewait(empty). A producer that sleeps on a full buffer while holding the mutex locks the consumer out.
Recap
- Races lose updates: R1 R2 W1 W2 turns $5$ into $6$, not $7$.
- A lock needs mutual exclusion, progress and bounded waiting; blocking locks sleep instead of spinning.
- Deadlock needs all four conditions; ordering the locks breaks circular wait.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้