OS Processes
A lesson on how operating systems run and manage processes, share CPU time, protect kernel services, and regain control of execution.
From to Shared CPU
From to Shared CPU
An is a stored file containing a program’s machine-code instructions and data. By itself, it is inert: it can be copied or stored like other files. Running it creates a , a particular, currently running instance of that program on a specific computer at a specific time. For example, running ./myprog starts a . Running the same again can create another at the same time. The processes share the program’s instructions and constant data, but have separate heaps and stacks, so their variables can hold different values.
For the single-core computer considered here, multiple processes do not execute instructions simultaneously on that one core. Instead, the processor uses : it runs one for a while, then switches to another. The kernel—the operating system’s managing code—also runs between slices. During its slice, each gets to use the machine’s registers, giving it the illusion that it owns the processor. The OS saves and restores the program counter (PC), so the appears to continue through its instructions across separate slices.
Virtual memory provides a related illusion: each appears to own the entire -byte memory address space. Its implementation is deferred here. These points describe a single CPU core; multiprocessor and multicore systems are outside this discussion.
State, Switching, and Scheduling
State, Switching, and Scheduling
The kernel tracks processes in a list, with one per . A PCB holds information needed to manage and resume its . This includes identifying information, such as the launching user and path; saved register state, including the PC; and scheduling status. Details vary among operating systems.
Consider the source example of running ./myprog. The OS creates a PCB in the initializing state and adds it to the list. It loads the ’s instructions into the text segment, sets the saved PC to the first instruction in main, allocates a stack, and sets up main’s stack frame and arguments. Once setup is complete, the becomes ready: it can run when CPU time is available. When selected, it becomes running.
A running may be switched out and become ready again. It may instead request kernel work, such as disk or network I/O; then it becomes blocked until that request is satisfied, after which it becomes ready. When it finishes—for example, by returning from main or calling exit—the OS moves its PCB to the terminated state.
A preserves the point where one stopped and restores the point where another should continue. The kernel:
Saves the current ’s CPU registers, including the PC, in its PCB.
Changes its status from running to ready.
Selects another ready .
Changes that ’s status from ready to running.
Restores its saved registers from its PCB.
Resumes at its saved PC.
The source gives rough contextual estimates—not fixed timings—of time windows around 1–5 ms and a around a microsecond, or roughly a thousand clock cycles. Since switching has a cost, the OS tries to limit unnecessary switches while still dividing CPU time fairly.
The scheduler chooses which ready to run at a switch. Its policy must balance competing goals: fairness, or roughly equal shares of CPU cycles; turnaround time, the time from a task’s start to its finish; and responsiveness, giving processes a chance to react quickly to events such as mouse movement or arriving network packets. These are competing design goals; no particular is prescribed here.
Kernel Boundaries and Privilege
Kernel Boundaries and Privilege
is where operating-system code performs work such as maintaining PCBs and choosing processes. is where code runs. The distinction is not just about which code does what: it also limits access. The kernel needs unrestricted access to memory and I/O peripherals, including the ability to read and write any ’s memory. A user should access only its own memory, so it must ask the kernel to perform operations such as I/O or communication with other processes.
Software alone cannot reliably enforce this boundary: a program might be mistaken or malicious. The processor’s instruction-set architecture (ISA) provides hardware-enforced privilege levels, also called modes or protection rings. Capabilities such as privileged instructions are available only in appropriate modes, and transitions to higher privilege are carefully controlled.
RISC-V is one example, not a template for every ISA. Its kernel runs in supervisor mode, with access to privileged instructions and registers; user processes run in the less-privileged user mode. When handing execution to user code, the kernel can explicitly instruct the CPU to lower its privilege before jumping to that code. This direction of transition gives up privileges rather than acquiring them. In RISC-V, the described mechanisms include writing to a privileged control status register (CSR) or using sret.
Traps, Interrupts, and Regaining Control
Traps, Interrupts, and Regaining Control
A sometimes needs the kernel to do something user code cannot do directly, such as allocate memory, spawn a , write a file, or print to the screen. The kernel may also need to handle a error, such as accessing unallocated memory or executing an illegal instruction. A provides a user-to-kernel transition triggered by an instruction in . For an explicit service request on RISC-V, the instruction is ecall; this request mechanism is known as a system call.
One way to share the CPU is cooperative multitasking. Processes explicitly give up control, typically by calling . At a , the kernel takes over and chooses what runs next; it may choose the same again. This approach depends on processes yielding. Programs may need many calls, and a runaway that does not can monopolize the CPU. It may also delay the kernel’s response to external input, and the —not the kernel—decides when to .
Preemptive multitasking lets the kernel regain control without waiting for the running to execute a instruction. It relies on hardware interrupts, which respond to external events rather than instructions. The two main categories described are I/O events, such as input arriving from a peripheral, and timer events, which can occur periodically every few milliseconds. Conceptually, the processor checks for an before issuing a new instruction; if one is signaled, it transitions into the kernel. This gives the kernel a way to respond to events and take control while a is running.
For this material, traps and interrupts are mutually exclusive: a responds to instruction execution, while an responds to a non-instruction signal. Both transition from to . Terminology varies outside this course, and some people use the terms as synonyms.
: A processor mode that determines which capabilities, such as particular instructions, are available.