Operating Systems5h estimated

System Calls & the Kernel

Difficulty
Importance

The problem

A program needs to do privileged things — read a file, open a network socket, allocate memory — that it cannot be trusted to do directly, because a bug or malicious program could otherwise damage other processes or the machine itself. There has to be a controlled gate between untrusted programs and the hardware they run on.

Why now

File systems and I/O, and virtual memory, both quietly assumed 'the OS mediates access' without explaining the actual mechanism. System calls are that mechanism: the one narrow, controlled doorway through which a normal program requests anything privileged from the kernel.

Mental model

Ordinary code runs in user mode with restricted privileges; the kernel runs in privileged mode with full hardware access. A system call is a deliberate, controlled jump from user mode into the kernel to ask it to do something on your behalf — open a file, send a packet — after which control returns to user mode. Every 'privileged' action in a program is secretly one of these controlled jumps.

Requires

Helps

Unlocks

Used in

Projects

  • Use strace (Linux) or an equivalent tool to trace the system calls made by a simple program (e.g. cat a file) and identify open/read/write/close
  • Explain, from the system call trace, exactly what a one-line file-reading program actually does at the OS level

Examples

  • console.log ultimately triggers a write() system call to output a file descriptor
  • Opening a network connection is a socket() system call followed by connect()

Resources

Mastery checklist