System Calls and APIs
A system call is the formal, controlled request an application makes to the OS kernel to do privileged work it cannot do directly, such as reading a file or sending network data. It switches the CPU from user mode to kernel mode. APIs are the friendlier library functions, like open() or read() in C, built on top.
Applications run with restricted privileges on purpose: they can't directly touch disk hardware, network cards, or another program's memory. When an application genuinely needs one of those things, like opening a file, it has to formally ask the OS kernel to do it on its behalf. That formal request is called a system call.
How a system call actually works
- The application calls a library function, like
open()orread()in C, which looks like an ordinary function call from the programmer's point of view. - That library function triggers a special CPU instruction that switches the processor from user mode (restricted) to kernel mode (privileged). This mode switch is the real security boundary, it's enforced by the CPU itself, not just software convention.
- The kernel looks at which system call was requested (open a file, read from a socket, allocate memory) and a number identifying it, then runs the actual privileged code to do that specific job.
- The kernel returns a result (a file handle, an error code, the bytes read) and switches the CPU back to user mode.
- Control returns to the application, which continues running with the result it asked for.
This round trip, called a context switch into the kernel and back, has real overhead, which is exactly why heavy system-call usage (opening and closing tiny files in a tight loop, for instance) is a common real performance bottleneck.
System calls vs APIs
A system call is the actual privileged mechanism, identified by a number, handled directly by the kernel. An API (Application Programming Interface) is usually a friendlier, higher-level function that a programmer actually calls, which may or may not map to exactly one system call underneath. fopen() in the C standard library, for example, is an API function that internally makes an open() system call, but also adds buffering and error-handling convenience on top that the raw system call doesn't provide. Not all APIs touch the kernel at all, plenty of library functions do pure computation with no system call involved.
A concrete example
Reading a file in a typical program looks like calling read(file, buffer, size). Under the hood: the C library wraps this into the actual read system call, the CPU switches to kernel mode, the kernel's file system code finds the requested bytes on disk (or in a disk cache), copies them into the buffer the application provided, switches back to user mode, and returns the number of bytes actually read. From the application's perspective this looks like an ordinary function call; the user-mode/kernel-mode switch and the actual disk I/O are completely hidden behind that one line of code.
Where this matters in real systems
Every OS exposes system calls this way: Linux has open, read, write, fork, execve and hundreds more; Windows exposes a similar set through the Win32 API, which itself sits on top of lower-level native system calls most application developers never touch directly. Tools like strace on Linux let engineers watch every system call a running program makes in real time, which is a genuinely common debugging technique for figuring out exactly what a misbehaving program is doing at the OS level.
What interviewers actually ask about system calls
- "What is a system call?" The formal mechanism an application uses to request a privileged operation from the OS kernel, like file I/O or process creation.
- "What's the difference between user mode and kernel mode?" User mode is restricted, applications can't directly touch hardware or other processes' memory; kernel mode is privileged and can do anything, enforced by the CPU itself, not just software.
- "What's the difference between a system call and an API?" A system call is the actual privileged kernel-level mechanism; an API is a higher-level function a programmer calls, which may wrap one or more system calls plus extra convenience logic, or may not touch the kernel at all.
- "Why are system calls relatively expensive?" Because they require a full mode switch from user mode to kernel mode and back, which has real CPU overhead beyond a normal function call.
Key takeaways
- A system call is how a restricted application formally asks the privileged kernel to do something on its behalf.
- It involves a real mode switch (user mode to kernel mode and back), enforced by the CPU, with genuine performance overhead.
- APIs are higher-level functions programmers actually call; they may wrap system calls with extra convenience, or may involve no system call at all.
- Tools like
stracelet you observe exactly which system calls a running program makes.
More in Introduction
Browse all Operating Systems topics
Log in to save which topics you've read across every subject.
Frequently asked questions
What is a system call?
The formal mechanism an application uses to request a privileged operation from the OS kernel, like file I/O or process creation.
What's the difference between user mode and kernel mode?
User mode is restricted, applications can't directly touch hardware or other processes' memory; kernel mode is privileged and can do anything, enforced by the CPU itself, not just software.
What's the difference between a system call and an API?
A system call is the actual privileged kernel-level mechanism; an API is a higher-level function a programmer calls, which may wrap one or more system calls plus extra convenience logic, or may not touch the kernel at all.
Why are system calls relatively expensive?
Because they require a full mode switch from user mode to kernel mode and back, which has real CPU overhead beyond a normal function call.