zyros

Roughly, operating systems

Created 575 words 3 min read

Operating systems are old, incredibly detailed, and it's possible to write for a long time about them. Operating Systems: Three Easy Pieces, also known as OSTEP, is typically the book people point you to if you want to learn about them. It breaks operating systems into three core parts: virtualisation, concurrency and persistence. Security also has a chapter, but it's shorter than the others.

The core problem the operating system solves is it enables your computer to run multiple applications. Before operating systems, your computer was limited to running a single task at a time, and the OS provides resource sharing mechanisms so that multiple programs can run at the same time.

An operating system breaks things into programs, processes, and threads. Programs are the static executable in memory. If you load this into your main memory, it can become a process. A process is an executing program, and a thread is a 'lightweight' process, or single execution stream in a process. One process can have multiple threads.

The process provides the illusion that the running program has the CPU all to itself. It maintains a program counter and stack pointer for where it is busy executing.

Disclaimer, I'm not that knowledgeable about operating systems.

Virtualisation

Demand paging

Back when computers were just hardware, a big challenge was how can application memory scale effectively. Processes need to be able to find their relevant data, but also spill down to disk. Virtualisation was made to solve this by providing isolation between programs and allowing memory to act larger than it physically is. Within the CPU, it uses a virtual address to understand where things are in memory and this goes down to a series of lookups for physical address in memory. This is called demand paging, or virtual memory.

This starts at the CPU core asking for data at a virtual address. In modern systems, it looks this up in the L1 cache and Translation Lookaside Buffer (TLB) at the same time. The L1 cache is a guess, and needs to get validated with a tag from the TLB. The TLB gave a physical address, and you can get the tag by using the top part of the address, known as VIPT (virtually indexed, physically tagged). These are compared, and if it is the correct data, the data is returned from the L1 cache.

When this fails, it needs to check whether the TLB hit at all. The TLB translated the virtual address into a physical address, and we can find where it is in our other storage using this. We check the L2 cache, L3 cache, then finally the RAM.

If the TLB failed to get an address, we'll have to go check the page table. If this works, we should be able to find our data in the RAM from the physical address. If it fails, we're going to hit a page fault, and load the data into the ram, update the page table, and restart the instruction so the TLB can load the new address.

If the RAM becomes full, live executing memory will be persisted to disk in the swap, and this is also known as context switching.

Sometimes there are exercises about calculate the cost of a page fault, and it looks exactly what you'd expect. Calculate how much data you're fetching, how many pages this is, assume some latency to fetch it, and compare to different access levels.

Concurrency

Persistence