作業系統基本觀念複習
DESCRIPTION
基本作業系統觀念TRANSCRIPT
![Page 1: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/1.jpg)
作業系統基本觀念複習 Process & Thread
Yiling Lai
2011/8/24
1
![Page 2: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/2.jpg)
• 恐龍書
Operating System Concepts
Silberschatz, Galvin, Gagne
2
![Page 3: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/3.jpg)
什麼是Process?
• A program in execution. 執行中的程式
3
![Page 4: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/4.jpg)
什麼是Process?
• A program in execution. 執行中的程式
3
![Page 5: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/5.jpg)
什麼是Process?
• A program in execution. 執行中的程式
3
![Page 6: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/6.jpg)
什麼是Process?
• A program in execution. 執行中的程式
3
Text Section
Data Section
Heap
Stack
Program counter
![Page 7: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/7.jpg)
什麼是Process?
• A program in execution. 執行中的程式
3
Text Section
Data Section
Heap
Stack
Program counter
Registers
![Page 8: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/8.jpg)
Process的一生 (STD)
New
4
Job Queue
![Page 9: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/9.jpg)
Process的一生 (STD)
New Ready
Load到memory裡,準備搶CPU控制權
4
Ready Queue
Job Queue
![Page 10: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/10.jpg)
Process的一生 (STD)
New Ready
Load到memory裡,準備搶CPU控制權
4
Ready Queue
PCB
Process ID Process State
PC CPU Registers
….
Job Queue
![Page 11: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/11.jpg)
Process的一生 (STD)
New Ready Running
Load到memory裡,準備搶CPU控制權
搶到CPU了
4
Ready Queue
PCB
Process ID Process State
PC CPU Registers
….
Job Queue
![Page 12: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/12.jpg)
Process的一生 (STD)
New Ready Running
Wait (Block)
Wait for I/O complete or resources available
Load到memory裡,準備搶CPU控制權
搶到CPU了
4
Ready Queue
PCB
Process ID Process State
PC CPU Registers
….
Job Queue
![Page 13: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/13.jpg)
Process的一生 (STD)
New Ready Running
Wait (Block)
Wait for I/O complete or resources available
Load到memory裡,準備搶CPU控制權
CPU被搶走了
搶到CPU了
4
Ready Queue
PCB
Process ID Process State
PC CPU Registers
….
Job Queue
![Page 14: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/14.jpg)
Process的一生 (STD)
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
Load到memory裡,準備搶CPU控制權
CPU被搶走了
搶到CPU了
4
Ready Queue
PCB
Process ID Process State
PC CPU Registers
….
Job Queue
![Page 15: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/15.jpg)
Process的一生 (STD)
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
Load到memory裡,準備搶CPU控制權
CPU被搶走了
搶到CPU了
4
Ready Queue
PCB
Process ID Process State
PC CPU Registers
….
Job Queue
![Page 16: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/16.jpg)
Process的一生 (STD)
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
Load到memory裡,準備搶CPU控制權
CPU被搶走了
我等著你回來~~ (好無聊啊!!)
搶到CPU了
4
Ready Queue
PCB
Process ID Process State
PC CPU Registers
….
Job Queue
![Page 17: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/17.jpg)
多找幾個行程來填補我的空虛與寂寞!! (Concurrent Processing)
5
![Page 18: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/18.jpg)
多找幾個行程來填補我的空虛與寂寞!! (Concurrent Processing)
你這個詐騙集團!! 我才是真的平行化好嘛!
5
![Page 19: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/19.jpg)
Parallel Processing
多找幾個行程來填補我的空虛與寂寞!! (Concurrent Processing)
你這個詐騙集團!! 我才是真的平行化好嘛!
P0
P1
P2
5
![Page 20: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/20.jpg)
Parallel Processing Concurrnt Processing
P0
P1
P2
多找幾個行程來填補我的空虛與寂寞!! (Concurrent Processing)
你這個詐騙集團!! 我才是真的平行化好嘛!
P0
P1
P2
5
![Page 21: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/21.jpg)
Parallel Processing Concurrnt Processing
P0
P1
P2
大家輪流使用CPU,但是怎麼輪??
多找幾個行程來填補我的空虛與寂寞!! (Concurrent Processing)
你這個詐騙集團!! 我才是真的平行化好嘛!
P0
P1
P2
5
![Page 22: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/22.jpg)
6
![Page 23: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/23.jpg)
6
![Page 24: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/24.jpg)
6
![Page 25: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/25.jpg)
我就知道沒有我果然不行,交給我吧!!
6
![Page 26: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/26.jpg)
7
![Page 27: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/27.jpg)
1. CPU的使用率(Utilization)要最大 2. 工作產能(Throughput)要高 3. Process等待的時間要短 4. 完成時間(Turnaround time)要短 5. 資源利用率也要大 6. 要公平 7. …
7
![Page 28: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/28.jpg)
1. CPU的使用率(Utilization)要最大 2. 工作產能(Throughput)要高 3. Process等待的時間要短 4. 完成時間(Turnaround time)要短 5. 資源利用率也要大 6. 要公平 7. …
• FIFO: First come first out • SJF: Shortest Job First • SRJF: Shortest Remaining Time Job First • Priority Scheduling • RR: Round Robin • …..
7
![Page 29: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/29.jpg)
1. CPU的使用率(Utilization)要最大 2. 工作產能(Throughput)要高 3. Process等待的時間要短 4. 完成時間(Turnaround time)要短 5. 資源利用率也要大 6. 要公平 7. …
• FIFO: First come first out • SJF: Shortest Job First • SRJF: Shortest Remaining Time Job First • Priority Scheduling • RR: Round Robin • …..
7
Scheduling Algorithm
![Page 30: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/30.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 31: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/31.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2
P0
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 32: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/32.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2
P1 P0
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 33: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/33.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 34: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/34.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2 平均等待時間 = (0+ 14+19)/3 = 11
P2 P1 P0 2 5 14
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 35: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/35.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2 平均等待時間 = (0+ 14+19)/3 = 11
P2 P1 P0 2 5 14
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 36: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/36.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2 平均等待時間 = (0+ 14+19)/3 = 11
P2
P2 P1 P0 2 5 14
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 37: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/37.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2 平均等待時間 = (0+ 14+19)/3 = 11
P2 P1
P2 P1 P0 2 5 14
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 38: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/38.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2 平均等待時間 = (0+ 14+19)/3 = 11
P2 P1 P0
P2 P1 P0 2 5 14
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 39: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/39.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2
平均等待時間 = (0+2+7)/3 = 3
平均等待時間 = (0+ 14+19)/3 = 11
P2 P1 P0 2 5 14
P2 P1 P0 2 5 14
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 40: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/40.jpg)
FIFO: 誰先來誰先做
SJF: 你要3個小時?我只要3分鐘,先給我用一下吧~
(SJF: shortest time job first)
Process CPU Time
P0 14
P1 5
P2 2
平均等待時間 = (0+2+7)/3 = 3
平均等待時間 = (0+ 14+19)/3 = 11
P2 P1 P0 2 5 14
P2 P1 P0 2 5 14
1. CPU的使用率要最大 2. 工作產能要高 3. Process等待的時間要短 4. 完成時間要短 5. 資源利用率也要大 6. 要公平
FIFO:
SJF:
8
![Page 41: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/41.jpg)
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
9
![Page 42: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/42.jpg)
P2 P1
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
9
![Page 43: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/43.jpg)
P2 P1
P3
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
9
![Page 44: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/44.jpg)
P3
P2 P1
P2 P1
P3
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
9
![Page 45: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/45.jpg)
P3
P2 P1
P2 P1
P3
P4
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
9
![Page 46: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/46.jpg)
P3
P2 P1
P2 P1
P3
P4 PN ….
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
9
![Page 47: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/47.jpg)
P3
P2 P1
P2 P1
P3
P4 PN ….
P4 P2 P1 …. PN
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
9
![Page 48: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/48.jpg)
P3
P2 P1
P2 P1
P3
P4 PN ….
P4 P2 P1 …. PN
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
喂…那我咧…
P0
9
![Page 49: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/49.jpg)
P3
P2 P1
P2 P1
P3
P4 PN ….
P4 P2 P1 …. PN
Process CPU Time
P0 14
P1 5
P2 2
P2 P1 P0
喂…那我咧…
P0
Starvation 飢餓:Process因為長期無法取得完工所需的全部資源,以致形成indefinite blocking之現象。 解法:Aging Tech等…
9
![Page 50: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/50.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P0
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 51: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/51.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P0
P1
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 52: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/52.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P0
P1
P0 P1
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 53: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/53.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P2
P0
P1
P0 P1
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 54: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/54.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P2
P0
P1
P0 P1
P0 P1
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 55: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/55.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P2
P0
P1
P0 P1
P0 P1
P3
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 56: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/56.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P2
P0
P1
P0 P1
P0 P1
P3
P0 P1
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 57: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/57.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P2
P0
P1
P0 P1
P0 P1
P3
P0 P1
P2
P0
P3
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 58: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/58.jpg)
SRJF: Shortest Remaining Time Job First
• 可插隊的。
Process Arrival Time
CPU Time
P0 0 6
P1 1 4
P2 2 7
P3 3 3
P2
P0
P1
P0 P1
P0 P1
P3
P0 P1
P2
P0
P3
P0 P1 P3 P0 P2
T0 T1 T2 T3
10
T1
T2
T3
T5
T20
![Page 59: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/59.jpg)
New Ready Running Terminate
Wait (Block)
Load到memory裡,準備搶CPU控制權
CPU被搶走了
搶到CPU了
Registers
11
PCB
Process ID ReProcess State
PC CPU gisters
….
![Page 60: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/60.jpg)
New Ready Running Terminate
Wait (Block)
Load到memory裡,準備搶CPU控制權
CPU被搶走了
搶到CPU了
Registers
11
PCB
Process ID ReProcess State
PC CPU gisters
….
![Page 61: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/61.jpg)
New Ready Running Terminate
Wait (Block)
Load到memory裡,準備搶CPU控制權
CPU被搶走了
搶到CPU了
Registers
11
PCB
Process ID ReProcess State
PC CPU gisters
….
![Page 62: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/62.jpg)
New Ready Running Terminate
Wait (Block)
Load到memory裡,準備搶CPU控制權
CPU被搶走了
搶到CPU了
Registers
Context Switching:若CPU將執行中的Process切換給其他Process使用時,必須保存目前執行中Process的狀態,並載入欲執行Process的狀態資訊。
11
PCB
Process ID ReProcess State
PC CPU gisters
….
![Page 63: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/63.jpg)
問題是搬家是需要成本的…
P0
P1
P2
12
![Page 64: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/64.jpg)
問題是搬家是需要成本的…
P0
P1
P2
太多Process並行效能反而不如預期…
12
![Page 65: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/65.jpg)
問題是搬家是需要成本的…
P0
P1
P2
太多Process並行效能反而不如預期…
解法1:提供多套Registers
Registers
12
![Page 66: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/66.jpg)
問題是搬家是需要成本的…
P0
P1
P2
太多Process並行效能反而不如預期…
解法1:提供多套Registers
Registers
12
![Page 67: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/67.jpg)
問題是搬家是需要成本的…
P0
P1
P2
太多Process並行效能反而不如預期…
解法1:提供多套Registers
Registers
解法2:改用Thread~
Thread: Light weight process. 是CPU分配資源的最小單位,而同一個Process內的threads共享code section, data section, 跟一些OS資源
12
![Page 68: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/68.jpg)
問題是搬家是需要成本的…
P0
P1
P2
太多Process並行效能反而不如預期…
解法1:提供多套Registers
Registers
解法2:改用Thread~
Thread: Light weight process. 是CPU分配資源的最小單位,而同一個Process內的threads共享code section, data section, 跟一些OS資源
12
![Page 69: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/69.jpg)
建立Thread也是有成本的
→ 用空間換取時間~~
13
![Page 70: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/70.jpg)
建立Thread也是有成本的
→ 用空間換取時間~~
Thread Pool: 在Process建立之初,預先建立多條threads置於thread pool中,當需要使用時,就從thread pool中取出使用,用完再還給thread pool。
13
![Page 71: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/71.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
![Page 72: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/72.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
![Page 73: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/73.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
排程問題
![Page 74: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/74.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
排程問題
![Page 75: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/75.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
排程問題
Context Switching
![Page 76: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/76.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
排程問題
Context Switching
![Page 77: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/77.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
排程問題
Context Switching
建立Thread的成本
![Page 78: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/78.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
…
![Page 79: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/79.jpg)
到目前為止…
14
單一行程若進入IO
,CP
U
效能則無法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
??
…
![Page 80: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/80.jpg)
Multi-Processing / Multi-Threading (1)
15
• 系統的資源通常是有限的
– CPU週期、記憶體空間、檔案、I/O Device…
當Process需要資源時…
![Page 81: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/81.jpg)
Multi-Processing / Multi-Threading (1)
15
• 系統的資源通常是有限的
– CPU週期、記憶體空間、檔案、I/O Device…
當Process需要資源時…
Process提出Request
![Page 82: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/82.jpg)
Multi-Processing / Multi-Threading (1)
15
• 系統的資源通常是有限的
– CPU週期、記憶體空間、檔案、I/O Device…
當Process需要資源時…
Process提出Request
資源是否足夠
![Page 83: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/83.jpg)
Multi-Processing / Multi-Threading (1)
15
• 系統的資源通常是有限的
– CPU週期、記憶體空間、檔案、I/O Device…
當Process需要資源時…
Process提出Request
資源是否足夠
Yes
Process使用資源
使用完畢,釋放
![Page 84: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/84.jpg)
Multi-Processing / Multi-Threading (1)
15
• 系統的資源通常是有限的
– CPU週期、記憶體空間、檔案、I/O Device…
當Process需要資源時…
Process提出Request
資源是否足夠
Yes
Process使用資源
使用完畢,釋放
No Process等待…
![Page 85: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/85.jpg)
Multi-Processing / Multi-Threading (1)
15
• 系統的資源通常是有限的
– CPU週期、記憶體空間、檔案、I/O Device…
當Process需要資源時…
Process提出Request
資源是否足夠
Yes
Process使用資源
使用完畢,釋放
No Process等待…
![Page 86: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/86.jpg)
16
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
![Page 87: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/87.jpg)
16
Thread 1 R1 used
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
![Page 88: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/88.jpg)
16
Thread 1 R1 used
Thread 2 R2 used
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
![Page 89: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/89.jpg)
16
Thread 1 R1 used
Thread 2 R2 used
Wait
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
![Page 90: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/90.jpg)
16
Thread 1 R1 used
Thread 2 R2 used
Wait Wait
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
![Page 91: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/91.jpg)
Deadlock: 系統中存在一組Processes,彼此形成circular waiting的情況,使得Processes皆無法繼續往下執行,導致CPU利用度及產能急速下降。
16
Thread 1 R1 used
Thread 2 R2 used
Wait Wait
New Ready Running Terminate
Wait (Block)
Wait for I/O complete or resources available
![Page 92: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/92.jpg)
Deadlock 處理策略
17
T1 R1 used
T2 R2 used
Wait Wait
![Page 93: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/93.jpg)
Deadlock 處理策略
17
• Deadlock形成的必要條件:
• 資源的Mutual Exclusion
• Hold & Wait
• No Preemptive
• Circular waiting
T1 R1 used
T2 R2 used
Wait Wait
![Page 94: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/94.jpg)
Deadlock 處理策略
• 處理策略:
– Deadlock Prevention
– Deadlock Avoidance
– Deadlock Detection & Recovery
17
• Deadlock形成的必要條件:
• 資源的Mutual Exclusion
• Hold & Wait
• No Preemptive
• Circular waiting
T1 R1 used
T2 R2 used
Wait Wait
![Page 95: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/95.jpg)
Deadlock 處理策略
• 處理策略:
– Deadlock Prevention
– Deadlock Avoidance
– Deadlock Detection & Recovery
17
• Deadlock形成的必要條件:
• 資源的Mutual Exclusion
• Hold & Wait
• No Preemptive
• Circular waiting
T1 R1 used
T2 R2 used
Wait Wait
預防勝於治療
![Page 96: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/96.jpg)
Deadlock 處理策略
• 處理策略:
– Deadlock Prevention
– Deadlock Avoidance
– Deadlock Detection & Recovery
17
• Deadlock形成的必要條件:
• 資源的Mutual Exclusion
• Hold & Wait
• No Preemptive
• Circular waiting
T1 R1 used
T2 R2 used
Wait Wait
預防勝於治療
見到棺材才掉淚
![Page 97: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/97.jpg)
Multi-Processing / Multi-Threading (2)
• Communication
– Shared Memory
– Message Passing
18
Thread 1 data Thread 2
Memory / …
Process 1 Process 2 Send Msg
Process 1 Process 2
![Page 98: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/98.jpg)
Producer & Consumer
19
![Page 99: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/99.jpg)
Producer & Consumer
19
Bounded buffer Producer/Consumer Problem:
(1) 當Buffer滿時,則Producer必須等待。
(2) 當Buffer空時,則Consumer必須等待。
![Page 100: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/100.jpg)
Producer & Consumer
19
Bounded buffer Producer/Consumer Problem:
(1) 當Buffer滿時,則Producer必須等待。
(2) 當Buffer空時,則Consumer必須等待。
→ 採用一個共用變數count來記錄buffer裡item的個數
![Page 101: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/101.jpg)
Producer: 生產一個item;
while(count==size)
do no-op;
buffer[in] = item;
in = (in+1) % size;
count = count+1;
20
Consumer:
while(count==0)
do no-op;
item = buffer[out];
out = (out+1) mod size;
count = count-1;
![Page 102: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/102.jpg)
Producer: 生產一個item;
while(count==size)
do no-op;
buffer[in] = item;
in = (in+1) % size;
count = count+1;
20
Consumer:
while(count==0)
do no-op;
item = buffer[out];
out = (out+1) mod size;
count = count-1;
1. Producer跟 Consumer是concurrent的
2. 共享count變數
![Page 103: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/103.jpg)
Producer: 生產一個item;
while(count==size)
do no-op;
buffer[in] = item;
in = (in+1) % size;
count = count+1;
20
Consumer:
while(count==0)
do no-op;
item = buffer[out];
out = (out+1) mod size;
count = count-1;
Registers
1. Producer跟 Consumer是concurrent的
2. 共享count變數
![Page 104: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/104.jpg)
21
Registers
Producer (Thread1) count = count+1;
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(3)
Consumer (Thread2) count = count-1;
Register2 = count …(4) Register2 = Register2 – 1 …(5) count = Register2 …(6)
count
4 Register2
Register1
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Not Atomic!!
![Page 105: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/105.jpg)
21
Registers
Producer (Thread1) count = count+1;
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(3)
Consumer (Thread2) count = count-1;
Register2 = count …(4) Register2 = Register2 – 1 …(5) count = Register2 …(6)
count
4 Register2
4
Register1
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Not Atomic!!
![Page 106: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/106.jpg)
21
Registers
Producer (Thread1) count = count+1;
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(3)
Consumer (Thread2) count = count-1;
Register2 = count …(4) Register2 = Register2 – 1 …(5) count = Register2 …(6)
count
4 Register2
4
Register1
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Not Atomic!!
![Page 107: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/107.jpg)
21
Registers
Producer (Thread1) count = count+1;
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(3)
Consumer (Thread2) count = count-1;
Register2 = count …(4) Register2 = Register2 – 1 …(5) count = Register2 …(6)
count
4 Register2
4
Register1
5 5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Not Atomic!!
![Page 108: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/108.jpg)
21
Registers
Producer (Thread1) count = count+1;
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(3)
Consumer (Thread2) count = count-1;
Register2 = count …(4) Register2 = Register2 – 1 …(5) count = Register2 …(6)
count
4 Register2
4
Register1 5
5 5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Not Atomic!!
![Page 109: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/109.jpg)
21
Registers
Producer (Thread1) count = count+1;
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(3)
Consumer (Thread2) count = count-1;
Register2 = count …(4) Register2 = Register2 – 1 …(5) count = Register2 …(6)
count
4 Register2
4
Register1 5
5 5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
4
Not Atomic!!
![Page 110: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/110.jpg)
21
Registers
Producer (Thread1) count = count+1;
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(3)
Consumer (Thread2) count = count-1;
Register2 = count …(4) Register2 = Register2 – 1 …(5) count = Register2 …(6)
count
4 Register2
4
Register1 5
5 5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
4
4
Not Atomic!!
![Page 111: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/111.jpg)
21
Registers
Producer (Thread1) count = count+1;
Consumer (Thread2) count = count-1;
count
4 Register2
4
Register1 5
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(5)
Register2 = count …(3) Register2 = Register2 – 1 …(4) count = Register2 …(6)
4
Not Atomic!!
![Page 112: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/112.jpg)
21
Registers
Producer (Thread1) count = count+1;
Consumer (Thread2) count = count-1;
count
4 Register2
4
Register1 5
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(5)
Register2 = count …(3) Register2 = Register2 – 1 …(4) count = Register2 …(6)
4 4
Not Atomic!!
![Page 113: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/113.jpg)
21
Registers
Producer (Thread1) count = count+1;
Consumer (Thread2) count = count-1;
count
4 Register2
4
Register1 5
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(5)
Register2 = count …(3) Register2 = Register2 – 1 …(4) count = Register2 …(6)
4 4 5
Not Atomic!!
![Page 114: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/114.jpg)
21
Registers
Producer (Thread1) count = count+1;
Consumer (Thread2) count = count-1;
count
4 Register2
4
Register1 5
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(5)
Register2 = count …(3) Register2 = Register2 – 1 …(4) count = Register2 …(6)
4 4 5
4
Not Atomic!!
![Page 115: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/115.jpg)
21
Registers
Producer (Thread1) count = count+1;
Consumer (Thread2) count = count-1;
count
4 Register2
4
Register1 5
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(5)
Register2 = count …(3) Register2 = Register2 – 1 …(4) count = Register2 …(6)
4 4 5
4 3
Not Atomic!!
![Page 116: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/116.jpg)
21
Registers
Producer (Thread1) count = count+1;
Consumer (Thread2) count = count-1;
count
4 Register2
4
Register1 5
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(5)
Register2 = count …(3) Register2 = Register2 – 1 …(4) count = Register2 …(6)
4 4 5
4 3 5
Not Atomic!!
![Page 117: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/117.jpg)
21
Registers
Producer (Thread1) count = count+1;
Consumer (Thread2) count = count-1;
count
4 Register2
4
Register1 5
5
1. Producer跟 Consumer是concurrent的
2. 共享count變數 3. CPU可被搶奪的 (e.g. SRJF)
Register1 = count …(1) Register1 = Register1 + 1 …(2) count = Register1 …(5)
Register2 = count …(3) Register2 = Register2 – 1 …(4) count = Register2 …(6)
4 4 5
4 3 5 3
Not Atomic!!
![Page 118: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/118.jpg)
Race Condition
• 在Share Memory溝通方式下,共享變數的值會因為Processes執行的順序不同而有所不同。
22
Producer
count = count+1;
Consumer:
count = count-1;
![Page 119: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/119.jpg)
Race Condition
• 在Share Memory溝通方式下,共享變數的值會因為Processes執行的順序不同而有所不同。
22
Producer
count = count+1;
Consumer:
count = count-1;
保證同一時間只有一個Process在存取共享變數
![Page 120: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/120.jpg)
Critical Section
23
Repeat
…
C.S.
…
Until false
Entry section
Exit section
![Page 121: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/121.jpg)
Critical Section
23
• Mutual Exclusion: 在任何時間點最多允許一個process在其C.S.內活動
Repeat
…
C.S.
…
Until false
Entry section
Exit section
![Page 122: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/122.jpg)
Critical Section
23
• Mutual Exclusion: 在任何時間點最多允許一個process在其C.S.內活動
• Progress – 不想進入C.S.的Process不能阻礙其他process進入
– 在有限的時間內必須從想進入C.S.的processes中決定出一個Process進入C.S.
Repeat
…
C.S.
…
Until false
Entry section
Exit section
![Page 123: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/123.jpg)
Critical Section
23
• Mutual Exclusion: 在任何時間點最多允許一個process在其C.S.內活動
• Progress – 不想進入C.S.的Process不能阻礙其他process進入
– 在有限的時間內必須從想進入C.S.的processes中決定出一個Process進入C.S.
• Bounded Waiting: Process從提出申請要進入C.S.到其獲准進入C.S.的這段時間是有限的
Repeat
…
C.S.
…
Until false
Entry section
Exit section
![Page 124: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/124.jpg)
Critical Section
23
• Mutual Exclusion: 在任何時間點最多允許一個process在其C.S.內活動
• Progress – 不想進入C.S.的Process不能阻礙其他process進入
– 在有限的時間內必須從想進入C.S.的processes中決定出一個Process進入C.S.
• Bounded Waiting: Process從提出申請要進入C.S.到其獲准進入C.S.的這段時間是有限的
No Deadlock
Repeat
…
C.S.
…
Until false
Entry section
Exit section
![Page 125: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/125.jpg)
Critical Section
23
• Mutual Exclusion: 在任何時間點最多允許一個process在其C.S.內活動
• Progress – 不想進入C.S.的Process不能阻礙其他process進入
– 在有限的時間內必須從想進入C.S.的processes中決定出一個Process進入C.S.
• Bounded Waiting: Process從提出申請要進入C.S.到其獲准進入C.S.的這段時間是有限的
No Deadlock
No Starvation
Repeat
…
C.S.
…
Until false
Entry section
Exit section
![Page 126: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/126.jpg)
C.S. Design – design entry/exit section
• Software Solution
• Hardware Solution
• Semaphore
• Monitor
• Critical Region
• …
24
Repeat
…
C.S.
…
Until false
Entry section
Exit section
![Page 127: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/127.jpg)
Multi-Processing / Multi-Threading (3)
• 同步問題
25
P1 P2
… …
A B
… …
如果我們希望A敘述一定要在B敘述之前執行,怎麼做?
![Page 128: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/128.jpg)
Multi-Processing / Multi-Threading (3)
• 同步問題
25
P1 P2
… …
A B
… …
如果我們希望A敘述一定要在B敘述之前執行,怎麼做?
Wait(s){
while(s<=0) do no-op;
s = s – 1;
}
Signal(s){
s++;
}
![Page 129: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/129.jpg)
Multi-Processing / Multi-Threading (3)
• 同步問題
25
P1 P2
… …
A B
… …
如果我們希望A敘述一定要在B敘述之前執行,怎麼做?
Wait(s){
while(s<=0) do no-op;
s = s – 1;
}
Signal(s){
s++;
}
P1 P2
… …
A
B
… …
Signal(s)
wait(s)
![Page 130: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/130.jpg)
Multi-Processing / Multi-Threading (3)
• 同步問題
25
P1 P2
… …
A B
… …
如果我們希望A敘述一定要在B敘述之前執行,怎麼做?
Wait(s){
while(s<=0) do no-op;
s = s – 1;
}
Signal(s){
s++;
}
P1 P2
… …
A
B
… …
Signal(s)
wait(s)
![Page 131: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/131.jpg)
Synchronization
• 著名的同步問題 – Dining philosophers problem
– Producer-consumer problem
– Readers-writers problem
– Sleeping barber problem
• 解法: – Semaphore
– Monitor
– Critical Region
26
![Page 132: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/132.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
![Page 133: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/133.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
![Page 134: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/134.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
Deadlock
![Page 135: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/135.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
Deadlock
Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery
![Page 136: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/136.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
共享記憶體 訊息傳遞
Deadlock
Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery
…
![Page 137: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/137.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
共享記憶體 訊息傳遞
Deadlock
Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery
…
Race Condition
![Page 138: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/138.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
共享記憶體 訊息傳遞
Deadlock
Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery
…
Race Condition
同步問題
![Page 139: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/139.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
共享記憶體 訊息傳遞
Deadlock
Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery
…
Race Condition
Critical Sectio
n
同步問題
![Page 140: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/140.jpg)
27
單一行程若進入IO
,CP
U
效能則無
法發揮
多行程並行,提升了C
PU
效能
OS
運用排程演算法協助排程
改用輕量級行程 th
reads
Thread
Poo
l
排程問題
Context Switching
建立Thread的成本
溝通
共享記憶體 訊息傳遞
Deadlock
Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery
…
Race Condition
Critical Sectio
n
Software Sol. Hardware Sol. Semaphore Monitor Critical Region …
同步問題
![Page 141: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/141.jpg)
補充
28
![Page 142: 作業系統基本觀念複習](https://reader036.vdocuments.mx/reader036/viewer/2022081802/5560fb22d8b42a91388b4e8f/html5/thumbnails/142.jpg)
補充
28