Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
System calls explained: user mode vs kernel mode, what happens during a syscall, why crossing the boundary costs time, and how to make fewer calls.
Quick answer: A system call (syscall) is how a program asks the operating system's kernel to do something it is not allowed to do itself, such as read a file, send network data, allocate memory or start another process. The program places a request number and arguments in CPU registers and executes a special instruction that switches the CPU into privileged kernel mode. The kernel checks the request, does the work, and switches back. That round trip costs far more than an ordinary function call, so fast programs try to make fewer, larger system calls.
Also Read: SSD vs HDD: Why SSDs Are So Much Faster - How To's
CPUs run code at different privilege levels:
This split is what keeps a buggy or malicious program from corrupting the disk or reading another program's data. But programs still need files, networks and screens. System calls are the controlled doorway between the two worlds.
Take read(fd, buffer, 4096), which reads up to 4,096 bytes from a file:
syscall; on ARM64 it is svc. The CPU switches to kernel mode and jumps to a fixed entry point set up by the kernel.Linux has several hundred system calls, listed in the syscalls manual page. Most programs use only a few dozen.
| Category | Examples | What they do |
|---|---|---|
| Files | open, read, write, close | Work with files and devices |
| Processes | fork, execve, exit, wait | Create and manage processes |
| Memory | mmap, brk | Request or map memory |
| Network | socket, connect, send, recv | Network communication |
| Waiting for events | epoll_wait, select, poll | Wait on many files or sockets at once |
High-level operations break down into these. Printing a line in Python eventually becomes a write call. Starting a program involves fork and execve, as described in what happens when you run a program.
An ordinary function call takes around a nanosecond. A simple system call typically takes from around a hundred nanoseconds to a microsecond or more, and often longer once the real work is counted. The reasons:
None of this matters for a call made occasionally. It matters a great deal for a call made millions of times.
Writing one byte at a time with a system call per byte would be disastrously slow. Standard libraries collect output in a buffer in user memory and issue one write for thousands of bytes. This is why output sometimes does not appear until you "flush" it.
# Slow: one system call per line when unbuffered
for line in lines:
os.write(fd, line)
# Fast: one system call for everything
os.write(fd, b"".join(lines))
Calls such as readv, writev and sendmmsg handle several buffers or messages in one trip.
Some frequently used calls, such as getting the current time, do not need privileged access at all. Linux exposes them through the vDSO, a small piece of kernel-provided code mapped into every process, so they run as ordinary function calls.
With mmap, a file appears as a region of memory. After the mapping is set up, reading it needs no further system calls; the virtual memory system loads pages as they are touched.
Also Read: How CPU Scheduling Works in Your Operating System
Linux's io_uring lets a program queue many I/O requests in a shared memory ring and submit them with one call, or sometimes none. High-performance servers use event-driven designs for the same reason; see what is the event loop.
On Linux, strace shows every system call a program makes:
strace -c ls # summary: count and time per syscall
strace -e trace=openat cat /etc/hostname
macOS has dtruss, and Windows has Process Monitor. Watching a familiar command this way is one of the quickest ways to understand what software really does.
No. A function call stays inside your program. A system call crosses into the kernel, with a change of privilege level and extra checks.
Not necessarily. A system call switches mode within the same process. A context switch changes which process or thread is running. A system call that blocks will usually cause one.
A library API such as fopen or printf is ordinary code that may or may not make system calls underneath. One library call can trigger zero, one or many system calls.
Containers share the host kernel, so their system calls work normally. Virtual machines run their own kernel; some operations cost more because they also involve the hypervisor.
System calls are the boundary where your program's world ends and the operating system's begins. The boundary exists for safety, and crossing it has a real cost. The practical lesson is simple: do I/O in large chunks, let libraries buffer for you, and when performance matters, count your system calls before optimising anything else.