← Interview Prep

Linux Kernel — Program Execution

From command to running code: the fork+exec idiom, what execve does (binfmt/ELF, tearing down & rebuilding the address space, the stack with argv/envp/auxv, jump to entry), static vs dynamic linking and ld.so with PLT/GOT lazy binding, shebang scripts, O_CLOEXEC, setuid and ASLR/PIE.

What happens between typing a command and its code running — how the kernel replaces a process's memory with a new program and hands control to it. Original, interview-focused notes; builds on Processes and the Address Space.

The Unix idiom is fork + exec: fork() makes a copy of the calling process, then exec() replaces that copy's address space with a new program. fork creates the process; exec loads the code. Same PID, brand-new memory.

fork then exec

What execve() actually does

  1. Find the format: the kernel checks the file's magic against registered binfmt handlers — ELF for binaries, script for #! files, misc for others (Java, Wine via binfmt_misc).
  2. Tear down & rebuild the address space: discard the old VMAs and map the new program's segments — text (code, read-only, shared), data/bss, and set up a fresh heap and stack.
  3. Set up the stack: push argv, envp, and the auxiliary vector (auxv) — kernel-supplied info (page size, entry point, the vDSO address, ASLR random bytes) the loader needs.
  4. Jump to the entry point in user mode.

Static vs dynamic linking & the loader

Scripts, security & hardening

Likely interview questions

Kernel Internals series. Related: Processes · Process Address Space.