第4章 抽象:进程 homework详解
写在前面
开个新坑,csapp读到汇编看不下去了,故转战操作系统。
题目在书中有提到,这里不再重复赘述(懒)。
实验材料从官网下载就好,贴个传送门。
建议在实验开始之前仔细阅读 readme 文件。
实验环境
笔者采用的是Ubuntu22.04的虚拟机。
本次作业用到 cpu-intro 文件
实验内容
1
| $ ./process-run.py -l 5:100,5:100 -c -p Time PID: 0 PID: 1 CPU IOs 1 RUN:cpu READY 1 2 RUN:cpu READY 1 3 RUN:cpu READY 1 4 RUN:cpu READY 1 5 RUN:cpu READY 1 6 DONE RUN:cpu 1 7 DONE RUN:cpu 1 8 DONE RUN:cpu 1 9 DONE RUN:cpu 1 10 DONE RUN:cpu 1
Stats: Total Time 10 Stats: CPU Busy 10 (100.00%) Stats: IO Busy 0 (0.00%)
|
运行完第一个进程紧接着运行第二个进程,cpu利用率100%。
2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ ./process-run.py -l 4:100,1:0 -c -p Time PID: 0 PID: 1 CPU IOs 1 RUN:cpu READY 1 2 RUN:cpu READY 1 3 RUN:cpu READY 1 4 RUN:cpu READY 1 5 DONE RUN:io 1 6 DONE WAITING 1 7 DONE WAITING 1 8 DONE WAITING 1 9 DONE WAITING 1 10 DONE WAITING 1 11* DONE RUN:io_done 1
Stats: Total Time 11 Stats: CPU Busy 6 (54.55%) Stats: IO Busy 5 (45.45%)
|
这里先运行第一个进程时会阻塞第二个进程运行,所以要依此进行。
3
1 2 3 4 5 6 7 8 9 10 11 12 13
| $ ./process-run.py -l 1:0,4:100 -c -p Time PID: 0 PID: 1 CPU IOs 1 RUN:io READY 1 2 WAITING RUN:cpu 1 1 3 WAITING RUN:cpu 1 1 4 WAITING RUN:cpu 1 1 5 WAITING RUN:cpu 1 1 6 WAITING DONE 1 7* RUN:io_done DONE 1
Stats: Total Time 7 Stats: CPU Busy 6 (85.71%) Stats: IO Busy 5 (71.43%)
|
容易发现交换顺序是会对运行时间和效率产生影响的,运行io时可以同时运行cpu。
4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ ./process-run.py -l 1:0,4:100 -c -S SWITCH_ON_END -p Time PID: 0 PID: 1 CPU IOs 1 RUN:io READY 1 2 WAITING READY 1 3 WAITING READY 1 4 WAITING READY 1 5 WAITING READY 1 6 WAITING READY 1 7* RUN:io_done READY 1 8 DONE RUN:cpu 1 9 DONE RUN:cpu 1 10 DONE RUN:cpu 1 11 DONE RUN:cpu 1
Stats: Total Time 11 Stats: CPU Busy 6 (54.55%) Stats: IO Busy 5 (45.45%)
|
如果限制了当进行io操作时,系统不允许切换到另一个进程,则产生的效果和不交换顺序时相同。
5
1 2 3 4 5 6 7 8 9 10 11 12 13
| $ ./process-run.py -l 1:0,4:100 -c -S SWITCH_ON_IO -p Time PID: 0 PID: 1 CPU IOs 1 RUN:io READY 1 2 WAITING RUN:cpu 1 1 3 WAITING RUN:cpu 1 1 4 WAITING RUN:cpu 1 1 5 WAITING RUN:cpu 1 1 6 WAITING DONE 1 7* RUN:io_done DONE 1
Stats: Total Time 7 Stats: CPU Busy 6 (85.71%) Stats: IO Busy 5 (71.43%)
|
和3一致,不做解释。
6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| $ ./process-run.py -l 3:0,5:100,5:100 -S SWITCH_ON_IO -I IO_RUN_LATER -c -p Time PID: 0 PID: 1 PID: 2 CPU IOs 1 RUN:io READY READY 1 2 WAITING RUN:cpu READY 1 1 3 WAITING RUN:cpu READY 1 1 4 WAITING RUN:cpu READY 1 1 5 WAITING RUN:cpu READY 1 1 6 WAITING RUN:cpu READY 1 1 7* READY DONE RUN:cpu 1 8 READY DONE RUN:cpu 1 9 READY DONE RUN:cpu 1 10 READY DONE RUN:cpu 1 11 READY DONE RUN:cpu 1 12 RUN:io_done DONE DONE 1 13 RUN:io DONE DONE 1 14 WAITING DONE DONE 1 15 WAITING DONE DONE 1 16 WAITING DONE DONE 1 17 WAITING DONE DONE 1 18 WAITING DONE DONE 1 19* RUN:io_done DONE DONE 1 20 RUN:io DONE DONE 1 21 WAITING DONE DONE 1 22 WAITING DONE DONE 1 23 WAITING DONE DONE 1 24 WAITING DONE DONE 1 25 WAITING DONE DONE 1 26* RUN:io_done DONE DONE 1
Stats: Total Time 26 Stats: CPU Busy 16 (61.54%) Stats: IO Busy 15 (57.69%)
|
在运行完第一个io操作后没有及时继续运行该进程,导致该进程后续进行io操作时浪费了时间。
7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| $ ./process-run.py -l 3:0,5:100,5:100 -S SWITCH_ON_IO -I IO_RUN_IMMEDIATE -c -p Time PID: 0 PID: 1 PID: 2 CPU IOs 1 RUN:io READY READY 1 2 WAITING RUN:cpu READY 1 1 3 WAITING RUN:cpu READY 1 1 4 WAITING RUN:cpu READY 1 1 5 WAITING RUN:cpu READY 1 1 6 WAITING RUN:cpu READY 1 1 7* RUN:io_done DONE READY 1 8 RUN:io DONE READY 1 9 WAITING DONE RUN:cpu 1 1 10 WAITING DONE RUN:cpu 1 1 11 WAITING DONE RUN:cpu 1 1 12 WAITING DONE RUN:cpu 1 1 13 WAITING DONE RUN:cpu 1 1 14* RUN:io_done DONE DONE 1 15 RUN:io DONE DONE 1 16 WAITING DONE DONE 1 17 WAITING DONE DONE 1 18 WAITING DONE DONE 1 19 WAITING DONE DONE 1 20 WAITING DONE DONE 1 21* RUN:io_done DONE DONE 1
Stats: Total Time 21 Stats: CPU Busy 16 (76.19%) Stats: IO Busy 15 (71.43%)
|
本题在6的基础上做了改善,立即运行进程,对运行时间做了优化。
8
第8题是一道开放性问题,为了让读者能够理解进程运行的奥秘,请读者自行实践。