What is a system call?

Video 029 of 140 in Virtualization Foundations, in the unit “Privilege, system calls and interrupts”. 3:36, with English and Hindi audio.

A program cannot open a file, send a packet or start another program by itself. It has to ask the operating system. The formal request is called a system call. Every file save, network send and memory request your server makes goes through one. This article explains which jobs only the kernel can do, how one system call travels from the program to the kernel and back, and what goes wrong when a call fails or is made too often.

What you will learn

The jobs that only the kernel can do

Each of these touches a shared resource. The disk, the network card and the memory are shared by every program on the machine. So the kernel must do the work and keep order between the programs.

How one system call travels to the kernel and back

Take a warehouse app that saves a customer's order. It makes three system calls: open the file, write the data, and close the file. Each call crosses into the kernel, and each call is checked. If the disk is full, the write call can return an error, and the app must report it.

How to read the manual for a system call

You can read the system calls on the machine itself. The manual's section two covers them, with one page for each call. Type man, then the section number, then the name of the call. Each page lists the arguments, the result, and the errors the call may return.

Open the manual page for the write system call
man 2 write
Open the manual page for the read system call
man 2 read

Note: On minimal images, some manual pages may be missing.

What goes wrong with system calls

System calls can fail. The kernel may refuse a request and return an error such as permission denied. The program must handle that answer. You would notice it in logs, as permission denied lines, or as a busy process that seems to make no progress.

A program that makes a huge number of calls pays for each crossing into the kernel. Its speed suffers even when every call succeeds.

Read two system call manual pages

You need a Linux terminal. The manual pages are on the machine itself, so you do not need the internet. Each page lists the arguments, the result and the possible errors.

  1. Open the manual page for the write system call. Section two of the manual covers system calls.

    man 2 write

    What you should see: The page lists the arguments of write, its result and the errors it may return. Press q to leave the page.

  2. Open the manual page for the read system call, and compare it with write.

    man 2 read

    What you should see: A page with the same layout: the arguments, the result and the possible errors for read.

Terms

System call
A program's formal request to the kernel for a service, such as opening a file or sending data over the network.
Kernel
The core of the operating system, which does the work that only it may do and keeps order between programs.
Register
A tiny storage slot inside the processor that holds a number it is working on right now.
Kernel mode
The processor mode in which the kernel runs, with full access to the hardware and memory.

Check yourself

Why can a program not write to the disk on its own?

The disk is shared by every program, so only the kernel may write to it. The program must ask the kernel with a system call.

Can a program choose where the processor lands when it makes a system call?

No. The processor jumps to an entry point that the kernel set up in advance, so the program cannot pick the destination.

What does a system call return when it is refused?

An error such as permission denied. The program must handle that answer.

Previous: The x86 privilege rings. Next: Watching system calls with strace.