$_ CLI In Depth
Learning progress 0%
lesson terminal shell kernel

System Calls: Crossing into the Kernel

6 min read Module 1: Terminal, Shell & Kernel Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:22

Two worlds: user space and kernel space

The CPU runs code in one of two modes. Kernel code runs in kernel space with full access to memory and hardware. Your programs, including the shell, ls, and cat, run in user space with restricted access. A user-space process can only touch its own memory. It cannot read another process's memory, write to a disk controller, or change the page tables that define what memory it can see.

This separation is what makes a multi-user system safe. If any program could touch hardware directly, one buggy or malicious program could corrupt the file system or read another user's data. The kernel is the only code that owns the hardware, and it decides what each process may do.

The trap: how a program asks for help

A user-space program asks the kernel for a service with a system call. The program puts a call number and its arguments into agreed registers, then executes a special instruction. On x86-64 that instruction is syscall. On aarch64, the architecture the lab's programs are built for, it is svc. The instruction traps: the CPU switches to kernel mode and jumps to the kernel's system call entry point. The kernel checks the arguments, performs the work, places a return value in a register, and switches back to user mode.

The diagram shows the path for a file open. The program calls an ordinary function, the function traps into the kernel, and the kernel reaches the hardware only when it needs to.

flowchart TB app["Program in user space"] --> libc["libc wrapper such as openat"] libc -->|"trap into kernel"| kern["Kernel in kernel space"] kern --> hw["Disk, terminal, network"]

libc wrappers

Programs rarely write the trap by hand. The C library (glibc on most Linux distributions, musl on Alpine and in this lab) provides a normal function for each system call. When a C program calls open() or read(), it is calling a small wrapper that sets up the registers and traps. The wrapper also converts the kernel's error report into a return value, which you will see in the errno section below. Some library calls make fewer system calls than you might expect. printf() writes into a user-space buffer, and the write call happens later, when the buffer is flushed.

The section 2 man pages describe the system calls themselves, and the section 3 pages describe the library wrappers. On Linux, syscalls(2) lists the calls and their numbers for each architecture. That page is the reference to check when you see a name in strace output.

Common system calls

Most everyday commands use a small set of calls. The table lists the ones you will meet most often in traces:

System callWhat it does
openatOpens a file relative to a directory and returns a file descriptor
readCopies bytes from a file descriptor into a user-space buffer
writeCopies bytes from a buffer to a file descriptor
closeReleases a file descriptor
execveReplaces the current program image with a new program
cloneCreates a new process or thread; the C library uses it to implement fork()
wait4Waits for a child process to change state and collects its status
exit_groupTerminates every thread in the process with a status code
mmapMaps memory or files into the address space
getdents64Reads directory entries, which is how ls lists a directory

When you type ls /workspace, bash forks a child with clone. The child calls execve to become ls. ls then uses openat and getdents64 to read the directory. Bash waits with wait4, and ls finishes with exit_group. That is a full command in about ten calls, and most of them are hidden from you.

File descriptors: the handle

When openat succeeds, it returns a small integer called a file descriptor. The integer is an index into a table that the kernel keeps for each process. The descriptor is only a handle. The real file is described by the kernel, and the program refers to it by number in later calls like read(3, ...). By convention, every process starts with three descriptors open: 0 for standard input, 1 for standard output, and 2 for standard error.

You can see the descriptors of your own shell. In the lab, this lists them:

BASH1 lines
ls -l /proc/$$/fd

Descriptors 0, 1, and 2 point at /dev/pts/0, the terminal. You will also see descriptor 255. Bash moves its own copy of the terminal to a high number so that it keeps it when a program redirects the standard descriptors. Redirection is just a change to this table. Writing cmd > out.txt makes descriptor 1 refer to the file before cmd starts.

errno and error messages

When a system call fails, the kernel returns a negative error code, and the C wrapper stores a positive number in the thread-local variable errno and returns -1. Each number has a symbolic name that you can read in errno(3). The errors you will see most are ENOENT (no such file or directory), EACCES (permission denied), and EISDIR (is a directory). Tools translate these names into the text you see:

BASH1 lines
cat /nope

The message is cat: /nope: No such file or directory. That text is the description of ENOENT, produced by the program that got the failure from openat. The kernel itself does not print any text.

strace: a window on the calls

strace runs a program and prints each system call it makes, with its arguments and return value. It is the best tool for seeing what a command really does. Start with a file you can read:

BASH2 lines
echo hello > hello.txt
strace -e trace=openat,read,write cat hello.txt

The -e trace= option limits output to the calls you name. A trimmed version of the output looks like this, with the file descriptor numbers and buffer sizes simplified:

TEXT4 lines
openat(AT_FDCWD, "hello.txt", O_RDONLY) = 3
read(3, "hello\n", 131072) = 6
write(1, "hello\n", 6) = 6
read(3, "", 131072) = 0

Read the lines in order. openat returned descriptor 3. read returned six bytes. write sent those bytes to descriptor 1, the terminal. The final read returned zero, which means end of file. The word hello inside the quotes is the data that was copied, while each line that starts with a call name is strace's report.

Two more options are worth knowing. strace -c counts calls and prints a summary table, with the time and error count for each call. strace -f follows child processes, which matters when the program forks. Without -f, you see the shell start a child but not what the child does. Strace writes its output to standard error, not standard output, so use 2>&1 if you want to pipe it into another command. The fuller reference is the strace(1) man page.

Terminal lab · 3 steps · real Linux in your browser

Watch programs talk to the kernel

Trace the openat, read and write calls behind cat, count syscalls, and catch an ENOENT.

01
Trace cat
Trace every system call cat makes while printing hello.txt: ``bash strace -o cat-trace.txt cat hello.txt ` In cat-trace.txt, find the openat of hello.txt, the read of its bytes, and the write(1, ...)` to your terminal.
Show hint for step 1
strace's own report goes to the file, while cat's output still reaches the terminal.
02
Count the calls
Let strace count instead of list. Summarize a recursive listing: ``bash strace -c -o summary.txt ls -R /usr/share > /dev/null `` Which system call reads directory entries?
Show hint for step 2
`-c` prints a table of calls, counts and errors when the program exits.
03
Catch the error at the source
cat /nope prints "No such file or directory". Find where that message comes from: ``bash strace -e trace=openat -o enoent.txt cat /nope ``
Show hint for step 3
The kernel returns -1 with errno ENOENT, and cat turns that into the message.
Check yourself Question 1 of 4
Concept Check What a libc wrapper does recall

What does a libc wrapper such as open() do when a C program calls it?

Recap & Key Takeaways

User programs run in user space and must ask the kernel, through a system call trap, for any access to files, processes, or hardware.
libc wrappers such as open() and read() set up the registers and trap; on aarch64 the file-open call is openat, not open.
File descriptors are integers that index the kernel's per-process table; 0, 1, and 2 are standard input, output, and error.
strace shows each call and its errno-style result; it writes to stderr, so use 2>&1 before piping, and -f follows child processes.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue