Pointer Walk

System Calls: Asking the Kernel

Your program cannot touch the disk, the network or the screen. It asks the kernel to do it, through a narrow, guarded doorway.

Operating SystemsIntermediate8 min read

Your program cannot do very much

A program can calculate. It can read and write its own memory. And that is nearly all it can do by itself.

It cannot read a file. It cannot send data over the network, draw on the screen, find out the time, or start another program. All of those involve hardware or resources shared with other programs, and ordinary programs are not allowed to touch them.

For every one of those things, a program must ask the operating system to do it. That request is a system call.

Two modes

The processor itself runs in one of two modes.

  • Kernel mode: everything is permitted, including talking to hardware. Only the core of the operating system, the kernel, runs here.
  • User mode: restricted. Your programs, your browser and every app run here.

This is not a convention that polite programs follow. It is built into the processor. An instruction that touches hardware simply does not work in user mode.

Trying to go around the kernelInteractive
Your program
running (user mode)
Kernel
Disk

A program wants 100 bytes from a file.

Why so strict? Without it, any program could read any file, overwrite any other program's memory, or wipe the disk. One bug could take down the machine. With it, the worst a broken program can usually do is damage itself.

Crossing the boundary

So how does a program get a file read? It asks, through the one doorway provided.

Reading a file through a system callInteractive
Your program
running (user mode)
Kernel
Disk

A program wants 100 bytes from a file.

  1. The program puts its request in agreed places

    A number saying which operation, plus the arguments.

  2. It executes a special instruction

    The processor switches to kernel mode and jumps to a fixed entry point in the kernel. The program cannot choose where: it can only knock on the door.

  3. The kernel checks the request

    Is this file yours to read? Is that memory address really yours? Nothing from user mode is trusted.

  4. The kernel does the work

    Using its privileges.

  5. It returns the result and switches back to user mode

    The program continues, usually with a number saying success or failure.

You make them all the time

You rarely write a system call directly. Your language's library does it for you.

You writeSystem calls made underneath
open("notes.txt")open
file.read()read
print("hello")write to the terminal
requests.get(url)socket, connect, write, read, close
time.sleep(1)A sleep call that tells the kernel to pause this program
Starting another programfork and exec, or their equivalent

On Linux you can watch them happen:

bash
strace python hello.py

Even a one-line script makes hundreds of system calls before it prints anything.

They are not free

A system call costs far more than an ordinary function call. The processor has to switch modes, save and restore state, and the kernel has to validate everything. It is still only a fraction of a microsecond. The cost shows up when a program makes them in huge numbers.

Example

Why output is buffered

A program writes a million short lines to a file, one write system call each: a million crossings.

Instead, the library collects the lines in memory and makes one system call per few thousand bytes. The same data is written with a few hundred crossings.

That is what buffering is, and it is why output sometimes does not appear until a buffer fills or is "flushed".

The same thinking is behind reading files in large chunks, and behind sending many small messages as one batch.

Why this matters to you

  • It explains a class of slowness. A program that is "doing nothing" may be making millions of tiny system calls.
  • It is what permissions are built on. "Permission denied" is the kernel saying no at the doorway.
  • It is how sandboxes work. A container or a browser tab is restricted by limiting which system calls it may make and what they are allowed to see.

Key takeaways

  • Programs run in user mode and cannot touch hardware or other programs' resources.
  • The kernel runs in kernel mode and is the only code that can.
  • A system call is a request from a program to the kernel, through a fixed entry point.
  • The kernel checks every request before acting, which is what makes permissions enforceable.
  • Each call has a cost, so libraries batch work to make fewer of them.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress