OS Architecture
An operating system is not one big blob of code. It is built in layers, each using the services of the layer below and offering simpler services to the layer above. This lesson walks through the layers of Linux and Windows and follows a single request (reading a file) all the way down to the hardware and back.
The general picture
+--------------------------------------------------------------+
| Applications: browser, editor, database, your scripts | user mode
+--------------------------------------------------------------+
| Libraries & runtimes: C library, Win32 DLLs, .NET, Python | user mode
+==============================================================+ <- system calls
| Kernel: process & thread management, scheduler |
| memory manager, virtual file system, network stack | kernel mode
| device drivers |
+--------------------------------------------------------------+
| Hardware: CPU, MMU, RAM, disks, network card, timers |
+--------------------------------------------------------------+Two rules make this work:
- Only the kernel touches hardware. Applications run in user mode, where the CPU refuses privileged instructions (User Mode & Kernel Mode).
- The only way down is a system call. A program that wants anything from the hardware calls a library function, which issues a system call to enter the kernel.
Linux layers
user space bash python nginx systemd
\ | | /
glibc / musl (open, read, malloc, printf ...)
------------------------ syscall interface (~450 calls) ------------------------
kernel process mgmt | scheduler | memory mgmt | VFS | network stack | IPC
|
ext4 xfs btrfs proc tmpfs (file systems)
|
block layer, device drivers (often loadable modules)
------------------------------------------------------------------------------
hardware CPU RAM NVMe/SATA NIC USB- The kernel is one large program (
/boot/vmlinuz-...) running in a single address space. Drivers and file systems can be compiled in or loaded at run time as modules (Kernel Modules & Drivers). - The VFS (virtual file system) gives every file system the same interface, so
read()works the same on ext4, a USB stick,/procor a network share. - The C library (glibc on most distributions, musl on Alpine) wraps system calls in friendly functions.
Windows layers
user mode explorer.exe notepad.exe powershell.exe services (subsystem processes: csrss.exe)
| | |
Win32 subsystem DLLs: kernel32.dll / kernelbase.dll, user32.dll, gdi32.dll, advapi32.dll
|
ntdll.dll (native API: NtCreateFile, NtReadFile ...)
------------------------------ system service dispatcher ------------------------------
kernel mode Executive: object manager | process & thread mgr | memory mgr | I/O mgr
security reference monitor | configuration mgr (registry) | power mgr
Kernel (ntoskrnl.exe): scheduling, interrupts, synchronization
Drivers (.sys): file systems (NTFS), storage, network, graphics (win32k.sys)
HAL (hal.dll): hardware abstraction layer
---------------------------------------------------------------------------------------
hardware- Programs normally call documented Win32 functions (
CreateFileW,ReadFile). These end up inntdll.dll, the thin layer that actually enters the kernel through the native API (NtReadFile). ntoskrnl.exeholds both the low-level kernel (thread scheduling, interrupts) and the executive (managers for objects, processes, memory, I/O, security, the registry).- The HAL hides differences between motherboards, so the rest of the kernel stays portable.
- Window management and drawing live partly in the kernel (
win32k.sys), a performance choice dating from NT 4.0.
The Windows NT Architecture lesson goes deeper.
Following one read() call down the stack
Here is what happens when a Python program on Linux reads a file. The simulation passes a request through each layer; every layer does its small job and calls the next:
DISK = {812: b"hello from block 812"} # pretend disk: block number -> bytes
INODES = {"/home/ada/notes.txt": 812} # file system metadata: path -> block
def driver_read_block(block):
print(f" [driver] NVMe command: read block {block}")
return DISK[block]
def ext4_read(path):
block = INODES[path]
print(f" [ext4] inode lookup: {path} -> block {block}")
return driver_read_block(block)
def vfs_read(fd_table, fd, n):
path = fd_table[fd]
print(f" [VFS] fd {fd} refers to {path}, ask its file system")
return ext4_read(path)[:n]
def sys_read(fd_table, fd, n): # the system call entry point
print(f" [syscall] read(fd={fd}, count={n}) -> switch to kernel mode")
data = vfs_read(fd_table, fd, n)
print(" [syscall] copy bytes to user buffer, return to user mode")
return data
def libc_fread(fd_table, fd, n): # C library wrapper
print("[libc] fread() -> read() system call")
return sys_read(fd_table, fd, n)
fds = {3: "/home/ada/notes.txt"} # the process's open files
print("app got:", libc_fread(fds, 3, 10))[libc] fread() -> read() system call
[syscall] read(fd=3, count=10) -> switch to kernel mode
[VFS] fd 3 refers to /home/ada/notes.txt, ask its file system
[ext4] inode lookup: /home/ada/notes.txt -> block 812
[driver] NVMe command: read block 812
[syscall] copy bytes to user buffer, return to user mode
app got: b'hello from'
On Windows the same journey is ReadFile (kernel32) → NtReadFile (ntdll) → system service dispatcher → I/O
manager, which builds an I/O request packet (IRP) and passes it down a stack of drivers: the NTFS file system
driver, then volume and disk drivers, then the storage port driver.
Executable formats: ELF and PE
The kernel's loader has to understand program files. Linux uses ELF (Executable and Linkable Format); Windows uses PE (Portable Executable). The first bytes ("magic number") identify the format:
head -c 4 /bin/bash | od -An -c
printf 'MZ\x90\x00' > fake.exe
head -c 2 fake.exe; echo " <- every Windows .exe and .dll starts with MZ" 177 E L F
MZ <- every Windows .exe and .dll starts with MZ
177 E L F is byte 0x7F followed by the letters ELF. Windows executables start with MZ, the initials of Mark
Zbikowski, who designed the MS-DOS executable format; a PE file still begins with a tiny DOS header for compatibility.
| Linux | Windows | |
|---|---|---|
| Executable format | ELF | PE/COFF (.exe, .dll, .sys) |
| Lowest user-mode library | C library (libc.so.6) |
ntdll.dll |
| Stable interface for programs | system call numbers (never change) | Win32 API (system call numbers change between builds) |
| Kernel file | /boot/vmlinuz-<version> |
C:\Windows\System32\ntoskrnl.exe |
| Drivers | .ko modules |
.sys drivers |
The second row is an important difference: on Linux a program may issue system calls directly and they will keep
working across kernel versions. On Windows only ntdll.dll knows the current system call numbers, so programs must go
through the DLLs.
Learn more: Kernel Designs · System Calls · Linux Architecture
Test yourself
On Windows, which user-mode DLL contains the stubs that actually enter the kernel?
- user32.dll
- kernel32.dll
- ntdll.dll
- hal.dll
What is the job of Linux's VFS layer?
- Schedule threads
- Give all file systems one common interface
- Encrypt the disk
- Load ELF programs
Which magic bytes start a Linux executable?
- MZ
- 0x7F E L F
- #!
- PK