System calls: the user and kernel boundary
Follow a read() call from the program into the kernel and back, and separate a change of privilege from a change of running task. Processes and threads · Operating systems.
A program calls read() and three bytes come back. It looks like any function call, yet the work behind it needs powers the program lacks. While that work happens, does the computer stop running your program and run something else? Follow one call into the kernel and back to find out.
Why a call has to cross a boundary
Take a text editor, program A with process ID 4301, running as the user ana. It wants three bytes of report.txt, a file ana owns. A program that could talk to the disk directly could overwrite every other user's files, so the CPU runs ordinary programs in a restricted user mode, where touching a device or another program's memory is not allowed. The operating system's core, the kernel, runs in kernel mode, where it is.
A system call is the way a program asks the kernel for a service on purpose: the program asks, and the kernel decides. A running program is what the scheduler, the part of the kernel that picks which program uses the CPU next, treats as one task. (The kernel can also be entered unasked: a timer interrupt, the clock hardware's signal that ends a task's turn, does it, as the previous lesson showed.)
Here are calls in the style of strace, a tool that lists a program's system calls, on Linux with an x86-64 CPU. The first number is a file descriptor, the integer handle for an open file; 3 means report.txt.
read(3, 0x7ffc1000, 3) = 3
read(99, 0x7ffc1000, 3) = -1 EBADF (Bad file descriptor)
read(3, NULL, 3) = -1 EFAULT (Bad address)The first line asks for three bytes into a buffer at address 0x7ffc1000, which belongs to A, and gets the count 3. The second names descriptor 99, which A never opened. The kernel checks the descriptor before any work, so it refuses at once with EBADF. The third passes a buffer address of zero, which is no part of A's memory. The kernel finds that out only when it tries to copy the bytes there, and then returns -1 with the error code EFAULT. Afterwards A's next instruction runs in every case.
The path of one call
The read you write in C is a wrapper from the C library, ordinary user-mode code. It cannot touch the disk. It puts the call number into a register (on x86-64, read is number 0 and goes in rax; on AArch64 it is 63 in x8), and executes the processor's trap instruction to enter the kernel: syscall on x86-64, svc on AArch64. That single instruction is the entry.
The CPU switches to kernel mode and jumps to an address the kernel fixed at boot, so a program cannot choose where it lands. Three layers are in play: the wrapper in the library, the trap instruction in the CPU, and the handler in the kernel. The wrapper is not the entry, and the entry is not the work.
Inside, the kernel checks what it can before it does the work. Is descriptor 3 open for reading? Only then does the handler fetch the bytes through the file system and disk and copy them into A's buffer. The result, 3, goes back in the result register (rax on x86-64) and the CPU returns to user mode at the instruction after the trap. After return the wrapper turns a negative result into -1, with the code stored in errno.
A read that is granted
Read the steps as text
- A holds the CPU, in user mode.
- A runs add r1 r2. Next up: syscall read.
- A calls read(3, 3).
- On x86-64 the wrapper puts the call number in rax and runs the syscall instruction, so the CPU switches to kernel mode (1 boundary crossing). The task is still A.
- Descriptor 3 is open for reading by A: granted.
- The kernel hands the request to the file system, which runs inside the kernel: an ordinary call (no boundary crossed). The file system sends the request to the disk in the hardware lane.
- The disk hands the result back to the file system. The file system hands the result back to the kernel.
- The kernel returns to user mode (1 boundary crossing): read(3, 3) gives 3 bytes. The task is A.
In the scene the kernel checks that descriptor 3 is open for reading before the file system and the disk do any work. It is, so the call is granted, and the task is A throughout. The ledger row beside it records that ana owns report.txt; that owner matters at the next example, an open.
When the kernel says no
The next example uses a simplified model: an already available ledger grants access only to the owner and rejects other users before contacting the disk. Program B, running as ben, asks to open report.txt, which belongs to ana. Real systems also check permissions for groups and others, and may read metadata while resolving a path before a refusal.
The same file, refused
Read the steps as text
- A and B share one CPU. B holds it, in user mode.
- B calls open("report.txt").
- On x86-64 the wrapper puts the call number in rax and runs the syscall instruction, so the CPU switches to kernel mode (1 boundary crossing). The task is still B.
- The ledger says report.txt belongs to user ana; B runs as ben: refused. The file and its owner are unchanged.
- The kernel returns to user mode (1 boundary crossing): open("report.txt") returns -1 with errno EACCES (permission denied). The task is B.
In this model the ledger says ana owns the file and B runs as ben, so the open is refused and returns -1 with EACCES (permission denied). This is a normal return, not a crash: B stays the running task, and the file, its owner and the disk are as before.
A mode switch is not a task switch
Entering the kernel changes the CPU's privilege, not the task: A's registers and place in the program are saved on entry and restored on return, and only the result register differs. A context switch is different: the scheduler gives the CPU to another task. A system call by itself never causes one; the scheduler does, when the caller cannot go on or its turn ends.
Here is a case. A pipe is a small kernel buffer that one task writes into and another reads from. This one holds up to 3 bytes and starts empty, with its writer still open. Reader R calls a blocking read for 3 bytes, and writer W is ready to run. The kernel has no bytes to return, so it marks R waiting and the scheduler runs W. W writes 3 bytes, which makes R ready again. When the next timer interrupt ends W's turn, the scheduler runs R, whose read completes with the count 3.
Count the task changes: R to W, then W to R, so two. The empty pipe caused the first change and the timer interrupt the second; the trap alone moved the CPU to no other task.
A read that has to wait
Read the steps as text
- R and W share one CPU. R holds it, in user mode.
- R calls read(Q, 3). On x86-64 the wrapper puts the call number in rax and runs the syscall instruction, so the CPU switches to kernel mode (1 boundary crossing). The task is still R.
- Q is empty and the read may wait, so R is marked waiting. It cannot run until data arrives. The scheduler picks W, so the running task changes from R to W. R stays waiting.
- The kernel returns to user mode (1 boundary crossing). W starts at its first instruction.
- W calls write(Q, 3). On x86-64 the wrapper puts the call number in rax and runs the syscall instruction, so the CPU switches to kernel mode (1 boundary crossing). The task is still W.
- 3 bytes move from W into Q, which now holds 3 of 3. R was waiting, so it is ready again. The kernel returns to user mode (1 boundary crossing): write(Q, 3) gives 3 bytes. The task is W.
- The timer raises its interrupt line. At the next instruction boundary the CPU saves W's place and enters the kernel (1 boundary crossing).
- The scheduler picks R, so the running task changes from W to R. 3 bytes move from Q to R; Q now holds 0 of 3.
- The kernel returns to user mode (1 boundary crossing): read(Q, 3) gives 3 bytes. The task is R.
Now set one flag, O_NONBLOCK, on the same empty pipe with a writer still open. In this run, no interrupt or other scheduling event intervenes before the return. The kernel still has no data, but it must not make R wait, so it returns -1 with EAGAIN ("try again") at once.
The same empty pipe, without waiting
Read the steps as text
- R and W share one CPU. R holds it, in user mode.
- R calls read(Q, 3) in non-blocking mode.
- On x86-64 the wrapper puts the call number in rax and runs the syscall instruction, so the CPU switches to kernel mode (1 boundary crossing). The task is still R.
- The kernel returns to user mode (1 boundary crossing): read(Q, 3) returns -1 with errno EAGAIN (no data, and the call must not wait). The task is R.
In this run R keeps the CPU even though W is ready. The flag changed the kernel's decision about whether the caller waits.
Recap
- No, the computer does not run something else by default. The wrapper runs in user mode, one trap instruction enters the kernel, the handler does the work, and the same task resumes at the next instruction.
- A bad descriptor or an open denied by the model returns
-1and an error code; the modeled refusal leaves the file and disk unchanged. - A system call never changes the task by itself; the scheduler does, when the caller cannot continue or a timer interrupt ends a turn. The empty pipe made R wait and a timer interrupt later ended W's turn, two changes in all, and with
O_NONBLOCKthere was none.
Boundaries to revisit
The raw Linux interface returns a negative error number in the result register, and the C library turns that into -1 plus errno; other kernels and architectures differ in the numbers and registers. Some Linux calls, such as reading the clock, can be answered in user space through the vDSO, a small kernel-provided library, without any trap, so not every library call that sounds like a service enters the kernel. The Linux kernel entry documentation describes entry handling as a boundary that may return to the current task. The Linux syscall overview and syscall(2) ABI notes give architecture-specific detail. POSIX, the portable interface standard behind these calls, specifies read() and write() behavior without prescribing one kernel design.
Check your understanding with the quiz, then continue to Process states and the saved path back to the CPU.