← SEARCHABLE ARCHIVE
Operating System · 2026-07-12

System calls and context exchange

How does a user program request work from the kernel?

INTERACTIVE READING MODE →

The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.

User mode programs cannot directly control disk or network devices. Instead, it requests work from the operating system, and this formal request method is called a System Call.

How is it different from a regular function call?

A general function call only changes the execution location within the same privilege area. For system calls, CPU permissions change from user mode to kernel mode, and after the kernel processes the request, it returns to user mode.

Generic function call
User code → User function → User code

system call
User code → Kernel entry → Permission and argument check → Kernel operation → User code

Functions such as opening files, memory mapping, socket communication, and process creation ultimately use system calls. Programmers usually call the operating system API or standard library, and the appropriate system call is executed in the layer below.

It is different from Context Switch

Invoking a system call does not necessarily change to another process. The same thread may enter kernel mode, process the request, and return immediately.

Context Switch is the process of changing the execution target by saving the current execution state and restoring the state of another thread or process.

System call: The same executor requests work from the kernel.
Context switching: CPU changes the subject itself to execute

However, system calls that need to wait for files or network data can put the current thread in a waiting state. In the meantime, the operating system runs other threads to avoid wasting the CPU, and context switching may occur at this time.

System calls are not free

Privilege switching and parameter checking cost money. This is one of the reasons why it is advantageous to collect small data in a buffer and write it at once rather than writing thousands of times one byte.

std::string buffer;

for (const auto& line : lines)
{
    buffer += line;
    buffer += '\n';
}

file.write(buffer.data(), buffer.size());

However, this does not mean that system calls should be avoided at all costs. It is a necessary boundary for the operating system to manage resources safely, and it is a good idea to look at the number of calls and waiting methods when performance problems are identified.