Shortcut to seniority

Home
Go to main page

Section level: Junior
A journey into the programming realm

Section level: Intermediate
The point of no return

Section level: Senior
Leaping into the unknown
Go to main page
A journey into the programming realm
The point of no return
Leaping into the unknown
An operating system is a software that manages all the hardware and other software on the computer.
Operating Systems use device drivers that are written by the hardware creators to communicate with such devices.
It sits between the applications and the hardware, using the hardware drivers as an interface between these two.
When an application wants to use the hardware, it delegates the task to the OS, and the OS speaks with the hardware.
The bootloader is the first thing that runs when the OS is started. Its job is to boot the system, and it is highly hardware (CPU, board) specific. The boot loader starts as soon as the computer / BIOS finished performing the startup and the hardware device checks, and it loads the OS kernel from the hard disk / boot device into the RAM.
The core of an Operating System is the kernel, which is one of the first things loaded when the system is started.
The kernel is responsible for allocating memory, dealing with the input and output of hardware devices, and converting the data processing instructions for the CPU.
A real time system is defined as a system in which each task has a strict deadline, mainly because the impact of missing such deadline can cause impactful, undesired consequences. Such systems guarantee that a task will happen in a given timeframe, and they are used when the timing of the processes is more important than the average performance.
Based on their tolerance to errors, RTOS systems are divided into Hard, Firm, and Soft RTOS:
Process scheduling is the activity of the process manager to replace the currently running process from the CPU with another one, based on a specific policy.
The Operating System maintains (at least) three scheduling queues:
Schedulers are software that decide which jobs to run on the CPU.
There are three types of schedulers: Long-term, short-term, and medium-term schedulers.
Long Term Scheduler: The long term scheduler decides which processes to run on the CPU – and load them into memory for CPU scheduling.
Short Term Scheduler (CPU Scheduler): The short term scheduler selects a process from the queue of processes that are ready to execute, and dispatches them to a CPU. Their job is to decide which process will be executed next.
Medium Term Scheduler: The medium term scheduler swaps two processes, by removing the current process from memory and making space for the other one.
A context switch is the mechanism of storing and restoring the state/context of a CPU so that a process can be resumed from the same point later on (similar to a Load/Save action), allowing multiple processes to share the same CPU.
When the scheduler swaps the processes, the state from the current process is stored, and the state from the next process is loaded (program counter, registers, etc).
Starvation is a problem that occurs when we use policies that schedule processes based on their priority.
If we have a process with a low priority, and we keep adding processes with higher priority in the queue, the low priority will not have a chance to run until we finish with all the other processes.
Aging is a technique used in association with policies that schedule processes based on their priority.
It gradually increases the priority of processes that wait in the system for a long time, so a process with low priority can reach normal/high priority after some time, having a chance to run on the CPU.
Memory management is the functionality that handles / manages the memory.
It does so by keeping track of each memory location, be it allocated to a process or not.
The process address space is the set of logical addresses that a process uses in its code (the addresses generated by the CPU during execution), and because it doesn’t physically exist anywhere, it’s also known as virtual address.
For 32-bit, address space is from 0 to 0x7FFFFFFF (2GB of memory).
The set of all logical addresses that are generated by a software is called the logical address space.
The set of all physical addresses that correspond to these logical addresses is called the physical address space, and they correspond to the addresses loaded into memory.
The user / developer will only deal with logical addresses.
There’s a hardware device called Memory Management Unit which is used for mapping virtual memory to physical memory, which uses a different table for each process, thus giving each process its own address space.
The physical address space is divided into a number of fixed-size blocks, called frames.
The logical address space (main memory) is also divided into a number of fixed-size blocks, called pages.
Frame size will always need to be the same as page size, due to the mapping to the frames in pages.
In a system with many processes active at the same time, their combined memory spaces will exceed the total RAM in the system, but not all the applications will use all of their memory space, therefore there’s no need to keep them all in RAM, but only the parts that are really used.
Paging splits up a big process into multiple smaller instructions and then stores them in RAM at different locations.
The Operating System can transfer unused data from RAM to main memory (in a file called swap file) in order to free the RAM for other applications.
It can call back the data anytime from the main memory into RAM to resume such functions. In Linux, this operation is known as swapping.
The Operating System keeps a frame-table which is a list of all the possible pages of physical memory and their state (free or not), and a page-table, which contains all the virtual pages and their mapping to physical frames.
The page table is a container that contains the page entries and page-related data, such as the protection bit, which says what protection there is for that page (read, write), or the modified bit (dirty flag), which is set if the page was modified or not.
If the page was not modified, the page can just be cleared (for security purposes) and reused.
Otherwise, the changes need to be commited to main memory first (written to a swap file before being reassigned).
When a process allocates new memory, the Operating System will find for it a free page of physical memory and insert the page into the virtual-physical mapping in the page table. When a process deallocates memory, the entry is removed and the frame is free for further allocation.
Let’s give an example of how paging works:
Each process is divided into pages of 1 kb each so that each page can be stored into one frame.
A page fault occurs when an application attempts to access data or code that is in its address space, but is not currently located in RAM.
When a page fault is raised, the following actions occurs:
We know that over time, the physical memory will become fragmented.
If we need to allocate 8kb of memory (two frames of 4kb), it would be problematic if those frames had to be contiguous.
Because the Operating system uses virtual addresses, it does not matter – the process assumes it has 8kb of contiguous memory, even if the pages point to frames that are very far apart.
Without virtual memory, each process will have complete access to all memory, so one process can overwrite the memory of another. With virtual memory, the Operating System will be an abstraction layer between each process and the memory access. If a process wants to access a virtual memory which is not in its page table, the Operating System will know that the process is doing something wrong, and it will generate an exception. That exception will further send a signal to the thread that tried to access the memory, with a default action to terminate it.
Each page has attributes that can be set, such as flagging it as read-only, or write-only. When the process will try to access the page, the Operating System also checks if the process has enough permissions before acting (eg. writing to a read only page).
If a process does something wrong, it can only crash itself and not the entire system.
The Operating System uses a memory mapping (system call mmap) which allows a page table to point to a file on disk, instead of pointing to a physical memory. That file can be accessed just like system RAM, and is used when we want to swap memory (move data from storage to RAM and the other way around).
When two or more processes want to share memory, they will execute a command that will make the Operating System to add an entry in the page table that points to the same physical frame, for all processes involved. Therefore, any change will be visible to all processes.
Copy on Write does the same thing, and sets to page to read-only. When a process want to write to a read-only page, the Operating System is notified, and knowing that the page is a copy on write page, it will make a new copy of the page and point the page from the page table to this new page.
Afterwards, the permissions can be changed from read-only to read-write.
First two were initially introduced in Chapter 5 – Problem solving
Priority queue (max heap): Elements are inserted based on their priority, thus the most important message is always the first one to be taken from the queue.