Every process gets a number
The OS gives each process a PID (process ID), a unique positive number, when it starts. PIDs are how you refer to a process when talking to the OS: "pause 4021", "how much memory is 4021 using", "kill 4021".
Processes also form a family tree. Whatever starts a process becomes its parent. Your terminal (a shell process) is the parent of the python3 process it launches. Python can ask the OS for both numbers:
os.getpid()returns my PIDos.getppid()returns my parent's PID
Code exercise · python
Run this. The actual PID changes every run (the OS hands out fresh numbers), so we print facts about it that are always true.
Code exercise · python
Explore: print the raw PID and parent PID. There is no expected output here because the numbers differ on every run and every machine. Run it a few times and watch the PID change.
Seeing processes from your own terminal
On your machine (macOS or Linux), these commands read the real process table:
ps aux # snapshot of every process: PID, owner, CPU%, memory top # live, updating view (press q to quit) ps aux | grep python # only rows mentioning python kill 4021 # politely ask process 4021 to quit kill -9 4021 # force it (the OS just erases it, no cleanup)
Try ps aux now if you have a terminal handy. The PID column is the same number os.getpid() reports from inside. On Windows, Task Manager shows the same table with a GUI.
Quiz
You run ps aux and see two rows for python3 with PIDs 5001 and 5002. What can you conclude?
Problem
In the process family tree, what do we call the process that started another process? (one word)