Basics
Operating Systems (OS) are fundamental software that manage computer hardware and software resources, providing common services for computer programs.
- Act as intermediary between hardware and software
- Handle tasks like memory management, process scheduling, file system management, and device control
- Crucial role in system security, performance optimization, and providing a consistent user experience across diverse computing devices and environments
- Popular OS: Linux, Windows, VMS, AIX, macOS
Machine Instructions
- The instructions that CPUs execute are binary data, usually 1-2 bytes that represent an instruction (opcode), followed by the data needed to execute it
- Machine code consists of a series of these binary instructions
- Assembly: A helpful syntax for reading and writing machine code, making it easier for humans to understand than raw bits
Random Access Memory (RAM)
- Computer’s main memory bank, storing data for running programs
- The CPU fetches machine code directly from RAM, so code cannot run unless loaded into RAM
- Fetch-Execution Cycle: The CPU uses an instruction pointer to fetch the next instruction. After executing each instruction, it moves the pointer and repeats.
- The pointer is stored in a register (small storage bucket that is extremely fast for the CPU to read and write to)
- Each CPU architecture has a fixed set of registers (used from storing temporary values during computations to configuring the processor)
- Some instructions can tell the instruction pointer to jump somewhere else instead, or jump different places depending on a certain condition; this makes reusable code and conditional logic possible.
Kernel
- The core of OS, it has near full access to computer’s memory, peripherals, and other resources, and is in charge of running software installed on computer (userland programs)
- When we boot up the computer, the instruction pointer starts at the kernel
- Linux is just a kernel and needs plenty of userland software like shells and display servers to be usable
- Kernel in macOS is called XNU and Unix-like, and the modern windows kernel is called the NT kernel
Privilege Level
- The mode (privilege level or ring) refers to the control level given to a processor
Kernel Mode | User Mode |
---|---|
CPU can execute any instruction and access any memory | Limited instructions, restricted memory and I/O access |
Kernel and drivers run in this mode | Applications run in this mode |
Processors start in this mode and switch to user mode before executing programs | N/A |
- The current privilege level (CPL) is stored in the code segment (CS) register
- Ring 0: Kernel mode
- Ring 3: User mode
- Rings 1 & 2: Rarely used, intended for drivers in some OS
- System Calls: Functions like
open
,read
,fork
, andexit
that programs use to request services from the OS kernel
Processes
- Processes are OS abstractions, not something CPUs natively understand or track