Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 66 ยท Getting Started

Kernel, User Mode and System Calls

Understand the kernel, user mode versus kernel mode, interrupts, traps and system calls, and watch real open, write, read and close calls run in a live Python demo.

The kernel is the core of the operating system: the part that is always in memory and that runs with full control of the hardware. Everything else, from your browser to the shell, runs as an ordinary program and has to ask the kernel for anything involving hardware, files, networks or other programs. The formal request mechanism is the system call.

This lesson explains how the CPU enforces the line between kernel and applications (user mode and kernel mode), what exactly happens during a system call, and which kinds of calls exist. You will also run real calls (open, write, read, close) from Python.

Two privilege levels

User mode Applications. Restricted instructions.
Kernel mode The OS. Can touch any hardware.

A mode bit in the CPU records which level is active.

1 / 2

User mode and kernel mode

If any program could execute any instruction, a buggy or malicious one could erase the disk, reprogram the timer or read another program's memory. So CPUs support at least two modes, controlled by a mode bit:

  • User mode. Normal application code. The CPU refuses privileged instructions (I/O instructions, changing the memory map, disabling interrupts, halting the CPU). An attempt raises an exception and the OS typically terminates the program.
  • Kernel mode (supervisor, privileged mode). The OS code. Everything is allowed.

Modern CPUs sometimes have more rings (x86 has four, though OSes use two), but the principle is the same. The only way from user mode into kernel mode is through a controlled entry point that the kernel itself set up. A program cannot jump to a random kernel address.

What the kernel is responsible for

A kernel usually contains: the process scheduler, memory manager, file systems, network stack, device drivers, and the code that handles interrupts and system calls. Whether all of that lives in one big program or is split into small servers is the question of kernel structure.

Interrupts, traps and exceptions

Three events switch the CPU into kernel mode:

Event Cause Example
Interrupt Asynchronous signal from hardware A key is pressed, a disk finishes, the timer ticks
Trap (software interrupt) A deliberate instruction in the program syscall, int 0x80, svc
Exception / fault Error caused by the running instruction Divide by zero, invalid memory access, page fault

The CPU looks up a handler in the interrupt vector table, saves the current state, switches to kernel mode and runs the handler. When it is done, it restores the state and returns to user mode. The timer interrupt is how the OS gets control back from a program that never gives up the CPU, which is what makes preemptive scheduling possible.

What a system call looks like

A system call is a request from a program to the kernel. Programmers rarely issue them directly. They call a library function (C's write(), Python's file.write), and that function:

  1. puts a system call number and the arguments into agreed registers,
  2. executes the trap instruction,
  3. lets the kernel look the number up in the system call table, validate the arguments, do the work and put a result (or a negative error code) in a register,
  4. returns to user mode, where the wrapper hands the result back.

The next example builds a toy kernel with a syscall table so you can see the structure. The "application" can only reach the files through the syscall() entry point, and the kernel checks every request.

Python A toy system-call table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real system calls from Python

Python's os module exposes thin wrappers over the real calls (the names match the C functions). The example below runs inside Pyodide's in-memory file system, but the calls and return values behave like on Linux. Notice that os.open returns a small integer, the file descriptor: the kernel's handle for an open file.

Python open, write, read, close

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On a real machine you can watch these with strace (Linux) or Process Monitor (Windows). A simple print("hi") triggers a write call underneath.

Types of system calls

Category Purpose Typical Linux/POSIX calls
Process control Create, end, wait, load programs fork, execve, exit, wait
File management Create, open, read, write, close open, read, write, close, unlink
Device management Request and release devices, ioctl ioctl, read, write
Information maintenance Time, process attributes, system data getpid, time, uname
Communication Pipes, sockets, shared memory pipe, socket, shmget, mmap
Protection Permissions and ownership chmod, chown, setuid

Windows names its equivalents differently (CreateProcess, ReadFile, WriteFile), and applications normally reach them through the Win32 API rather than the raw numbers.

Why system calls cost time

A system call is much slower than an ordinary function call: the CPU must switch modes, save and restore registers, and often flush parts of its caches. That is why programs buffer I/O (collect many small writes into one big call) and why kernels try to keep the fast path short.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can a user program not execute I/O instructions directly?

    Privileged instructions are blocked in user mode so programs cannot damage the system or each other.
  2. 2. Which of these is a software-generated trap used to enter the kernel on purpose?

    A system call instruction deliberately raises a trap. Timer and keyboard interrupts come from hardware; a page fault is an exception.
  3. 3. What does the kernel return from a system call that fails?

    Calls report failure through error codes, which wrappers expose as errno or exceptions.
  4. 4. Why do programs buffer output instead of making one system call per character?

    Mode switches have a real cost, so grouping data into fewer calls is faster.

Summary

  • The kernel is the always-resident core of the OS and runs in privileged kernel mode.
  • User programs run in user mode and enter the kernel only via interrupts, traps (system calls) or exceptions.
  • A system call passes a call number and arguments, runs a kernel handler, and returns a result or error code.
  • Calls fall into process control, file, device, information, communication and protection groups.
  • Mode switches cost time, so I/O is buffered.

Next: How a Computer Boots