---
# System prepended metadata

title: 'Ch02: Operating System Structures'
tags: [OS]

---

# Operating System Structures

## Operating System Services

![](https://hackmd.io/_uploads/SJJ1531g6.png)

Providing functions that are helpful to users:

- **User Interface (UI)**
  - GUI (Graphical User Interface)
  - Touch-screen interface
  - CLI (Command Line Interface)
- **Program execution**
- **I/O operations**
- **File system manipulation**
- **Communications**
  - Shared memory
  - Messaging passing
- **Error detection**

---

- OS functions for ensuring the efficient operation of the system.
- System with multiple processes can gain efficiency by sharing the computer resources among the different processes.
  - **Resource allocation**
  - **Logging**
  - **Protection and security**

## System Calls

### Application Programming Service

- API (Application Programming Interface)
  - Application developers design program according to an API.
  - Example of standard API
    ![](https://hackmd.io/_uploads/BkxbwT1x6.png)
- System call interface
  ![](https://hackmd.io/_uploads/Hk3pUaJea.png)

Three methods are used to pass parameters to the OS:

- Put in the ==registers==
- Put the ==block, table in memory==
  - The address of the block is passed as a parameter in a register
  - （這張圖看不懂正常）
  ![](https://hackmd.io/_uploads/Sybhdpkl6.png)
- Put in a ==stack==
  - Push
  - Pop

### Types of System Calls

System calls can be grouped into six categories:

- **Process control**
  - create process, terminate process
  - load, execute
  - get process attributes, set process attributes
  - wait event, signal event
  - allocate and free memory
- **File manipulation**
  - create file, delete file
  - open, close
  - read, write, reposition
  - get and set file attributes
- **Device manipulation**
  - request device, release device
  - read, write, reposition
  - get device attributes, set device attributes
  - logically attach or detach devices
- **Information maintenance**
  - get time or date, set time or date
  - get system data, set system data
  - get process, file, or device attributes
  - set process, file, or device attributes
- **Communications**
  - create, delete communication connection
  - send, receive messages
  - transfer status information
  - attach and detach remote devices
  - Message-passing model
  - Shared memory model
- **Protection**
  - Get file permissions
  - Set file permissions

## System Services (System Utilities)

- Provide a convenient environment for program development and execution.
- Categories
  - File management
  - Status information
  - File modification
  - Programming language support
  - Program loading execution
  - Communications
  - Background services

## Linkers and Loaders

![](https://hackmd.io/_uploads/H1154Ryx6.png)

## Why Applications are Operating System Specific

An application can run on multiple OS in one of three ways:

- The application can be written in an interpreted language that has an **interpreter** available for multiple OSs.
- The application can be written in a language that includes a **virtual machine** containing the running application.
- The application developer can use a standard language or API in which the **compiler** generates binaries in a machine-specific and OS-specific language.

## Operating System Design and Implementation

### Design Goals

- User goals
  - convenient to use, easy to learn, reliable, safe, and fast
- System goals
  - easy to design, implement, and maintain, as well as flexible, reliable, error-free, and efficient

### Mechanisms and Policies

（考點？）

- Mechanisms 機制
  - determine ==how== to do something
- Policies 政策
  - determine ==what== will be done

### Implementation

- In assembly language
- In C
- In C++

## Operating System Structure

<!-- 下禮拜才教 -->

### Monolithic Structure

Place all of the functionality of the kernel into a single, static binary file that runs in a singe address space.

- UNIX
  ![](https://hackmd.io/_uploads/SJ_vygtxa.png)
- Linux
  ![](https://hackmd.io/_uploads/SJ-5keYxa.png)

### Layer Approach

![](https://hackmd.io/_uploads/SJ0AA4SMa.png =40%x)

- The OS is broken into a number of layers (Fig 2.14)
  - The bottom layer (layer 0) is the hardware
  - The highest layer (layer N) is the user interface
- The layers are selected such that each uses functions (operations) and services of only lower-level layers.
  - Simplifying debugging and system verification

### Microkernal

![](https://hackmd.io/_uploads/ryvtCNSG6.png =70%x)


(Fig 2.15)

Removing all **nonessential** components from the kernel and implementing them as system and user-level programs.

- Benefits
  - Easier to extend
  - Easier to port
  - More secure
  - More reliable
- Disadvantage
  - Performance can suffer from due to increased system-function overhead

### Modules

Loadable kernel modules:

- The kernel has a set of core components and links in additional services via modules, either at boot time or during run time.
- The kernel provides core services while other services are implemented ==dynamically==.
- Linking services dynamically.

### Hybrid Systems

- Combining different structures, resulting in hybrid systems.
  - Address performance, security, usability issues.

<!-- 先講的 -->
## Building and Booting an Operating System

### Operating System Generation

- System generation
  - The system must then be configured or generated for each specific computer site

### System Boot

- Booting
  - The procedure of starting a computer by loading a kernel.
- The boot process proceeds as follows
  1. A small piece of code known as the **bootstrap program** or **bootstrap loader** locates the kernel.
  2. The kernel is loaded into memory and started.
  3. The kernel initializes hardware.
  4. The root file system is mounted.

## Operating System Debugging

- Debugging
  - Finding and fixing errors in a system, both in hardware and software.
- Performance tuning
  - Improving performance by removing processing bottlenecks.

### Failure Analysis

- Log file
- Core dump
- Crash dump

### Performance Monitoring and Tuning

![](https://hackmd.io/_uploads/HJXomOszT.png)

### Tracing

### BCC

![](https://hackmd.io/_uploads/Hka67doGa.png)

[BPF Compiler Collection](https://github.com/iovisor/bcc)
