OSTEP intro
What happens when a program runs?
The processor fetches an instruction from memory, decodes it, and executes it. Then the processor moves on to the next instruction until the program completes.
The operating system is a software responsible for making it easy to run programs, allowing programs to share memory, enabling programs to interact with devices, and other fun stuff.
(This is often done through virtualization so the OS is sometimes referred to as a virtual machine)
OS is sometimes known as a resource manager (because it manages CPU, memory, disk, etc…).
The OS provides APIs that you can call (which we call standard library). A typical OS, exports a few hundred system calls that are available to applications (These system calls are usually wrapped by higher-level APIs in standard libraries).
Virtualizing CPU
1 |
|
1 | ./cpu A & ./cpu B & ./cpu C & ./cpu D & |
Turning a single CPU into a seemingly infinite number of CPUs is what we call virtualizing the CPU.
Virtualizing Memory
The model of physical memory presented by modern machines is very simple. Memory is just an array of bytes; to read memory, one must specify an address to be able to access the data stored there; to write (or update) memory, one must also specify the data to be written to the given address.
1 |
|
1 | ./mem & ./mem & |
We may see that each running program has allocated memory in the same address, and yet each seems to be updating the value at the address independently. It is as if each running program has its own private memory.
OS virtualizes memory. Each process accesses its own private virtual address space which the OS somehow maps onto the physical memory of the machine. A memory reference within one running program does not affect the address space of other processes (or the OS itself).
Concurrency
1 |
|
1 | ./threads 1000 |
But life is not simple, as it turns out. If we give higher values for loops, the Final Value not only gives a wrong value, but also different every time.
1 | ./threads 100000 |
Unfortunately, a key part of the program above, where the shared counter is incremented, takes three instructions: one to load the value of the counter from memory into a register, one to increment it, and one to store it back into memory. Because these instructions do not execute automatically, strange things can happen.
Persistence
1 |
|
The software in the operating system that usually manages the disk is called the file system. Unlike the abstractions provided by the OS for the CPU and memory, the OS does not create a private, virtualized disk for each application. Rather, it is assumed that often times, users will want to share information that is in files.
What the OS does to write to disk:
Figuring out where on disk this new data will reside, and then keeping track of it in carious structure the file system maintains. (Of course, there are more details.)
To handle the problems of system crashing during writes, most file systems incorporate some kind of intricate write protocol, such as journaling or copy-on-write.
Design Goals
Abstractions - In order to make the system convenient and easy to use.
Performance - Minimize overheads of OS.
Protection - We want to make sure that the malicious or accidental bad behavior of one program does not harm others. Isolation of processes from one another is key to protection.
Reliability - System may crash.
Energy-efficiency - green world.
Security - against malicious applications.
Mobility - To be able to run OS in mobile devices.