Log inStart free
LESSON 1.12 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Processes, signals and environment variables

Watch a process copy itself, swap its program, and get a signal.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 1.12 · PROCESS BAY1 PROCESS · 1 THREAD · MODE IN ENV
01
Stage 1 of 6 · Set MODE
demo.py runs as a process with PID 5718. os.environ adds MODE=fast to its env.
Explain it simply

The shell started demo.py as process 5718, with its own memory. Next to the memory sits the env: a short list of settings, like notes that say NAME=value. os.environ adds MODE=fast to that list, so programs started from here can read it.

The shell, PID 5690, started demo.py as process 5718 with one thread. Its env holds HOME, PATH and MODE=fast.

process and threadmemory and envPID, parent link, handlersignal, warning, endoutput and exit code
program · demo.pyprocessesimport os, signal, threading, timedef on_term(signum, frame): …def work(): …signal.signal(signal.SIGTERM, on_term)1os.environ["MODE"] = "fast"2threading.Thread(target=work).start()3pid = os.fork()if pid == 0:4 os.execlp("python3", "python3", "child.py")time.sleep(1)5os.kill(pid, signal.SIGTERM)6status = os.waitpid(pid, 0)[1]code = os.waitstatus_to_exitcode(status)print("exit code:", code)bash · PID 5690waitspython3 demo.pyrunningPID 5718 · PPID 5690threadsmemoryenvsignalsmain 5718on_term()HOMEPATHMODE=fastSIGTERM→ on_termSIGKILL→ end (fixed)$ python3 demo.pyprogram · demo.pyprocessesimport os, signal, threading, timedef on_term(signum, frame): …def work(): …signal.signal(signal.SIGTERM, on_term)1os.environ["MODE"] = "fast"2threading.Thread(target=work).start()3pid = os.fork()if pid == 0:4 os.execlp("python3", "python3", "child.py")time.sleep(1)5os.kill(pid, signal.SIGTERM)6status = os.waitpid(pid, 0)[1]code = os.waitstatus_to_exitcode(status)print("exit code:", code)bash · PID 5690waitspython3 demo.pyrunningPID 5718 · PPID 5690threadsmemoryenvsignalsmain 5718on_term()HOMEPATHMODE=fastSIGTERM→ on_termSIGKILL→ end (fixed)$ python3 demo.py
Toy: 3 processes · Linux rules · PIDs are stand-ins · only the names used here are drawn
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
P=(PID, memory, env, threads)P = (\sy{pid}{param}{\mathrm{PID}},\ \sy{mem}{data}{\text{memory}},\ \sy{env}{data}{\text{env}},\ \sy{thr}{ink}{\text{threads}})
P=(5718, {on_term()}, {HOME, PATH, MODE=fast}, {5718})P = (5718,\ \{\texttt{on\_term()}\},\ \{\texttt{HOME},\ \texttt{PATH},\ \texttt{MODE=fast}\},\ \{5718\})

A process has a PID, its own memory, an env and threads.

Common mix-ups

MYTHexec starts a new process.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01If MODE is a plain variable, what does child.py print?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.