Introduction to Operating Systems
An operating system is the software that sits between programs and hardware. It shares the CPU, memory, files and devices among many programs, keeps them from harming each other, and gives them one simple way to use any hardware.
What Is an Operating System
An operating system is the software layer between your programs and the computer's hardware. A browser does not drive the network card or the disk itself. It asks the operating system, and the OS does the work and hands back the result.
That layer does two jobs. It turns awkward hardware into simple ideas, such as files instead of disk sectors and processes instead of raw CPU time. And it shares the hardware fairly and safely among many programs at once. Windows, macOS, Linux, Android and iOS are all operating systems.
Because of the OS, the same open() call reads a file whether it lives on an SSD, a USB stick or a network share. The program never needs to know which.
- Without an OS, every program would need its own code for every device
- With an OS, programs share one machine without seeing or breaking each other
Goals of an Operating System
Textbooks give an OS two main goals. Convenience: make the computer easy to program and use. Efficiency: keep the CPU, memory and devices busy with useful work instead of idle. A third, protection, became essential once many users and programs shared one machine.
The goals pull against each other. Checking every request for safety costs time, and the simplest interface is rarely the fastest. Much of OS design is choosing where to strike that balance.
- Convenience: hide hardware details behind simple calls
- Efficiency: keep expensive hardware doing work
- Protection: stop one program or user harming another
Kernel, Shell and User Mode
The kernel is the core of the OS. It runs with full hardware privilege in kernel mode and is always in memory. Applications, including the shell you type commands into, run in user mode, where the CPU blocks direct hardware access.
When a program needs something only the kernel can do, it makes a system call: the CPU switches into kernel mode, the kernel does the work, and control returns to the program. The details are in kernel and system calls. Device drivers are kernel code that turns a general request into commands for one specific device.
- Kernel: privileged core, manages hardware
- Shell: the user's interface, a GUI desktop or a command line
- System call: the only door from user mode into the kernel
Terms, operations, and practical uses
System layers
- ApplicationA user program that requests services instead of controlling hardware directly.
- KernelThe privileged core that manages processors, memory, files, and devices.
- DriverKernel-side software that translates a general request into commands for one device.
Managed resources
- CPU timeScheduled among runnable threads.
- MemoryAllocated, translated, protected, and reclaimed.
- Persistent storageNamed through files and directories rather than raw device sectors.
- Input and outputQueued and completed through drivers and interrupts.
Why the OS exists
- ConvenienceApplications use consistent services across different hardware.
- IsolationOne process cannot freely read or overwrite another process's memory.
- CoordinationShared resources are assigned without requiring every application to negotiate directly.
One command traced through the operating system
import os
# fork(): ask the process manager for a new process
pid = os.fork()
if pid == 0:
# exec(): the file system finds the program, the memory manager maps it in
os.execv("/bin/echo", ["echo", "hello from the child"])
else:
# the parent waits until the process manager reports the exit
_, status = os.waitpid(pid, 0)
print("exit status", os.WEXITSTATUS(status))#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
int main() {
pid_t pid = fork(); // process manager: new PCB
if (pid == 0) {
// file system finds the program, memory manager maps it in
execl("/bin/echo", "echo", "hello from the child", (char*)nullptr);
_exit(127); // only reached if exec failed
}
int status = 0;
waitpid(pid, &status, 0); // block until the child exits
std::cout << "exit status " << WEXITSTATUS(status) << '\n';
}class Main {
public static void main(String[] args) throws Exception {
// the JVM asks the OS for the same fork + exec underneath
Process p = new ProcessBuilder("/bin/echo", "hello from the child").inheritIO().start();
int status = p.waitFor(); // process manager reports the exit
System.out.println("exit status " + status);
}
}Step through it
Running on echo hello, typed into a shell
Functions of Operating System
Exam questions on the functions of an operating system usually expect six. Each one manages a resource that many programs want at once. The services programs see, such as program execution and I/O operations, are covered in operating system services.
- Process management: create, schedule and end processes, and choose who gets the CPU next (processes and threads, CPU scheduling)
- Memory management: give each process its own address space, load pages when needed, and free them on exit (memory management)
- File management: organise data into files and directories and enforce permissions (file systems)
- Device management: queue I/O requests and complete them through drivers and interrupts (device drivers)
- Protection and security: log users in, check permissions and keep processes apart (protection and security)
- User interface: a graphical desktop or a command-line shell
What Happens When You Run a Program
- The shell calls fork(): the process manager creates a new process with its own PCB
- The child calls exec(): the file system finds the program and checks permission
- The memory manager maps the program into a fresh address space without loading it
- The first instruction causes a page fault, and that page is read from disk
- The scheduler gives the process the CPU; the shell waits
- Output goes through a write() system call and a driver to the screen
- On exit(), the kernel frees memory and files and wakes the shell
One short command uses nearly every function above. The tracer follows echo hello through each layer, then shows two cases the OS refuses: a file without execute permission, and a program touching memory it does not own.
- The program asks; the kernel acts; the hardware never sees the program directly
A Short History
The first computers had no OS: one programmer at a time booked the machine and loaded a program by hand. Batch systems then queued jobs to cut the idle time between them. Multiprogramming kept several jobs in memory so the CPU could work while one waited for I/O. Time-sharing added a timer to switch between users quickly, which gave us interactive terminals and Unix.
Personal computers brought graphical interfaces, networks brought shared servers, and phones brought battery-aware mobile systems. Each step added a new kind of OS rather than replacing the old ones; they are compared in types of operating systems.
- No OS → batch → multiprogramming → time-sharing → personal → mobile