One command, every concept
You type python3 hello.py and press Enter. Here is the whole story, and you have already learned every step:
- The shell reads your keystrokes. It is just a user-space process, blocked on a
readsyscall from the keyboard (lessons 6-2 and 8-1). - The shell asks the kernel for a child process and has it load the
python3program from disk into RAM (lessons 3-3 and 1-1). The kernel builds a process: fresh PID, a brand-new page table mapping its private virtual address space, empty stack and heap, entry in the process table (lessons 3-1, 3-2, 4-1, 4-4). - The scheduler starts giving it time slices (lesson 6-1). The CPU begins the fetch-decode-execute cycle on Python's instructions (lesson 1-2).
- Python reads hello.py with open/read syscalls, byte by byte, decoding UTF-8 into text (lessons 7-1, 2-3), and starts interpreting it.
...and the ending
- hello.py calls print("Hello!"): the text goes to a buffer, the newline flushes it, the
writesyscall crosses into the kernel, the bytes land in file descriptor 1, the terminal draws glyphs (lessons 7-2, 8-2). - The script ends. Python exits with code 0. (Had it hung, Ctrl-C would have delivered SIGINT, lesson 3-4.) The kernel frees the process's memory and page table, closes its descriptors, removes the process-table entry, and hands the exit code to the parent, the shell (lessons 3-3, 3-4, 4-3, 4-4).
- The shell wakes up (it was blocked waiting for its child), sees exit code 0, and prints the next prompt.
Elapsed time: maybe 30 milliseconds, containing thousands of context switches system-wide, and every single mechanism in this course.
Quiz
Put these in the order they happen when you run python3 hello.py: (a) write syscall sends Hello! to the terminal, (b) the kernel creates a new process and loads python3, (c) the shell receives exit code 0, (d) the scheduler gives the new process its first time slice.
Quiz
While python3 waits for hello.py's bytes to arrive from the disk, what is its state, and who decided that?
Problem
In step 6, the kernel hands the number 0 from the dying python3 process to the waiting shell. What is that number called? (two words)