Workspace

Processes and system calls

3 day streakD

Processes and system calls

A process is an address space, a set of open files and a thread or more of execution. The kernel boundary is where every outside effect happens.

BasicsLesson 1 of 714 min

When you run a program, the kernel creates a process: a private virtual address space holding code, data, heap and stack; a table of file descriptors; credentials such as user ID; and one or more threads that execute instructions. Threads in one process share memory and descriptors. Separate processes share nothing unless they set it up explicitly.

Figure 1.1

What a process owns

What a process ownsA process has a virtual address space with code, data, heap, mapped libraries and stack, and a descriptor table where 0 is stdin to a terminal, 1 and 2 are stdout and stderr to a pipe, 3 is a socket and 4 is a file. Each descriptor refers to a kernel object across the system call boundary.Process · pid 4101thread 1thread 2Virtual address spaceStack ↓Shared libs · mmapHeap ↑DataCodehighlowFile descriptors0stdin1stdout2stderr3socket4filekernel boundary · system callsKernel objectsTerminal/dev/pts/3Pipe64 KiB bufferTCP socketapi.example:443Filepage cacheread() · write() · openat() · mmap()
The address space is private memory. File descriptors are small integers that index into kernel objects: terminals, pipes, files and sockets. Every arrow that crosses the dashed kernel boundary is a system call.

File descriptors: everything is a number

Descriptors 0, 1 and 2 are stdin, stdout and stderr by convention. open, socket and pipe hand out the lowest free number. A descriptor is a reference to a kernel object, and several descriptors in several processes can refer to the same object. That sharing is how pipes connect programs, and, as you will see, how pipes fail to close.

System calls

User code cannot touch hardware. To read a file, it calls read(fd, buf, n), which switches the CPU into kernel mode, does the work, and returns. A system call costs from about a hundred nanoseconds to microseconds, which is why buffered I/O exists: one write of 64 KB beats 65,536 writes of one byte.

watch-syscalls.shShell
# Every system call 'cat' makes, with arguments and results
strace -f -e trace=openat,read,write,close cat /etc/hostname

# On macOS, the closest equivalent needs SIP adjustments:
# sudo dtruss -f cat /etc/hostname

# List a running process's open descriptors
ls -l /proc/$PID/fd        # Linux
lsof -p $PID               # Linux and macOS

strace -f follows children too, which is what you want when debugging a shell tool.

Where you will meet it

Coding agents

Claude Code, Codex and similar tools run shell commands as child processes and stream their stdout and stderr back to the model.

Descriptor leaks

A server that opens files or sockets without closing them hits EMFILE: too many open files at the ulimit -n limit, often 1,024 by default.

Debugging hangs

strace -p PID on a stuck process shows the exact system call it is blocked in, usually read, write, wait4 or futex.

Check yourself

Answer out loud before you open each one.

What do threads in one process share that separate processes do not?

The address space and the file descriptor table.

Why is writing one byte at a time slow?

Each write is a system call with fixed overhead. Buffering amortises it over many bytes.

What is file descriptor 2?

stderr, by convention.

Try it

Run strace -f -c node -e 'require("fs").readFileSync("/etc/hosts")' on Linux. The -c summary counts every system call Node makes just to start and read one file.

A good place to start

Explore a pattern

A little guidance

Start with a pattern, read through a problem, and mark it solved when the idea clicks. Save anything you want to revisit.

New tutorials are added one at a time. Your bookmarks and solved checkmarks stay in this browser.

Explore the roadmap