High-level languages and the operating system
What a runtime asks of the OS, CPython: bytecode, refcount/GC, the GIL, os/subprocess and the idea of a JIT.
The runtime’s role
A C program is almost directly against the kernel: malloc becomes mmap/brk, pthread_create becomes clone, printf ends in write. Python, Java and JavaScript promise automatic memory, portability and rich libraries — those promises are a runtime: a substantial C (or C++) program that implements the language with the same system calls the C half of the course used.
The runtime’s job is not magic. It asks the kernel for anonymous pages with mmap, creates threads, multiplexes I/O with poll/epoll or a library event loop, installs handlers with sigaction, and starts subprocesses with fork/exec or posix_spawn. Everything above — objects, GC, green threads, async — is user-space policy on that substrate. Understanding a language at systems level means seeing which syscalls it wraps and where it pays.
CPython: execution
CPython is the reference Python implementation. Source becomes bytecode for a stack machine; a C interpreter loop fetches opcodes and executes them. dis.dis shows the bytecode; .pyc files cache it. Interpretation alone costs about an order of magnitude or more against compiled C on tight loops — each bytecode does work a C compiler would have turned into a handful of machine instructions.
Memory has three layers. Every object carries a reference count; when it hits zero the object is freed immediately. Cycles (A→B→A) never reach zero, so a generational cycle collector finds them. Small objects (up to 512 bytes) come from pymalloc, an allocator on top of OS pages, so most allocations never enter the kernel. There is no manual free: dropping the last reference is the free.
Strings, lists and dicts are heap objects with headers; slicing and concatenation allocate new ones. The cost of for i in range(n): s += 1 is dominated by interpreter dispatch and object traffic, not by the add.
GIL, syscalls, JIT
The GIL (global interpreter lock) lets only one thread run bytecode at a time. It exists because reference counts and much interpreter state are shared and unsynchronized; taking a lock around every INCREF/DECREF would be worse. The GIL is released around blocking system calls (read, connect, …), so threads help I/O-bound Python. For CPU-bound work, use processes (multiprocessing) so each has its own interpreter and GIL.
The os and subprocess modules are thin wrappers: os.open → open(2), os.fork → fork, subprocess → posix_spawn or fork+exec. Signal handlers registered in Python run later, between bytecode instructions, in the main thread; interrupted syscalls are typically restarted.
Other runtimes JIT: they observe hot types and write machine code into pages that must be writable then executable (mmap with PROT_WRITE, then mprotect to PROT_EXEC, or MAP_JIT on some OS). PyPy, HotSpot and V8 all do this. Ahead-of-time compilers (this course’s LLVM path) skip that and emit a native binary up front. Next — Compilers and LLVM: how that binary is made from C.
Concrete picture. print("hi") in CPython formats, then eventually calls the C function that issues write(1, …). open("/tmp/x") becomes open with flags; a socket in the socket module is the same Berkeley API the sockets topic used. An asyncio event loop is an epoll/kqueue wrapper with futures on top — C10K ideas, not a separate kernel.
Reference counting is visible in sys.getrefcount. Assigning b = a bumps the count; del a drops it. A cycle a.x = b; b.x = a needs the cycle collector; without it those objects would leak forever. pymalloc’s 512-byte cutoff means a million small tuples stress the arena allocator, not mmap per object.
When writing systems-facing Python, prefer explicit processes for CPU work, threads or asyncio for I/O, and remember that a C extension can release the GIL and run native code in parallel — that is how NumPy saturates cores while bytecode stays serialized.
Runtimes also own failure modes the kernel does not see. An unhandled exception unwinds Python frames and prints a traceback without killing other threads until the process exits. Out-of-memory in the allocator raises MemoryError rather than always getting SIGKILL from the OOM killer — though the OOM killer can still strike if the process touches too many anonymous pages. Measuring with strace -c on a short script shows the syscall mix: many read/write/mmap/futex calls behind a few lines of Python.
Garbage-collected runtimes (HotSpot, V8) replace refcounts with tracing: they walk roots from stacks and globals, mark reachable objects, and reclaim the rest in generations. That removes the GIL’s original motivation but introduces pause times and write barriers — different systems tradeoffs, same kernel underneath (mmap, threads, signals). Whether refcount or tracing, the process still looks to the OS like any other: a VAS, fds and threads.
On a microbenchmark summing to ten million, CPython may take tens of milliseconds where -O2 C takes about one; the gap is interpreter dispatch, heap traffic and the GIL — not the arithmetic. That gap is why extension modules and JITs exist, and why the next topic’s ahead-of-time LLVM path still matters for systems code.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.