Lesson 3 · Operating Systems

Kernel, User Mode, and System Calls

Applications run with limited permission. When they need a protected operation—such as reading a file or creating a process—they enter the kernel through a defined system call.

Kernel, User Mode, and System Calls concept diagramA visual explanation of the layout and operations shown in this lesson.ApplicationLibraryKernelDeviceAPI callsystem calldriverprotected kernel modeuser mode
1

User mode and kernel mode

Modern processors provide privilege levels. Application instructions execute in user mode, where protected instructions and kernel memory are unavailable. The operating-system kernel executes in a privileged mode that can configure memory translation, devices, and interrupts.

This separation limits damage from application bugs. A crashed application should not be able to rewrite another process's page table or disable the system clock.

  • Privilege is enforced by the processor
  • Page permissions protect kernel memory
  • Drivers usually execute with elevated access
2

What happens during a system call

The application places a call number and arguments where the platform's calling convention expects them. A special instruction transfers control to a kernel entry point and changes privilege safely.

The kernel validates pointers, permissions, and resource identifiers before performing work. It stores a return value or error code, restores user execution state, and returns to the instruction after the call.

  • Prepare call number and arguments
  • Enter through a controlled CPU instruction
  • Validate and perform the operation
  • Return a value or error
Key reference

Terms, operations, and practical uses

Privilege boundary

  • User modeRestricts protected instructions and direct access to kernel memory.
  • Kernel modePermits the operating system to configure hardware and protected state.
  • System callA deliberate, controlled transition from a program into a kernel service.

Call sequence

  • PreparePlace the call number and arguments according to the platform convention.
  • ValidateCheck addresses, permissions, handles, lengths, and resource state.
  • ReturnRestore user execution with a result or an explicit error code.

Related control transfers

  • InterruptAn asynchronous hardware event such as timer or device completion.
  • ExceptionA condition raised by the current instruction, such as a page fault.
  • Library wrapperA programmer-friendly function that may buffer or combine several lower-level calls.
Code example

Read a file through a library and system call

from pathlib import Path

# each call below traps into the kernel to reach the disk
Path("notes.txt").write_text("hello")
text = Path("notes.txt").read_text()
print(text)
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main() {
    ifstream file("notes.txt");
    string text;
    getline(file, text);
    cout << text << '\n';
}
import java.nio.file.Files;
import java.nio.file.Path;

class Main {
    public static void main(String[] args) throws Exception {
        String text = Files.readString(Path.of("notes.txt"));
        System.out.println(text);
    }
}
Inputnotes.txt contains: hello
Outputhello
Example

Run the example step by step

Output
3

Libraries and APIs

Most programs call language or operating-system libraries rather than issuing processor instructions themselves. Python's Path.read_text, Java's Files.readString, and C++ file streams eventually rely on lower-level OS file operations.

A library call is not always one system call. It may buffer data, transform arguments, reuse an existing result, or make several kernel requests.

  • API describes the programmer-facing operation
  • System-call interface crosses the privilege boundary
  • Buffering can reduce kernel transitions
4

System calls, interrupts, and exceptions

A system call is requested by the running program. A hardware interrupt is raised asynchronously by a device or timer. An exception is raised by the current instruction—for example, division by zero or a missing memory page.

All three can transfer control to kernel handlers, but their causes differ. The kernel records enough execution state to handle the event and later resume, signal, or terminate the affected work.

  • System call: intentional service request
  • Interrupt: external asynchronous event
  • Exception: condition caused by the current instruction