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.
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 call | What it does |
|---|---|
openat | Opens a file relative to a directory and returns a file descriptor |
read | Copies bytes from a file descriptor into a user-space buffer |
write | Copies bytes from a buffer to a file descriptor |
close | Releases a file descriptor |
execve | Replaces the current program image with a new program |
clone | Creates a new process or thread; the C library uses it to implement fork() |
wait4 | Waits for a child process to change state and collects its status |
exit_group | Terminates every thread in the process with a status code |
mmap | Maps memory or files into the address space |
getdents64 | Reads 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:
ls -l /proc/$$/fdDescriptors 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:
cat /nopeThe 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:
echo hello > hello.txt
strace -e trace=openat,read,write cat hello.txtThe -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:
openat(AT_FDCWD, "hello.txt", O_RDONLY) = 3
read(3, "hello\n", 131072) = 6
write(1, "hello\n", 6) = 6
read(3, "", 131072) = 0Read 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.