Processes, PIDs, and threads
corebeginnerA 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.
Think of it as
A process is a separate apartment — its own walls, its own furniture, nothing leaks into the apartment next door unless you deliberately open a door (a pipe, a socket, shared memory). A thread is a roommate inside that same apartment — free movement through every room, but also free to knock over the same furniture another roommate is using at the same moment.
What we're doing: Read the current process's PID and parent PID, then start a genuinely separate child process with subprocess and confirm it has a different PID.
- 4
- os.getpid() returns the PID the kernel assigned to THIS running Python process.
- 5
- os.getppid() returns the PID of whatever process started this one — a shell, or another Python process.
- 8
- subprocess.run([...]) starts a genuinely new process with its own PID and its own memory — not a thread.
this process: PID 8676, parent PID 10520
child process PID: 22040
different PID: TrueWhy this works: The kernel hands out a fresh, unique PID every time a new process starts — subprocess.run() asks the kernel to create one, so the child's PID is guaranteed different from the parent's. Contrast this with threading.Thread(...): a thread started that way shares the SAME PID as the process that created it, because it is not a separate process at all.
Assuming a thread has its own PID like a process does
Wrong
Better
What you see: os.getpid() prints the identical number in every thread of the same process — logs that key on PID alone cannot tell which thread produced a given line.
Why: A PID identifies a process, not a thread — every thread inside one process shares that process's single PID because they share its whole memory space, PID included. To distinguish threads in logs or debugging, use threading.get_ident() or threading.current_thread().name instead of os.getpid().
- Process (PID 4821) — its own memory space
- Thread 1 — reads/writes the process's memory
- Thread 2 — reads/writes the SAME memory
Process vs. thread, and how Python starts each
Together
Remember: 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.
See also: threading and thread · multiprocessing basics · what the gil is · os sys subprocess · signals · process inspection toolset

