OSTEP: Abstract: Process
This series mainly completes the after-school homework of Introduction to Operating Systems (Operating Systems: Three Easy Pieces ), and also involves some summaries and insights of each chapter. Most of the questions do not have a standard answer. Students are welcome to find disputes and errors. discuss together.
related resources
Website:http://www.ostep.org/
Homework:https://github.com/remzi-arpacidusseau/ostep-homework/
Column: Introduction to Operating Systems (ostep)
Answers to exercises
Note: The exercises in this chapter require Python to be installed to continue.
First use the -H command to view some options in process-run.py:
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 process-run.py -h
Usage: process-run.py [options]
Options:
-h, --help show this help message and exit
-s SEED, --seed=SEED the random seed
-P PROGRAM, --program=PROGRAM
more specific controls over programs
-l PROCESS_LIST, --processlist=PROCESS_LIST
a comma-separated list of processes to run, in the
form X1:Y1,X2:Y2,... where X is the number of
instructions that process should run, and Y the
chances (from 0 to 100) that an instruction will use
the CPU or issue an IO
-L IO_LENGTH, --iolength=IO_LENGTH
how long an IO takes
-S PROCESS_SWITCH_BEHAVIOR, --switch=PROCESS_SWITCH_BEHAVIOR
when to switch between processes: SWITCH_ON_IO,
SWITCH_ON_END
-I IO_DONE_BEHAVIOR, --iodone=IO_DONE_BEHAVIOR
type of behavior when IO ends: IO_RUN_LATER,
IO_RUN_IMMEDIATE
-c compute answers for me
-p, --printstats print statistics at end; only useful with -c flag
(otherwise stats are not printed)
1. It should be 100%, because there are no IO operations and all CPU usage.
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 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%)
2.4(process 0) + 6(process 1 IO) + 1 = 11
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 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. It only took 7S, because when process 1 interacted with IO, the control of the CPU was released, allowing process 0 to run directly.
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 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%)
4. Return to 11S again, because the CPU and IO cannot work at the same time.
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 process-run.py -l 1:0,4:100 -c -p -S SWITCH_ON_END
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%)
5. Return to 7S, the same as question 3.
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 process-run.py -l 1:0,4:100 -c -p -S SWITCH_ON_IO
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%)
6. It cannot be used effectively, because after process 0 finishes running the first IO, it will not run immediately, but will wait for other processes to finish running before continuing to run IO, resulting in that IO and CPU are not used at the same time for a part of the time. Time wasted.
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 process-run.py -l 3:0,5:100,5:100,5:100 -c -p -S SWITCH_ON_IO -I IO_RUN_LATER
Time PID: 0 PID: 1 PID: 2 PID: 3 CPU IOs
1 RUN:io READY READY READY 1
2 WAITING RUN:cpu READY READY 1 1
3 WAITING RUN:cpu READY READY 1 1
4 WAITING RUN:cpu READY READY 1 1
5 WAITING RUN:cpu READY READY 1 1
6 WAITING RUN:cpu READY READY 1 1
7* READY DONE RUN:cpu READY 1
8 READY DONE RUN:cpu READY 1
9 READY DONE RUN:cpu READY 1
10 READY DONE RUN:cpu READY 1
11 READY DONE RUN:cpu READY 1
12 READY DONE DONE RUN:cpu 1
13 READY DONE DONE RUN:cpu 1
14 READY DONE DONE RUN:cpu 1
15 READY DONE DONE RUN:cpu 1
16 READY DONE DONE RUN:cpu 1
17 RUN:io_done DONE DONE DONE 1
18 RUN:io DONE DONE DONE 1
19 WAITING DONE DONE DONE 1
20 WAITING DONE DONE DONE 1
21 WAITING DONE DONE DONE 1
22 WAITING DONE DONE DONE 1
23 WAITING DONE DONE DONE 1
24* RUN:io_done DONE DONE DONE 1
25 RUN:io DONE DONE DONE 1
26 WAITING DONE DONE DONE 1
27 WAITING DONE DONE DONE 1
28 WAITING DONE DONE DONE 1
29 WAITING DONE DONE DONE 1
30 WAITING DONE DONE DONE 1
31* RUN:io_done DONE DONE DONE 1
Stats: Total Time 31
Stats: CPU Busy 21 (67.74%)
Stats: IO Busy 15 (48.39%)
7. Process 0 runs directly after IO ends, and then continues to run the next round of IO. At this time, other processes can use the CPU, and will try to ensure that both IO and CPU can be used at the same time.
yzy@yzy-virtual-machine:~/ostep-homework/cpu-intro$ python3 process-run.py -l 3:0,5:100,5:100,5:100 -c -p -S SWITCH_ON_IO -I IO_RUN_IMMEDIATE
Time PID: 0 PID: 1 PID: 2 PID: 3 CPU IOs
1 RUN:io READY READY READY 1
2 WAITING RUN:cpu READY READY 1 1
3 WAITING RUN:cpu READY READY 1 1
4 WAITING RUN:cpu READY READY 1 1
5 WAITING RUN:cpu READY READY 1 1
6 WAITING RUN:cpu READY READY 1 1
7* RUN:io_done DONE READY READY 1
8 RUN:io DONE READY READY 1
9 WAITING DONE RUN:cpu READY 1 1
10 WAITING DONE RUN:cpu READY 1 1
11 WAITING DONE RUN:cpu READY 1 1
12 WAITING DONE RUN:cpu READY 1 1
13 WAITING DONE RUN:cpu READY 1 1
14* RUN:io_done DONE DONE READY 1
15 RUN:io DONE DONE READY 1
16 WAITING DONE DONE RUN:cpu 1 1
17 WAITING DONE DONE RUN:cpu 1 1
18 WAITING DONE DONE RUN:cpu 1 1
19 WAITING DONE DONE RUN:cpu 1 1
20 WAITING DONE DONE RUN:cpu 1 1
21* RUN:io_done DONE DONE DONE 1
Stats: Total Time 21
Stats: CPU Busy 21 (100.00%)
Stats: IO Busy 15 (71.43%)
8. Random testing
Summary and insights
- A process is a running program. It has some machine states, the core of which are memory and registers (program counter, stack pointer, etc.).
- When you run a program, the operating system first loads the code and static data into the process' memory . Then allocate some memory to the runtime stack of the process (used to save some local variables, function parameters, return addresses, etc.). Allocate some memory to the process's heap (showing requested dynamically allocated data, global variables, etc.). Perform some other initialization tasks , such as initializing IO, etc.
- The process status is divided into running, ready, and blocked.