操作系统 Operating System

时间:2021-10-08
本文章向大家介绍操作系统 Operating System,主要包括操作系统 Operating System使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Overview

This is really a hard-core course.

The operating system manages the computer's hardware and software resources.

It mainly includes the following functions:

  • Process Management:
  • Memory Management
  • File Management

一、Process Management

A process is a collection of running programs and related resources.

When you open a software, one or more processes are created.

A process has multiple threads, which can be regarded as lightweight process.

Threads of the process share the resources of the process

1. Process Scheduling

The main task of process management is to schedule different processes.

1) Three Levels of Scheduling

The CPU Scheduling is complicated.

It has three levels:

  • High Level Scheduling: The task of it is to select jobs submitted by the user, and allocate resources and convert the jobs into processes.

  • Medium Level Scheduling: The task of it is to control the number of processes in memory.

    Sometimes we need to suspend some processes to make room for other processes.

    Suspend means swap process out of memory.

  • Low Level Scheduling: Process scheduling. It is responsible for scheduling all processes in the system.

2) Three States of Process

We focus on process scheduling.

Simply put, a process has three states: Running, Ready, and Blocked.

  • Running: The Process is running in CPU
  • Ready: Ready to run, queuing
  • Blocked: Not ready to run, waiting for assignment.

3) Scheduling Algorithms

So, how do we actually schedule the process?

For that we have different strategies.

  • First Come First Serve: Always serve the first process.

    But when the first process requires a lot of time, the following processes needs to wait a long time. We call this phenomenon hunger.

  • Shortest Job First: Serve the short jobs first. But when there are lots of short processes, long ones may be hungry.

  • Prioritized Scheduling: Each process has its own priority. The CPU always serves high-priority processes.

  • Round-Robin/Time Slices: This is the most fair algorithm. We call a period of time, such as 1 ms, a time slice.

    Each process gets a number of time slices per round.

    When the process runs out of time slices, it has to be swapped to ready queue.

    When the time slice is too long, it just degenerates into FCFS algorithm.

2. Deadlock

During the scheduling process, some unexpected situations may occur between processes with overlapping resource requirements. Such as deadlock.

For example: Process 1 holds the Resource 1, and is waiting for Resource 2, which is hold by Process 1. Process 2 is waiting for Resource 1 which is hold by Process 1.

They are all waiting for each other to make a compromise., which is impossible.

We use Banker Algorithm to prevent deadlock.

For example,

  • when you take a loan from a bank
  • the bank needs to determine whether you are able to repay the loan
  • Meanwhile, the bank also needs to consider, after lending the money to you, whether it can meet the needs of other lenders

In scheduling, loan is resources, and bank is the OS.

二、Memory Management

The task of memory management:

  • optimize memory usage
  • and expand memory through virtual memory technology

1. Page Memory Allocation

We use Page Memory Allocation to optimize memory usage.

Basic Idea of PMA:

  • Divide the logical address space of the process into Pages with same size. Like 128 KB.

  • Divide the physical memory space into Page Frames with same size of page.

Therefore, logical address consists of page number and page offset.

We use Page Table to store the page number and its corresponding page frame. So that we can immediately find the physical data.

2. Demand Paging Mechanism

We know that when the process is running, it is in the memory.

But sometimes we don’t need all the data of the process.

So, in order to save memory, the demand paging mechanism is introduced.

  • We see disk as a part of memory, and then store part of data we currently need in memory and the rest in the disk.
  • When we need data in the hard disk, we swap the page into memory
  • At the same time, unneeded page is swapped out of memory.

1) Process of demand paging

This function relies on hardware MMU. Its main function is address translation.

graph TD start(logical address)-->op1(decompose it into page number,offset) op1-->cond{is in TLB?} cond-->|Yes|op2(get page frame) cond-->|No|cond2{is in page table?} op2-->op3(compose frame offset into physical address) op3-->finish(end) cond2-->|Yes|op22(get page frame) op22-->op4(load entry into TLB) op22-->op3 cond2-->|No|op5(page fault)

2) Page Fault Processing

When can't find the data we want in memory, there is a page fault.

This is how we deal with it:

graph TD start(suspend process)-->op1(query and get physical address in disk) op1-->cond{is there free page frame?} cond-->|Yes|op2(swap page into frame, update tables) op2-->finish(wake up process) cond-->|No|op3(eliminate page in frame according to algorithms) op3-->cond2{is page changed?} cond2-->|Yes|op4(write it back to disk) cond2-->|No|op2 op4-->op2

3) Elimination Algorithms

  • Fist In First Out: Always eliminate pages that came first.
  • Least Recently Used: Always eliminate pages least recently used.

三、File Management

The file management system is convenient for users to store and view files.

It mainly includes:

  • Directory Hierarchy
  • File Space Management

1. Directory Hierarchy

Commonly used hierarchy is tree.

2. Disk Space Management

We need to manage the storage space of disk.

Here are some methods:

1) Bitmap

Bitmap is like a matrix. Each bit stands for one physical block of disk.

1 means the block is occupied. 0 means free.

2) Free Block Table

We store informations of free blocks in a table, like its starting address and size.

When we need to allocate a file, we just scan the table and find the appropriate block.

3) Free Block Chain

All free blocks are chained together with a pointer to the first free block, and each free block contains a pointer to the next free block.

四、Curriculum Design

I wrote a program in Java to simulate a computer and OS, which implements the entire process of demand paging virtual memory management and three levels of scheduling.

1. Hardware

Hardware classes includes Clock, CPU, MMU, Hard Disk and Memory.

I use the free linked list method to manage disk space.

2. Software

For process scheduling, the scheduling algorithm I chose is Round-Robin with priority.

This OS also implements banker algorithm and deadlock management.

I use the LRU algorithm to implement page fault handling.

3. UI

I used Java Swing to design UI. User can create jobs and view the usage of memory and hard disk.

At the same time, there are two windows that display the scheduling details to the user in real time.

For example, the working process of the MMU, the pages allocated to the process.

原文地址:https://www.cnblogs.com/danielwong2021/p/15380770.html