1.Processes, PIDs, and threads
A process is a running program with its own memory space and a unique process ID (PID) the kernel assigns it. A thread runs inside a process and shares that process's memory with any other threads in it.
Key features
- Every process gets a unique PID (process ID) from the kernel when it starts
- A process has its own memory space — one process cannot read another's variables directly
- A thread runs inside a process and shares that process's memory with its sibling threads
- os.getpid() returns the current process's PID; os.getppid() returns its parent's PID
- subprocess.run(...) starts a new, separate process — python.concurrency-and-parallelism.threading-and-thread starts a new thread in this one
Process vs. thread, and how Python starts each
- Process (PID 4821) — its own memory space
- Thread 1 — reads/writes the process's memory
- Thread 2 — reads/writes the SAME memory
Command
import os
os.getpid() # this process's own PID
os.getppid() # the PID of the process that started it (its parent)A process has its own memory and a unique PID; a thread runs inside a process and shares that process's memory — subprocess.run() gets you the former, threading.Thread() the latter.