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
- A system call is a program's formal request for a service that only the kernel may perform.
- Only the kernel may open files, send packets and start programs, because those resources are shared.
- The request enters kernel mode through a fixed doorway, and the program cannot choose where it lands.
- The kernel checks each request before it acts, and it may return an error such as permission denied.
The jobs that only the kernel can do
- Files: open, read and write data on the disk.
- Network: send and receive data over the network card.
- Processes: start a program, or stop one.
- Memory: ask for more memory for the program.
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.
- The program places a call number and its arguments into registers, which are small storage slots inside the processor.
- It runs a special instruction. On x86-64 Linux, that instruction is called syscall. It is a deliberate doorway into the kernel, not a free jump anywhere.
- The processor enters kernel mode and jumps to an entry point that the kernel set up in advance. The program cannot choose where it lands.
- The kernel reads the call number, checks that the caller may do this, and then does the work, such as reading the disk.
- The result goes back in a register. The processor returns to user mode, and the program carries on.
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.
man 2 writeman 2 readNote: 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.
Open the manual page for the write system call. Section two of the manual covers system calls.
man 2 writeWhat you should see: The page lists the arguments of write, its result and the errors it may return. Press q to leave the page.
Open the manual page for the read system call, and compare it with write.
man 2 readWhat 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.