CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 7 of 8 · 3 min read

The cost of a call

The cost of crossing the door

Think of paying a toll at a gate. Each crossing costs a little time even when the road is empty, so a driver who crosses a million times pays a lot. A system call is a crossing like that. The analogy stops in one place: a toll is money, and a system call costs processor time and warm caches.

In plain words: A plain function call stays inside one program. A system call crosses into the kernel and back: switch the mode, save and restore registers, check the arguments. It costs more than a normal call. Numbers vary with the processor and the kernel, so treat these as made up to show the idea. Suppose an ordinary function call takes 2 nanoseconds and a system call takes 200 nanoseconds. Then a loop that makes one million system calls spends 1,000,000 × 200 ns = 0.2 seconds in crossing alone, against 0.002 seconds for the same number of plain calls.

This is why programs batch their work. Writing 1,000,000 bytes with a million one-byte write calls is far slower than writing them in blocks of 4,096 bytes, which needs only 245 calls (1,000,000 / 4,096 is 244.1, so 245). The C library does exactly this with printf: it collects output in a buffer and calls write when the buffer is full, or when a newline appears on a terminal, or when the program ends.

The key lines.

You can observe the buffer with a program that uses both ways to print:

#include <stdio.h>
#include <unistd.h>

int main(void) {
    printf("line from printf\n");
    write(1, "line from write\n", 16);
    return 0;
}

When the output goes to a pipe or a file, as in ./a.out | cat, the lines come out in this order:

line from write
line from printf

The printf text waited in the library's buffer until the program ended, while write went to the kernel at once. On a terminal the order is as written, because the library sends a line out at each newline. Adding fflush(stdout) after the printf forces the buffer out and keeps the order in every case.

Interview angle

Answer in your head first, then open it.

What happens on a system call?

The wrapper loads the number and arguments, executes a trap, the processor enters kernel mode at the trap vector, the kernel checks arguments and runs the service routine, then returns to user mode.

Why two modes?

So only the OS can run privileged instructions and touch devices and all memory.

System call versus library call?

A system call enters the kernel. A library function may or may not make one.

How does the OS get the CPU back from a looping program?

A timer interrupt.

Check yourself: 1. How many system calls does an unbuffered one-byte write loop make for 10,000 bytes? 2. How many does a 4,096-byte buffer need for the same data? 3. Why does the C library buffer output?

Words you will meet

Word Meaning
Mode bit A processor bit that says user mode or kernel mode
Kernel The core of the OS, which runs in kernel mode
Trap A software-generated interrupt, caused by an error or a system call
Trap vector A table of addresses of the kernel's handlers
Wrapper A library function that makes a system call
errno The global variable that holds the error code of the last failed call
POSIX A standard API for Unix-like systems

Quick notes

  • User mode cannot run privileged instructions. Kernel mode can run anything.
  • A system call is a special instruction that enters the kernel at a fixed point, with a call number in a register.
  • The C library wraps system calls. On failure a wrapper returns -1 and sets errno.
  • strace lists every system call of a program, so calls can be counted.
  • A timer interrupt and traps let the OS keep control.
  • Each crossing into the kernel costs time, so programs batch their work.