---
type: theoretical
backlinks:
---

## Operating System
- Manages hardware and software
- Resource allocator
	- Manages and routes resources and requests
- Control program
	- Prevents errors and improper use

## User v. System

### User

```mermaid
graph TD;

	A["user"] <--> B["application programs"]
	B <--> C["operating system"]
	C <--> D["computer hardware"]

```



### System
An operating system is a control program


## Major OS Components


| Name                     | Description                                        |
| ------------------------ | -------------------------------------------------- |
| Kernel                   | Core part of the OS; manages CPU, memory, I/O      |
| Process manager          | Handles multitasking, scheduling, and execution    |
| Memory Manager           | Allocates/deallocates RAM, manages virtual memory. |
| File System              | Organizes, stores, and secures files.              |
| Device Manager           | Interfaces with hardware peripherals               |
| User Interface (CLI/GUI) | Allows users to interact with the OS               |


## Management of resources

### CPU
- A program can do nothing unless its instructions are executed by a CPU
- An executing program becomes a **process**
- The process needs resources
- All processes can potentially be executed concurrently by multiplexing on one CPU core or in parallel across multiple


### Memory
- For a program to be executed, it must be loaded into memory.
- To improve the CPU utilization, several programs are kept in memory.

OS should:
- Know which parts of memory are currently being used and which process is using htem
- Allocate and deallocate memory accordingly
- Decide which processes and data moves in and out of memory

### Cache
- Cache operates at different levels (L1, L2, L3) and is managed primarily by hardware (CPU cache controllers), not the operating system.
- **HOWEVER**, the OS can influence this by
	- Managing process scheduling which affects cache locality[^1]
	- Optimizing memory access patterns
	- Supporting cache-aware algorithms[^2]


### File System

- The operating system abstracts from the physical properties of its storage devices to define a logical storage unit, the file

### Mass Storage
The operating system is responsible for:
-  Mounting and unmounting
- Free-space management
- Storage allocation
- Disk scheduling
- Partitioning
- Protection

### I/O

The I/O subsystem consists of several components:
- A memory-management component that includes buffering, caching, and spooling
- A general device-driver interface
-  Drivers for specific hardware devices




## User and Operating System Interface
![](Pasted%20image%2020250204103541.png)


## Command Interpreters
Special programs that run when a user first logs on. They read and interpret user commands and execute programs.

On Linux, the command interpreter is called a **shell**. The most common shell is the **Bourne Again Shell (bash)**

## System calls
- System calls are the interface between the user and the operating system
- They are the only way a user program can request a service from the operating system
- They are typically written in a high-level language (C) and are accessed via a library

E.g.

```c
#include <stdio.h>
#include <unistd.h>

int main() {
	write(1, "Hello, World!\n", 14); // 1 is the file descriptor for stdout, 14 is the number of bytes to write
	return 0;
}
```

![example syscall](assets/image.png)


## APIs in the OS

- APIs are a set of functions that allow the programmer to interact with the OS
- They are typically written in a high-level language (C) and are accessed via a library
- They are used to interact with the OS and its services


i.e. Under the hood, widely used libraries like `stdlib.h` and `stdio.h` are just APIs that interact with the OS



## Performance Optimization
A computer system executes a program by:
- Loading the program into memory
- FDE cycle (Fetch, Decode, Execute)
- Repeat until the program is done
- The OS must manage resources to ensure that the program runs efficiently


### Instruction Types
- CPU-bound: spends most of its time executing instructions
- I/O-bound: spends most of its time waiting for I/O operations to complete

Keyword - **waiting**, hence I/O operations are very slow.

### How do we sped it up?

- Replace slow I/O devices
- Perform I/O operations independently of the CPU
- Multiprogramming/multitasking


#### Spooling
- Simultaneous Peripheral Operations On-Line
- A technique that uses a buffer to hold data for devices that are not currently in use

```mermaid
graph TD;

	A["User"] --> B["Spooling"]
	B --> C["Printer"]
	B --> D["Disk"]
```

#### Multiprogramming
- The OS keeps several jobs in memory simultaneously
- Needed for efficiency
- Single users cannot keep the CPU and I/O devices busy at all times
- Scheduling is key
- 
```mermaid
graph TD;

	A["User"] --> B["Multiprogramming"]
	B --> C["Job 1"]
	B --> D["Job 2"]
```

#### Multitasking/Timesharing
- The OS switches between jobs so quickly that it appears as if they are running simultaneously
- If several jobs are ready to run at the same time, the OS must decide which one to run

```mermaid
graph TD;

	A["User"] --> B["Multitasking"]
	B --> C["Job 1"]
	B --> D["Job 2"]
```



---

[^1]: is the tendency of a processor to access the same set of memory locations repetitively over a short period of time
[^2]: https://stackoverflow.com/questions/473137/a-simple-example-of-a-cache-aware-algorithm