Computer Operating Systems Final Exam

Note: This article is based on "Computer Operating System" (MOOC Edition) - of course, the same goal through different routes, all the key content that may be assessed in the exam will be covered, if there is any lack, please add it.

Chapter 1 Introduction

The purpose and role of the operating system

Goal: Target
Function:
insert image description here

operating system development

1. Single-channel batch processing system: process one by one
2. Multi-channel batch processing system: put it in external storage, select several jobs according to the algorithm to share cpu and system resources - at this time, the model of the operating system has begun to take shape
3. Time-sharing System: multi-channel, independence, timeliness, interactivity
4. Real-time system: multi-channel, independence, timeliness, interactivity, reliability
real-time task type5. Microcomputer operating system: system configured on the microcomputer
6. Embedded Operating system: small system kernel, streamlined system, high real-time performance, and configurability
7. Network operating system: hardware independence, interface consistency, resource transparency, system reliability, and execution parallelism
8. Distributed operating system: Distributed, transparent, identical, and global——Hongmeng OS

The main driving force for the development of the operating system

  1. Improve the utilization of computer system resources
  2. user-friendly
  3. Devices are constantly updated and iterated
  4. Computer Architecture Evolves
  5. Constantly put forward new application requirements

Basic features of an operating system

concurrent, shared, virtual, asynchronous

  1. Concurrency: Refers to the occurrence of two or more events within the same time interval

  2. Sharing:
    Mutually exclusive sharing - there are not many resources, but many people want them, and each of them has to monopolize the resources
    Simultaneous sharing - many people want them, but the resources can be shared

  3. Virtual:
    time-division multiplexing technology
    uses the idle time of the processor to run other programs to improve the utilization of the processor

    • Virtual processor technology: use multi-programming technology to create at least one process for each program, so that multiple programs can be executed concurrently
    • Virtual device technology: Virtualize one physical I/O device into multiple logical I/O devices, allowing multiple users to occupy one logical I/O device

    Space division multiplexing technology
    uses the idle time of the memory to store other programs to improve memory utilization

  4. Asynchronous:
    Because of limited resources, the execution of the process cannot be completed in one go, but will stop and go, so the total time required for each program to run is unknown

operating system function

This picture is for reference
The main functions of the operating system

Chapter 2 Process Description and Control

concurrent execution of programs

Only programs that do not have a predecessor relationship can be executed concurrently, otherwise they cannot be executed concurrently - concurrent execution is to make the utilization of computer resources higher

  • Intermittent: Because of concurrent execution and shared resources, it involves resource contention when using critical resources, and suspend execution if resources are not obtained
  • loss of closure
  • Irreproducibility Loss of closure leads to loss of reproducibility

The basic concept of the process, the structure of the process entity (three parts), the basic characteristics

Concept: A process is the execution process of a program, an independent unit for resource allocation and scheduling by the system Structure
: Program segment, related data, PCB
Basic features:

  1. Dynamics: The essence of a process is the execution process of the program, which is always dynamically changing—the process is generated by creation, executed by scheduling, and dies by cancellation
  2. Concurrency: Refers to multiple processes coexisting in memory and executing at the same time
  3. Independence - Definition of Reference Process
  4. Asynchronicity: Processes run asynchronously, that is, move forward at independent and unpredictable speeds

PCB - process management table

  1. Concept:
    The control table managed by OS can be divided into: memory table, device table, file table and process table (for process management) - PCB is the process table. The role of PCB is to
    make a program that cannot run independently in a multi-program environment (including data), become a basic unit that can run independently, that is, a process that can execute concurrently with other programs
  2. The role of PCB:
  • Logo as an independently functioning base unit
  • Realize intermittent operation mode
  • Provide information needed for process management
  • Provide information needed for process scheduling
  • Synchronize and communicate with other information
  1. Information in the PCB
    Information in the PCB
  2. PCB organization
  • linear way
  • link method
  • index method

The three basic states of the process and the reasons for state transitions (emphasis)

  1. Ready state (ready): The process has been allocated all resources except the CPU, and enters the ready queue to wait for scheduling
  2. Execution status (running): get the CPU after being scheduled
  3. Blocked state (block): When the process triggers an event, it cannot continue to execute, that is, it is blocked. The OS will assign the processor to other processes, and let the blocked process enter the blocking queue - because the blocking caused by different reasons will be assigned to different blocking queues to improve efficiency
  4. Create state: first, the process applies for a blank PCB, and fills in the PCB with information for controlling and managing the process; then the process allocates the resources required for runtime; finally, converts the process state into a ready state and inserts it into the ready queue middle
  5. Termination state: first wait for the OS to deal with the aftermath; then clear the PCB of the process and return the PCB space to the OS
    process state transition reason

Suspend and introduce process state transitions after suspend

Suspend operations were introduced to:

  • end user needs
  • needs of the parent process
  • The need for load regulation
  • The needs of the OS
    State transition after the introduction of the suspend operation - the introduction of the suspend primitive Suspend and the activation primitive Active
    Synthesized state transition diagram

process creation

  1. Apply for a blank PCB - the creation of a process needs to allocate resources and manage information, so it is necessary to create a PCB first to facilitate the system to control it
  2. Allocate the resources needed for the new process to run - including various physical resources and logical resources
  3. Initialize the PCB
    • Initialize flag information
    • Initialize processor state information
    • Initialize processor control information
  4. If the process ready queue can accept the new process, insert the new process into the ready queue

process termination

Invokes the process termination primitive, which does the following:

  1. According to the identifier of the terminated process, retrieve the process PCB in the PCB collection, and read the process status from the process PCB
  2. If the terminated process is in the execution state, immediately terminate the execution of the process, and set the scheduling flag to true to indicate that the process should be rescheduled after being terminated
  3. If the terminated process has children and grandchildren, it will also be terminated to prevent them from becoming uncontrollable processes
  4. Return all resources of the terminated process to the parent process or OS
  5. Remove the PCB of the terminated process from the queue (or linked list) and wait for other programs to collect information

Implementation of blocking and waking up, suspending and activating processes

  1. The process will block itself by calling the blocking primitive block - so blocking is an active behavior of the process itself
  2. When the event expected by the blocking process—that is, the event that caused the blocking is completed, the relevant process will call the wakeup primitive wakeup to wake up the process waiting for the event.
    block and weakup must appear in pairs, otherwise it may cause a permanent block of a process
  3. suspend: suspend primitive
  4. Activation: active primitive

Process communication method

  1. Pipeline (Pipe): Pipeline is a half-duplex communication method that can communicate between processes with affinity. Pipes can be anonymous pipes (used only between parent and child processes) or named pipes (used between unrelated processes).
  2. Named Pipe: A named pipe is a mechanism for communicating through named objects in the file system. Unrelated processes can communicate by opening and reading from and writing to named pipes.
  3. Shared Memory: Shared memory is an efficient inter-process communication method. By mapping a memory area to the address space of multiple processes, these processes can directly read and write shared memory. Shared memory is suitable for inter-process communication that requires frequent exchange of large amounts of data.
  4. Message Queue (Message Queue): Message Queue is a communication method that can transfer messages between different processes. Processes can send messages to message queues, and other processes can receive messages from message queues. Message queues enable asynchronous communication and decoupling.
  5. Semaphore: A semaphore is a mechanism for inter-process synchronization and mutual exclusion. It controls access to shared resources, ensuring that only one process can access a resource at a time.(The semaphore mechanism will be an important point, which will be discussed later)
  6. Socket: A socket is a method of network communication that can be used to communicate between processes on different hosts. Sockets provide a network-based interprocess communication mechanism.

thread

  1. Concept:
    The purpose of a process is to enable multiple programs to execute concurrently to improve resource utilization and system throughput.
    Threads are used to reduce the time and space overhead of programs during concurrent execution, so that the OS has better concurrency
  2. Comparison between thread and process
    Thread has many characteristics of traditional process, so it is also called light-weight process (LWP) or process element; the corresponding traditional process is called heavy-weight process (HWP), it only has a thread task
    • The basic unit of scheduling: thread has become the basic unit of computer resource allocation and scheduling, and the overhead of thread switching in the same process is much less than that of process switching—it will not cause process switching, but when threads in two processes switch process switch
    • concurrency
    • have resources
    • independence
    • system overhead
    • Support for multiprocessor systems
  3. Thread control block
    TCB is a thread control block, equivalent to PCB
    • thread identifier
    • a set of registers
    • execution state of the thread
    • priority
    • Thread-specific storage area - store information when threads switch
    • signal shielding
    • Stack pointer - procedure call, there may be multiple nesting, so a stack is set for each thread, and there are two pointers to the stack: a pointer to the user's own stack and a pointer to the core stack - corresponding to the thread respectively The stack used when running in user mode and kernel mode

Chapter 3 Processor Scheduling and Deadlock

Three levels of processor scheduling

  1. Advanced Scheduling——Job Scheduling
    Scheduling object: job
    Scheduling process: According to a certain algorithm, the job in the backup queue on the external memory is transferred into the memory, a process is created and resources are allocated for it, and then it waits for the scheduling application in the ready queue
    : Mainly used in multi-channel batch processing system
    No —> Creation state —> Ready state running frequency is low
  2. Intermediate Scheduling—Memory Scheduling
    Scheduling Object: Preliminary Summary: When a process is in a suspended state, it will be transferred from memory to external memory.
    Scheduling Process: Intermediate Scheduling is to select a suitable process in the pending queue according to a certain algorithm. Transfer the data in the external storage to the memory running
    suspend state -> ready state running frequency
  3. Low-level scheduling—process scheduling
    Scheduling object: process
    Scheduling process: decide which process in the ready queue gets the processor according to a certain algorithm
    is the most basic
    scheduling application in the operating system: mainly used in multi-channel batch processing systems, Real-time, real-time OS
    ready state -> high operating frequency in execution state

Process Scheduling Mode

Non-preemptive : Once the processor is allocated to a process, the process will be executed continuously, and the processor of the process will never be seized due to clock interruption or other reasons until the process is completed or an event occurs. When blocked, the processor is allocated to other processes. ——Simple implementation, low system overhead but unable to process urgent tasks in time, suitable for early batch processing systems

Preemptive : The scheduler is allowed to suspend an executing process according to a certain principle, and reassign the processor assigned to the process to another process. (Widely adopted by modern OS) Preemption method: The scheduler is allowed to suspend an executing process according to a certain principle, and reassign the processor assigned to the process to another process. (Widely adopted by modern OS)—— More urgent processes can be prioritized, and processes can be served in turn according to time slices. It is suitable for time-sharing operation, which can give priority to more urgent processes, and can also allow processes to be served in turn according to time slices. It is suitable for time-sharing operating systems and real-time operating systems

Computation of Processor Scheduling

CPU utilization

CPU utilization

system throughput

insert image description here

Turnaround time

insert image description here

Scheduling Algorithm

FCFS - first come first serve algorithm

It schedules jobs in the order in which they arrive, that is, first-come, first-served. In the FCFS algorithm, when a job enters the ready queue, it will be placed at the end of the queue, waiting for the CPU to schedule execution. If the current CPU is executing a job, the next job will not be scheduled for execution until the job is complete.
Pros: average wait time, average turnaround time is shorter.
Disadvantages: It is good for short jobs, but not good for long jobs. If there are a steady stream of short processes added to the ready queue, the long processes will not be served for a long time, and starvation will occur. If they are not served all the time, the service will be blocked. "starve".

For example: In daily life, there is a line to buy food, whoever comes first will be in the front line, and no jumping in line is allowed
insert image description here

SJF - short job first algorithm

Algorithm idea: pursue the shortest average waiting time, the shortest average turnaround time, and the shortest average weighting time.
Non-preemptive short job priority algorithm: When a process with a shorter service time arrives in the ready queue, it waits for the running process to finish executing before scheduling the process with a shorter service time.

For example: when getting on the bus in a kindergarten, you will be queued up according to age, and the youngest will get on the bus first. After the queue is completed, the people who come later will be inserted into the queue according to their age. Of course, this queue is still empty. It is also impossible for people who are getting on the bus to say that the people who are getting on the bus should not get on and queue up again.

Preemptive short job priority algorithm: also known as the shortest remaining time first algorithm, that is, in the ready queue, which process has the shorter remaining running time and which process is scheduled first.

Give an example (this is really embarrassing): Suppose you are eating and there is only one stool, and the person who is currently sitting will take ten minutes to finish eating. give it a go. If the execution time of the two processes is the same, assign processors according to their order (an example of garbage, it is better not to mention it)

Pros: average wait time, average turnaround time is shorter.
Disadvantages: It is good for short jobs, but not good for long jobs. If there are a steady stream of short processes added to the ready queue, the long processes will not be served for a long time, and starvation will occur. If they are not served all the time, the service will be blocked. "starve".

HRRN - High Response Ratio Priority Algorithm

High Response Ratio Priority Scheduling Algorithm (Highest Response Ratio Next, HRRN) is a process scheduling algorithm based on the SJF scheduling algorithm, designed to take into account the priority of long jobs and short jobs.
The HRRN algorithm calculates the response ratio (Response Ratio, RR) of each process and schedules them in order of RR from high to low.
RR = (waiting time + service time) / service time - response ratio is greater than or equal to 1

RR - time slice rotation method

Arrange all the processes in the ready queue in the order of FCFS, and then take out the process at the head of the queue each time, assign a time slice to the process to execute, when the time slice is exhausted, put the process back to the end of the ready queue, and take out The next process at the head of the queue executes.
This mechanism can ensure that all processes can be executed, and there will be no situation where a certain process occupies the CPU all the time and causes other processes to starve.
Guidelines: time slice/10> process context switching time - the overhead of switching processes cannot exceed 1%

PR - Priority Scheduling Algorithm

In the priority scheduling algorithm, each process is given a priority, and the CPU selects the process with the highest priority among the executable processes for execution. When the priority is the same, the round-robin method or the first-in-first-out method is usually used for scheduling

  1. Non-preemptive
    In simple terms, it is to sort the processes waiting in the ready queue according to the priority, and only when the currently running process actively gives up the processor, the sorted process is scheduled.
  2. Preemptive
    On the basis of non-preemption, the ready queue is also "monitored", and whenever the ready queue changes, it is necessary to check whether preemption is required

Note: There are two types of priority.
Static priority: Priority is assigned when the process is created, which is simple and has low overhead, but it is rigid and starvation will occur. Dynamic priority
: Priority is assigned when the process is created, as the process advances or time increase and change
insert image description here

Multilevel Feedback Queue Scheduling Algorithm

In the multi-level queue scheduling algorithm, processes are divided into multiple different queues, and each queue has a different priority. Usually, processes in high-priority queues will be executed first, and processes in low-priority queues may be executed first. Wait for a long time to execute. Processes can be assigned to different queues according to their characteristics, for example, CPU-intensive processes can be assigned to high-priority queues, while I/O-intensive processes can be assigned to low-priority queues.

Definition, causes and prevention of deadlock

Deadlock (Deadlock): Refers to a deadlock caused by multiple processes competing for resources during operation. When the processes are in this deadlock state, without external force, these processes will never be able to move forward.

Four necessary conditions for deadlock to occur:

  1. Mutual exclusion condition: refers to the exclusive use of the allocated resources by the process, that is, a certain resource is only occupied by one process within a period of time;
  2. Request and Hold: A process holds at least one resource, but makes a new resource request, and the resource is occupied by another process. At this time, the requesting process is blocked but holds on to the resource it owns.
  3. Non-preemptive: The resources obtained by the process cannot be preempted by other processes before they are used up, and can only be actively released
  4. Circular waiting condition: It means that when a deadlock occurs, there must be a process - a ring chain of resources, that is, P0 in the process set {P0, P1, P2, ..., Pn} is waiting for a resource occupied by P1, and P1 is waiting The resource held by P2, ... while Pn is waiting for the resource already held by P0.

The prevention of deadlock is to destroy one or more of the four necessary conditions for deadlock - mutual exclusion is necessary for resource sharing, so it cannot be destroyed but must be guaranteed

Deadlock Avoidance and Banker's Algorithm

deadlock avoidance

One-time blocking method: each process (transaction) locks all the data to be used, otherwise, it cannot continue to execute;
sequential blocking method: pre-determines a blocking order for data objects, and all processes (transactions) are locked in this order. Lock;
banker's algorithm: to ensure that the process is in a safe process sequence.

banker's algorithm

Core idea: When a process makes a resource application, first judge whether the allocation will cause the system to enter an unsafe state. If it will enter an unsafe state, the request will not be granted temporarily, and the improvement process will be blocked and waited first.
Banker's Algorithm (Banker's Algorithm) is an algorithm for solving resource allocation problems. It is used to ensure that a process will not cause the system to enter an unsafe state while obtaining resources. The main idea is: before allocating resources to each process, check the security status of the system to determine whether the process's needs can be met. If it can, the resource is allocated; if not, the process' request is denied.
The steps of the banker's algorithm are as follows:

  • Define the Max matrix, indicating the maximum number of resources that each process can obtain.
  • Define the Allocation matrix, indicating the number of resources that each process has obtained currently.
  • Define the Need matrix, and Need[i][j] indicates how many resources j the i-th process still needs to complete the task. Need[i][j] = Max[i][j] - Allocation[i][j]
  • Define the Available vector, and Available[j] indicates the current available quantity of the jth resource.
  • Check that the system is in a safe state. That is, check that Need[i] ≤ Available, if all are satisfied, the system is in a safe state and resources can be allocated; otherwise, the system enters an unsafe state and cannot allocate resources.
  • If the system is in a safe state, the requested resource is assigned to the process. Allocation[i][j] = Allocation[i][j] + Request[i][j]; Available[j] = Available[j] - Request[i][j];
  • Repeat steps 5 and 6 until all resources are allocated.

Banker's Algorithm Java Code Implementation

package zhuxuanyu;

import java.util.Arrays;

/**
 * @title: BankerAlgorithm
 * @Author 竹玄羽
 * @Date: 2023/4/19 10:35
 */

public class BankerAlgorithm {
    
    
    private int[] available; // 可用资源向量
    private int[][] max; // 最大需求矩阵
    private int[][] allocation; // 已分配资源矩阵
    private int[][] need; // 需求资源矩阵
    private int[] work; // 工作向量,记录当前系统可用资源

    public BankerAlgorithm(int[] available, int[][] max, int[][] allocation) {
    
    
        this.available = Arrays.copyOf(available, available.length);
        this.max = Arrays.copyOf(max, max.length);
        this.allocation = Arrays.copyOf(allocation, allocation.length);
        this.need = new int[max.length][max[0].length];
        this.work = Arrays.copyOf(available, available.length);
        calculateNeed();
    }

    // 计算需求资源矩阵
    private void calculateNeed() {
    
    
        for (int i = 0; i < max.length; i++) {
    
    
            for (int j = 0; j < max[i].length; j++) {
    
    
                need[i][j] = max[i][j] - allocation[i][j];
            }
        }
    }

    // 判断系统是否处于安全状态
    public boolean isSafe() {
    
    
        boolean[] finish = new boolean[max.length];
        Arrays.fill(finish, false);
        int[] workCopy = Arrays.copyOf(work, work.length);

        int count = 0;
        while (count < max.length) {
    
    
            boolean found = false;
            for (int i = 0; i < max.length; i++) {
    
    
                if (!finish[i] && isLessOrEqual(need[i], workCopy)) {
    
    
                    finish[i] = true;
                    for (int j = 0; j < workCopy.length; j++) {
    
    
                        workCopy[j] += allocation[i][j];
                    }
                    found = true;
                    count++;
                }
            }
            if (!found) {
    
    
                break;
            }
        }

        return count == max.length;
    }

    // 判断资源请求是否安全
    public boolean isSafe(int processId, int[] request) {
    
    
        if (isLessOrEqual(request, need[processId]) && isLessOrEqual(request, available)) {
    
    
            int[] workCopy = Arrays.copyOf(work, work.length);
            int[][] allocationCopy = copy2DArray(allocation);
            int[][] needCopy = copy2DArray(need);

            for (int i = 0; i < request.length; i++) {
    
    
                workCopy[i] -= request[i];
                allocationCopy[processId][i] += request[i];
                needCopy[processId][i] -= request[i];
            }

            boolean[] finish = new boolean[max.length];
            Arrays.fill(finish, false);

            int count = 0;
            while (count < max.length) {
    
    
                boolean found = false;
                for (int i = 0; i < max.length; i++) {
    
    
                    if (!finish[i] && isLessOrEqual(needCopy[i], workCopy)) {
    
    
                        finish[i] = true;
                        for (int j = 0; j < workCopy.length; j++) {
    
    
                            workCopy[j] += allocationCopy[i][j];
                        }
                        found = true;
                        count++;
                    }
                }
                if (!found) {
    
    
                    break;
                }
            }

            if (count == max.length) {
    
    
                return true;
            } else {
    
    
                return false;
            }
        } else {
    
    
            return false;
        }
    }

    // 复制二维数组
    private int[][] copy2DArray(int[][] array) {
    
    
        int[][] copy = new int[array.length][array[0].length];
        for (int i = 0; i < array.length; i++) {
    
    
            for (int j = 0; j < array[i].length; j++) {
    
    
                copy[i][j] = array[i][j];
            }
        }
        return copy;
    }

    // 判断数组a是否小于等于数组b的每个元素
    private boolean isLessOrEqual(int[] a, int[] b) {
    
    
        for (int i = 0; i < a.length; i++) {
    
    
            if (a[i] > b[i]) {
    
    
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
    
    
        int[] available = {
    
    10, 5, 7};
        int[][] max = {
    
    {
    
    7, 5, 3}, {
    
    3, 2, 2}, {
    
    9, 0, 2}, {
    
    2, 2, 2}, {
    
    4, 3, 3}};
        int[][] allocation = {
    
    {
    
    0, 1, 0}, {
    
    2, 0, 0}, {
    
    3, 0, 2}, {
    
    2, 1, 1}, {
    
    0, 0, 2}};

        BankerAlgorithm bankerAlgorithm = new BankerAlgorithm(available, max, allocation);

        // 判断初始状态是否安全
        System.out.println("初始状态是否安全:" + bankerAlgorithm.isSafe());

        // 判断P1再请求(1,0,2)是否安全
        int[] request1 = {
    
    1, 0, 2};
        System.out.println("P1再请求(1,0,2)是否安全:" + bankerAlgorithm.isSafe(1, request1));

        // 判断P0请求(0,2,0)是否可分配
        int[] request2 = {
    
    0, 2, 0};
        System.out.println("P0请求(0,2,0)是否可分配:" + bankerAlgorithm.isSafe(0, request2));
    }
}

Chapter 4 Process Synchronization

Mutex and Synchronization

Synchronization is a cooperative relationship, a coordinated call between multiple processes or threads established to complete a certain task, waiting in sequence, and sending messages to inform resource occupancy.
Mutual exclusion is a restrictive relationship. When a process or multiple processes enter the critical area, they will be locked. At this time, other processes (threads) cannot enter the critical area. Only when the process (thread) is used to unlock other people Can be used, this technique is often done by blocking.

Critical Resources and Critical Sections

critical resource

  • Some resources in the system are only allowed to be used by one process at a time, and such resources are called critical resources or mutually exclusive resources or shared variables.
  • Mutually exclusive methods should be adopted among the processes to realize the sharing of such resources

critical section

Critical Section refers to the part of the code segment or area that accesses shared resources in a multi-threaded or multi-process environment. In the critical section, access to shared resources is mutually exclusive, and only one thread or process is allowed to enter the critical section to perform related operations at the same time, and other threads or processes need to wait.
The purpose of the critical section is to ensure that concurrent access to shared resources is safe, avoiding race conditions and data inconsistencies. When multiple threads or processes access shared resources concurrently, without proper synchronization mechanisms protecting critical sections, data corruption, inconsistent results, or unpredictable behavior may result.

critical section problem

The critical section problem refers to a series of concurrency problems that may be caused by access to shared resources in a multi-threaded or multi-process environment, such as race conditions (Race Condition), deadlock (Deadlock), livelock (Livelock), etc.
A synchronization mechanism that resolves critical sections should follow four guidelines:
insert image description here

Semaphore mechanism

amount of signal

Semaphore (Semaphore) is a classic synchronization mechanism used to control access to shared resources. It is a counter that can be used to limit the number of threads or processes that are simultaneously accessing a resource.
There are two main operations on semaphores: P (wait, wait) and V (signal, signal).

  • P (wait) operation: When a thread or process wants to access a shared resource, it first performs the P operation, indicating that it wants to gain access. If the counter of the semaphore is greater than 0, that is, there are resources available, then the counter is decremented by 1, and the thread or process can continue to execute and access the resource. If the counter is equal to 0, indicating that there are no resources available, the thread or process will be blocked and enter the waiting state.
  • V (signaling) operation: When a thread or process finishes accessing a shared resource, it performs a V operation, indicating that it has released the resource. This causes the semaphore's counter to increment by one. If there are other threads or processes waiting for the resource, one of them will wake up and gain access.
    insert image description here

integer semaphore

Use an integer variable as a semaphore to indicate the number of certain resources in the system
There are only three operations: initialization, P and V
Disadvantages: the process is busy, etc.

//初始化整型信号量,表示当前的临界资源
int S = 1;

//P操作
void wait(int S){
    
    
	//检查临界资源是否被其他人持有
	while(S<=0);
	//如果资源满足就持有一个
	S--;
}

//V操作
void signal(int S){
    
    
	//使用完资源后释放资源
	S++;
}

record semaphore

In concurrent programming, a record semaphore (Record Semaphore) is a special type of semaphore, also known as an accounting semaphore. Unlike traditional binary semaphores or counting semaphores, record semaphores track each thread's usage of resources, not just count them.
Operation: Record the resource status, P and V.
In addition to an integer value S.value, each semaphore S also has a process waiting queue S.list, which stores the PCB of each process blocked in the semaphore

  • The semaphore can only be accessed through initialization and two standard primitive PVs - executed as OS core code, not interrupted by process scheduling
  • Initialization specifies a non-negative integer value, indicating the total number of idle resources (also known as "resource semaphore") - - If it is a non-negative value, it indicates the current number of idle resources, and if it is a negative value, its absolute value indicates the process currently waiting for the critical section number
typedef struct{
    
    
	//剩余资源数
	int value;
	//等待队列
	struct process control_block *list;
}semaphore;

//请求一个资源
void wait(semaphore *S){
    
    
	//减少资源
	S->value--;
	if(S->value < 0 ){
    
    
		//进程自我阻塞
		block(S->list);
	}
}

//释放一个资源
void signal(semaphore *S){
    
    
	//资源增加一个
	S->value++;
	if(S.value <= 0 ){
    
    
		//唤醒等待队列中的一个进程
		wakeup(S.list);
	}
}

Monitor

A monitor defines a data structure and a set of operations (functions) that can be performed (on the data structure) by concurrent processes. This set of operations can synchronize processes and change data in the monitor.

  1. Mutual exclusion:
    • Variables in the monitor can only be accessed by operations in the monitor
    • Only one process is operating in the monitor at any time
    • done by the compiler
  2. Synchronize:
    • condition variable
    • Wakeup and blocking operations

insert image description here

Chapter 5 Memory Management

insert image description here

Program loading and linking

insert image description here

program loading

  1. Absolute loading mode
    , that is, the address generated during compilation is an absolute address - every time the program is changed, it needs to be recompiled
  2. Relocatable loading method - static relocation
    The compiled object module uses relative addresses - one-time address translation is completed when the process is loaded
  3. Loading method at dynamic runtime
    The loaded module after compiling and linking uses a relative address—it is still a relative address after being loaded into memory, and needs the help of relocating registers

program link

Static Linking

In static linking, the compiler combines the code and data of all object files and library files into a single executable file. The executable file generated in this way contains all the required code and data, without relying on external library files, and can run independently during execution. The advantages of static linking are simplicity, independence, and no need for external dependencies, but the disadvantage is that the executable file is large, which may cause code redundancy and waste.

Dynamic Linking

In dynamic linking, the compiler leaves symbolic references in the program's object files and library files unresolved until the program is run, and the linker links it with the shared library files in the operating system or runtime environment. The executable file generated in this way is smaller, containing only the code and data of the program, and the shared library file is loaded and linked by the system at runtime, thereby reducing the size and memory usage of the executable file. The advantage of dynamic linking is that it saves memory and disk space, and is easy to share and update, but the disadvantage is that it needs to rely on external shared library files, and may be limited by versions and compatibility.

Swap and Cover

exchange

Swapping is a technique to temporarily move a process from main memory (RAM) to auxiliary storage (such as a hard disk) in order to free up enough main memory space for other processes or to meet the dynamic allocation requirements of memory resources. Swap technology allows the operating system to dynamically remove some or all pages of a process from main memory according to the memory requirements of the process and the availability of system resources, thereby optimizing the memory utilization of the system.
step:

  1. Page Replacement: When the main memory space is insufficient, the operating system needs to select one or more pages to be removed from the main memory to make room for new pages. The page replacement algorithm is a decision algorithm used to select the pages to be replaced. Common page replacement algorithms include Optimal, First In First Out (FIFO), Last Most Not Used (LRU), Clock, etc.
  2. Page Scheduling: When a page needs to be moved out of the main memory, the operating system needs to write the content of the page back to the auxiliary memory and record the location information of the page so that it can be reloaded into the main memory when needed in the future. The paging algorithm is used to determine when and in which order pages are written back to auxiliary storage, and when and where pages are reloaded into main memory.
  3. Page Loading: When a page needs to be loaded from auxiliary storage to main memory, the operating system needs to be responsible for reading the content of the page into main memory and updating the status information of the page. Page loading usually involves a read operation from auxiliary storage and an allocation operation of a page in main memory.

cover

Overlaying is a technique that alternately loads different parts of a process in memory. When the memory is not enough to accommodate the entire process, the operating system can divide the process into multiple logical segments (Overlay), and only load the part that needs to be executed each time, while the other parts are kept in the external memory. When switching between different parts of a process, the operating system takes care of loading the appropriate part into memory and replacing the previous part.

Swap and overwrite are the two main techniques for dealing with low memory. Swapping is moving the entire process from memory to external memory, while overlaying is loading different parts of the process alternately in memory. They differ in how data is moved, latency, system performance, and programmer workload. The operating system will choose the appropriate memory management technology according to the specific situation and strategy to optimize memory utilization and system performance.

dynamic partition allocation

data structure

  1. Free partition table
    - one free partition occupies one entry
    - one entry includes partition number, partition size and partition start address
  2. Free Partition Chain
    - Bidirectional Singly Linked List Structure

A Dynamic Allocation Algorithm Based on Sequential Search

FF - First Fit Algorithm

The address is incremented, and a large free partition with a high address is reserved
insert image description here

NF - Loop First Fit Algorithm

The address is incremented, and the free partitions are more evenly distributed
insert image description here

BF - Best Fit Algorithm

insert image description here

WF - Worst Fit Algorithm

insert image description here

Index-Based Dynamic Allocation Algorithm

Fast Adaptive Algorithm

insert image description here

buddy system

insert image description here

hash algorithm

insert image description here

memory recovery

  1. The recovery area is adjacent to the previous free partition at the insertion point—merge the two free partitions and modify the size of the partition
  2. The recovery area is adjacent to the last free partition at the insertion point—merge the two free partitions, and modify the
    starting address and partition size
  3. The reclaimed area is adjacent to the two free areas before and after the insertion point at the same time—merge the three free areas and modify the
    starting address and size of the area
  4. There are no adjacent free partitions before and after the recovery area - create a new entry and fill in the starting address and size of the recovery area
    insert image description here

Paging storage management method

pages and physical blocks

  1. Page: Paging storage management divides the address space of the process into several pages, and does not number each page—that is, the page number
  2. Page frame: The memory space is also divided into several blocks, and the number is the page frame number
  3. Page Size: Should be a power of 2

address structure

insert image description here

page table

When paging, each process has a page table, and the page table resides in the memory, recording the data structure of the corresponding relationship between the page and the page frame

  1. A process corresponds to a page table
  2. Each page of the process corresponds to a page table entry
  3. Each page table entry consists of "page number" and "block number"
  4. The page table records the correspondence between the process page and the actual stored memory block

The address a of the page table entry = the base address of the page table A + the page number n × the number of bytes of the entry w

address translation agency

The system sets a page table register to store the starting address of the page table in memory and the length of the page tableinsert image description here

example

insert image description here

fast watch

In this mechanism, each data/instruction access requires two memory accesses, one for accessing the page table and one for accessing data/instructions. The solution to the double access problem is a small but dedicated and fast hardware buffer called a translation table buffer (TLB) or associative register.
insert image description here
insert image description here

Two-level page table and multi-level page table

Multi-level page tables are infinite dolls
insert image description here

Segmented storage management

advantage

  1. easy programming
  2. Information Sharing
  3. information protection
  4. dynamic link
  5. dynamic growth

Fundamental

In segmented storage management, the address space of a job is divided into several segments, and each segment defines a set of logical information. Each segment
has its own name, and a segment number is usually used instead of the segment name. Each segment They are all addressed from 0 and stored in a continuous address space.
The length of a segment is determined by the length of the corresponding logical information group, so the length of each segment is different.
insert image description here

segment table

Similar to the paging system, a segment mapping table (segment table) is established for each process in the system to realize the mapping from the logical segment to the physical memory area.
Each segment occupies an entry in the table, which records the starting address (base address) of the segment in memory and the length of the segment. The segment
table is stored in memory and its address is saved by the control register.

address translation

insert image description here

Difference between paging and segmentation

insert image description here
insert image description here

Segmented paging storage management

The combination of segmentation and paging principles, that is, first divide the user program into several segments, then divide each segment into several pages, and assign a segment name to each segment

structure

insert image description here

address translation

insert image description here

Chapter 6 Virtual Storage

principle of locality

It means that when the program accesses the memory, it is likely to access data or instructions that are relatively close to the last access, rather than accessing the memory completely randomly

  1. Temporal locality: It means that a program has a high probability of accessing a certain storage unit at a certain time, that is, if a storage unit is accessed once, it is likely to be accessed again in the near future.
  2. Spatial locality: It means that a program has a high probability of accessing a storage unit near a certain storage unit at a certain time, that is, if a storage unit is accessed once, then the storage units near it are likely to be accessed as well. .

Request paging storage management method

request page table mechanism

The basic function is still to map the logical address in the user address space to the physical address in the memory space .
insert image description here

  • Status bit (presence bit) P : used to indicate whether the page has been loaded into memory
  • Access field A : used to record the number of visits to this page within a period of time
  • Modification bit M : Indicates whether the page has been modified after being loaded into memory
  • External memory address : used to point out the address of the page on the external memory

Page Fault Interrupt Mechanism

In the demand paging system, whenever the page to be accessed is not in the memory, a page fault interrupt is generated , and the OS is requested to transfer the missing page into the memory.
Page fault interrupt is a special interrupt, which is obviously different from general interrupts, mainly in two aspects:

  • Generate and handle interrupt signals during instruction execution
  • An instruction may generate multiple page fault interrupts during execution

address translation agency

insert image description here

Memory Allocation Strategies and Algorithms

  1. Fixed Allocation with Local Replacement (Fixed Allocation with Local Replacement): Each process is allocated a fixed number of pages that are always occupied during the execution of the process. If a process needs more pages, it must wait for other processes to free them. A "page fault" occurs when a process needs more pages than it has a fixed allocation of pages.
  2. Variable Allocation with Global Replacement: In this strategy, all processes share a variable-sized memory pool. When a process needs a page, the operating system allocates a certain number of pages for it, and if it needs more pages, it can be allocated from the memory pool. If there are not enough pages in the memory pool, page replacement (replacement) is required.
  3. Variable Allocation with Local Replacement: In this strategy, each process can allocate pages from a variable-sized memory pool. If a process needs more pages, they can be allocated from the memory pool. If there are not enough pages in the memory pool, page replacement (replacement) is required. Unlike the variable allocation global configuration, each process has its own page replacement algorithm, which means that when a process needs to perform page replacement, only pages in that process are considered, not pages in all processes.
  1. Equal allocation algorithm: All available physical blocks in the system are evenly allocated to each process
  2. Proportional allocation algorithm: allocate physical blocks proportionally according to the size of the process
    insert image description here
  3. Allocation algorithm considering priority: two parts, one part is allocated proportionally, and the other part is allocated according to the priority of each process

Page fault rate

Assume that a process has n pages in logical space, allocated memory physical blocks are m (m<=n), the number of successful page accesses during operation is S, and the number of access failures is F: the total number of page accesses of the process:
A =S+F
Page fault rate: f=F/A;
Influencing factors:

  1. Page size: The larger the page, the lower the page fault rate; otherwise, the higher the page fault rate
  2. The number of physical blocks allocated by the process: the more the lower
  3. Page Replacement Algorithm
  4. Intrinsic features of the program

Page Replacement Algorithm

Optimal Page Replacement Algorithm (OPT)

For each page that needs to be replaced, select the page with the furthest time of the latest use for replacement, that is, select the page that has not been accessed for the longest time for replacement. This ensures that the replaced pages will not be accessed again, thereby minimizing the number of page faults.This algorithm is a theoretical algorithm and an optimal permutation algorithm, but it cannot be implemented in an actual operating system
insert image description here

First-in-first-out page replacement algorithm (FIFO)

The first-in-first-out page replacement algorithm (FIFO) is a simple page replacement algorithm, and its core idea is to select the page that enters the memory first for replacement. When a page needs to be replaced, the page that enters the memory earliest is selected for replacement. Because the FIFO algorithm only considers the time when the page enters the memory, and does not consider factors such as the frequency of page use and the probability of future use, its performance is relatively poor.
insert image description here

Least Page Replacement Algorithm (LFU)

The least frequently used (Least Frequently Used, LFU) page replacement algorithm is an algorithm that performs page replacement according to the frequency of pages being accessed. The principle is to find the page with the least access frequency for replacement by maintaining the number of times each page is accessed.

Clock page replacement algorithm

In the Clock algorithm, pages are stored in a circular linked list, and each page has an accessed bit and a modified bit.
When a page needs to be replaced, the algorithm scans the page from the head of the linked list. If the accessed bit of the page is 0, indicating that the page has not been accessed, then the page is replaced. If the accessed bit is 1, it means that the page has been accessed, then the accessed bit is cleared and the page is moved to the end of the linked list. If the accessed bit of all pages is 1, then the algorithm will scan the pages from the head of the linked list, and find the first page whose accessed bit is 0 for replacement.
The simple CLOCK algorithm is to associate an additional bit with each frame, called the usage bit.

  • When a page fits into a physical block, access location 1
  • The page is in the physical block and is being accessed again, so the access location is 1
  • After the page is replaced, the pointer points to the next page to be replaced
  • The page is in the physical block and is accessed again, and the pointer points to the next page to be accessed
  • When the access bit is 0, the page can be replaced. When the page is replaced, the access bit is set to 0. Summary
    : The physical block is regarded as a cycle. When the access bit is 0, it can be replaced. The pointer finds the page or replaces the page. The access position is 1. The pointer points to the next physical block
    (hand drawing is really tiring)
    insert image description here

Improved Clock Page Replacement Algorithm

The improved Clock page replacement algorithm introduces a usage counter to record the number of times a page is accessed. The algorithm works the same as the traditional Clock page replacement algorithm, except for the usage count. When a page needs to be replaced, the algorithm starts searching from the current pointer position. It first searches a circle: find the first visited page with a usage count of 0 in this circle. If such a page is found, it is selected for replacement. If not found, the algorithm enters the second search stage: select a page with a usage count of 1 in this circle for replacement. In the second search stage, a circle is also firstly searched, and then a page with a use count of 1 is selected in this circle.
insert image description here

paging algorithm

A page buffering algorithm is an algorithm for managing pages or blocks of data in a computer's memory. It aims to minimize the number of pages or blocks of data in memory and ensure that the most frequently used pages or blocks of data are always available for quick access . This algorithm is commonly used in caching mechanisms in database management systems.
The main idea of ​​the page buffering algorithm is to divide the memory into fixed size blocks and store pages or blocks of data in the blocks. When a certain page or data block needs to be accessed, the algorithm will first check whether the page or data block is already in memory, if yes, then directly access the page or data block; if not, it needs to remove the page or data block from Disk reads into memory and stores it in a block in memory.

Chapter 7 Input and Output System

I/O System Hierarchy

insert image description here
Device drivers are responsible for direct interaction with hardware devices, including writing commands or data to device registers.
The user layer software is responsible for managing and verifying the user's rights, including checking whether the user has the right to use a specific device; the device driver is responsible for direct interaction with the hardware device, including writing commands or data to the device register.
Device independence software is responsible for handling device-independent operations, including buffer management, data caching and scheduling, and so on.

I/O system interface

insert image description here

I/O devices and device controllers

Classification

insert image description here
The device does not directly communicate with the CPU, but communicates with the device controller. Therefore, there should be an interface between the device and the device controller, and each of the three types of signals corresponds to a signal line in the interface.
insert image description here

device controller

Function

  1. Main function: Control one or more IO devices to realize data exchange between I/O devices and computers
  2. The device controller is the interface between the CPU and the /O device, receives commands from the CPU, and controls the work of the I/O device
  3. The device controller is an addressable device.
    When it only controls one device, it has only one unique device address.
    If the controller can connect multiple devices, it should contain multiple device addresses, and each device address corresponds to a equipment

composition

The interface between the device controller and the processor: realize the communication between the CPU and the device controller, including data lines, address lines and control lines. The
interface between the device controller and the device: one device controller can connect one or more device
I/O O logic: Realize the control of the device.
insert image description here
The driver converts abstract I/O commands into specific commands and parameters, etc., and loads them into the corresponding registers of the device controller. The controller executes these commands to implement the control of the I/O device.

I/O path

concept

I/O Channels (I/O Channels) are an abstraction for input and output operations in a computer system. It provides a mechanism to access different types of input and output devices in a uniform way, enabling programs to interact with these devices.Control I/0 operations by executing channel programs

type

byte multiplexer

insert image description here

Array selection channel

insert image description here

array multiplex

insert image description here

Bottleneck problem

Reason: Because the channel is expensive, insufficient channels cause bottleneck problems
. Solution:

  1. Increase the path between the device and the CPU without adding channels
  2. The multi-channel method not only solves the "bottleneck" problem, but also improves the reliability of the system

Single-path versus multi-path

single channel

insert image description here

multi-channel

insert image description here

I/O device control method

Polling Programmable I/O Mode

Serial operation - the whole process requires CPU intervention

Interrupt Programmable I/O Mode

Parallel operation - requires CPU intervention only for start and end

Direct Memory Access (DMA)

The main I/O control of the block device mainly adopts DMA mode

insert image description here

I/O channel mode

Intervention only when transferring multiple data blocks - requires minimal CPU intervention

Interrupts and Interrupt Handlers

to interrupt

concept

Interruption means that during the execution of the computer, an event that has nothing to do with the currently executing instruction occurs, and it is necessary to immediately interrupt the execution of the current instruction and process the event instead. When an interrupt occurs, the computer will suspend the current execution process, save the current context (program counter, registers, etc.), and jump to the interrupt handler to perform the corresponding operation. The interrupt mechanism can improve the concurrency and responsiveness of the computer system.
Interrupts are triggered by external events and are used to handle events that have nothing to do with the current instruction; while traps are triggered internally by the program for operations such as system calls or exception handling

interrupt vector table

The interrupt vector table stores the entry address of the interrupt handler for each device, and serves as an interrupt number for each device's interrupt request, corresponding to an entry in the interrupt vector table

interrupt priority

The system specifies different priorities for each interrupt source

Handling multiple interrupt sources

insert image description here

interrupt handler

insert image description here

Equipment Distribution and Recycling

distribute

step

  1. Device request: A process sends a request to the operating system for access to a specific device or class of devices.
  2. Device allocation strategy: The operating system selects an appropriate device allocation strategy based on the characteristics of the device and the current system state. Common strategies include first come first serve (FCFS), shortest job first (SJF), priority, etc.
  3. Device allocation: The operating system allocates the requested device to the process, and establishes the connection between the process and the device, so that the process can use the device for input and output operations.

Device Allocation Algorithm

FCFS - first come first serve algorithm

It allocates device resources in the order in which the devices are requested, that is, the process or user that arrives first obtains the right to use the device first.
The FCFS algorithm works as follows:

  1. When a process or user requests to use a device, the operating system adds it to the end of the device request queue.
  2. If the current device is free, the operating system assigns the device to the first process or user in the queue and begins performing device operations.
  3. When the device operation is complete, the operating system removes the process or user from the queue, frees device resources, and assigns the device to the next requesting process or user.
  4. This process loops until the device request queue is empty.

The FCFS algorithm is characterized by simplicity and intuition, and allocates devices in the order of requests, which is fair and has no preference. However, it can lead to increased wait times for long jobs, creating a "starvation" problem. Because if a long-running process requests the device first, subsequent short jobs may be blocked waiting for a long time.

HPF - Highest Priority First Algorithm

It determines the allocation order of device resources according to the priority of processes or jobs. The process or job with the highest priority gets access to the device first.
The HPF algorithm works as follows:

  1. When a process or job requests to use a device, the operating system inserts it into the corresponding priority queue according to its priority. Normally, higher priority processes are inserted at the front of the queue.
  2. If the current device is free, the operating system assigns the device to the process or job with the highest priority and begins performing device operations.
  3. When the device operation is completed, the operating system may continue to allocate the device to the next process or job in the same priority queue, or decide whether to switch to a process or job in a higher priority queue according to the scheduling policy.
  4. This process loops until all queues are empty or there are no higher priority processes or jobs in the system.

The characteristic of the HPF algorithm is to allocate equipment according to the priority, and the process or job with high priority has a higher execution priority. It is suitable for scenarios that require fast response to high-priority tasks, such as real-time systems. However, if there are long-running low-priority tasks in the system, it may cause the low-priority tasks to wait for device resources for a long time, resulting in waste of resources or delay of low-priority tasks.

Recycle

  1. Device Release: After the process finishes using the device, it releases the device and notifies the operating system.
  2. Device recycling policy: According to the device recycling policy, the operating system decides whether to reclaim device resources immediately, or delay recycling for use by other processes.
  3. Device reclamation: The operating system reclaims freed device resources and marks them as available so that other processes can request them again.

spooling system

SPOOLing - spooling technology

In order to alleviate the contradiction between the high speed of CPU and the low speed of I/O equipment, the off-line input and off-line output technology is introduced.

  • Use a program to simulate the function of the peripheral control machine during offline input, and transfer the data on the low-speed I/O device to the high-speed disk
  • Use another program to simulate the function of the peripheral control machine during offline output, and transfer data from the disk to the low-speed output device

Peripheral operations are performed simultaneously with the processing of data by the CPU. This simultaneous peripheral operation is called SPOOLing (Simultaneous Peripheral Operations On-Line), or spooling technology.

System composition

insert image description here
insert image description here

How the SPOOLing system works

insert image description here

features

  1. Improved I/O speed
  2. Transform exclusive devices into shared devices
  3. Realized virtual device function

SPOOLing technology - printer example

The printer is an exclusive device. Using SPOOLing technology, it can be transformed into a device that can be shared by multiple users, thereby improving the utilization rate of the device and making it convenient for users.
insert image description here
When multiple user processes request output printing, the system will grant their requests, but will not immediately assign the printer to them, but the spool management process will complete the following work:

  1. Apply for a free buffer for the process in the disk buffer and send the data to be printed into it for temporary storage;
  2. Apply for a blank user request printing form for the user process, fill in the user's printing requirements, and then change the form to the spool file queue;
  3. When the printer is idle, the spool printing process will take out a print request table from the head of the spool file queue, and then transfer the data to be printed from the output well to the output buffer according to the requirements in the table, and then deliver it to the printer for printing ;
  4. Process all print jobs at once in this way

The system mainly consists of three parts:

  1. disk buffer
  2. print buffer
  3. Spool management process and spool print process

buffer zone

The main goal of buffer management is to optimize data read and write operations to reduce the number of accesses and latency to the underlying storage device. It achieves this through the following aspects:

  1. Cache: Buffer management uses a cache mechanism to cache frequently accessed data in memory to provide fast access. When the application needs to read data, it first looks in the buffer, and returns directly if found, reducing the access to the underlying storage device.
  2. Read ahead and write ahead: Buffer management can load data into the buffer or write data to the storage device in advance through read ahead and write ahead, so as to reduce the delay of actual read and write operations. Read-ahead can pre-load data into the buffer before the application accesses the data, improving data availability and response speed. Write-ahead can buffer data into memory, and then periodically write data in batches to storage devices, reducing the number of actual write operations and delays.
  3. Buffer replacement strategy: When buffer space is limited, buffer management needs to select an appropriate buffer replacement strategy to determine which data should be kept in the buffer and which data should be replaced. Common buffer replacement strategies include least recently used (LRU), first in first out (FIFO), and least frequently used (LFU).
  4. Synchronous and asynchronous operations: Buffer management can control the reading and writing of data through synchronous and asynchronous operations. Synchronous operations require the application to wait for the read or write of data to complete before continuing, while asynchronous operations allow the application to continue with other tasks without waiting for the data operation to complete.

Generally speaking, buffer management plays a vital role in the operating system. It improves the efficiency and performance of data access by rationally managing buffer resources, thereby improving the overall operating effect of the system.

Function

  1. Alleviate the speed mismatch between CPU and I/O devices
  2. Reduce the interrupt frequency of the CPU and relax the limit on the CPU interrupt response time
  3. Solve the problem of data granularity mismatch and improve the parallelism between CPU and I/O devices

Classification

single buffer
double buffer
ring buffer
buffer pool

disk

disk structure

Disk surface (head): The disk device can contain one or more disks, each disk is divided into one or two disk surfaces, each surface has a read-write head
track (cylindrical surface): each disk surface can be divided into several strips Track
Sector: Each track is logically divided into several sectors of the same size. The size of each sector is equivalent to a disk block (data block). The
same number of binary bits can be stored on each track.
The disk density is the number of bits stored in each inch. Obviously, the density of the inner track is higher than that of the outer track. high density.

disk access time

insert image description here

Disk Scheduling Algorithm

The disk scheduling algorithm refers to the algorithm used in the operating system to determine the order of disk access requests. The goal of the disk scheduling algorithm is to optimize the disk access efficiency and reduce the average response time and waiting time of disk access—therefore, the goal of disk scheduling is to minimize the average seek time of the disk ]

First come first served (FCFS)

The simplest disk scheduling algorithm
Scheduling according to the order in which processes request access to the disk
Advantages: Fair and simple, each process's request can be processed in turn, and there will be no situation where a process's request cannot be satisfied for a long time
Disadvantages: Due to The seek is not optimized, so the average seek time may be longer
FCFS is only suitable for occasions where the number of processes requesting disk I/O is small
If the tracks requested to be accessed are relatively concentrated, the performance of the algorithm is acceptable; if the tracks requested to be accessed are scattered, the performance of the algorithm will be poor

Shortest Seek Time First (SSTF)

Select the request closest to the current track for processing. This algorithm can reduce the seek distance of the magnetic head and improve the disk access efficiency.
Good performance, short average seek time may cause "starvation" phenomenon

For example: the track numbers that need to be accessed are 10, 28, 48, 39, 100, 124, 37, 69. The current head is at 50,
then according to SSTF, it will access the nearest number 48. After accessing number 48, see who is closest to 48 Whoever visits - the principle of proximity, so it may take a long time to access the track farther away from the head, resulting in starvation

Scan Algorithm (SCAN)

The head visits the tracks sequentially in one direction until it reaches the boundary of the disk, and then changes direction to continue accessing. This algorithm avoids frequent head movements, but may result in high access latency for some tracks.

For example: the track numbers to be accessed are 10, 28, 48, 39, 100, 124, 37, 69. The current head is at 50, and the head moves in the direction of increasing track numbers. At this
time, the track numbers must be sorted, from small to large, and then from The current track number is separated, accessed along the moving direction of the magnetic head, and reversed when reaching the edge:
sorting: 10, 28, 37, 39, 48,
69, 100, 124 accessing sequence: 69, 100, 124, 48, 39, 37, 28, 10

Cyclic scanning algorithm (C-SCAN)

Similar to the scanning algorithm, the head moves in a certain direction, and requests are processed continuously as it moves. However, when the magnetic head moves to the outermost track to be accessed, it immediately returns to the innermost track to be accessed, and the request is not processed when returning. Treat the cylinders as a chain, connecting the last cylinder to the first cylinder. This results in more even access times across the tracks.

For example: the track numbers to be accessed are 10, 28, 48, 39, 100, 124, 37, 69. The current head is at 50, and the head moves in the direction of increasing track numbers. At this
time, the track numbers must be sorted, from small to large, and then from The current track number is separated, accessed along the moving direction of the magnetic head, and reversed when reaching the edge:
sorting: 10, 28, 37, 39, 48,
69, 100, 124 accessing sequence: 69, 100, 124, 10, 28, 37, 39, 48

Optimal (Optimal) Algorithm (OPT)

According to the future disk access situation, select the optimal track for access. This algorithm needs to predict future requests, so it is difficult to implement in practice, but it can be evaluated as an ideal standard for disk scheduling algorithms.

NStepScan scheduling algorithm and FSCAN scheduling algorithm

insert image description here

Chapter 8 Document Management System

file system hierarchy

  1. I/O control layer: the lowest layer of the file system, composed of disk drivers
  2. Basic file system: used to realize the exchange of data blocks between memory and disk
  3. File organization module: complete transactions related to disk I/O
  4. Logical file system: handles and records operations related to files
    insert image description here

file logical structure

Classification

insert image description here

sequence file

Arrangement

String structure: sorted according to the time of storage, the order of records has nothing to do with keywords - retrieval is time-consuming
Sequential structure: specify a field as a keyword, all records are sorted by keyword - effective search algorithms can be used for retrieval , half search method, interpolation search method, skip search method, etc.

Advantages and disadvantages

Advantages:
It is beneficial to read and write a large number of records with the highest efficiency.
It can be stored on sequential storage devices (tapes).
Disadvantages:
Finding or modifying a single record: Poor
Adding or deleting a record: Difficult
. For example, if a sequential file has 10,000 records, use The sequential search method finds a specified record, which needs to be searched 5000 times on average.
(statistical results)

record addressing

  1. Implicit addressing
    Rptr、Wptr为读、写指针,L为记录的长度
    Fixed-length record: the address of the next record Rptr=Rptr+L / Wptr=Wptr+L
    Variable-length record: the address of the next record Rptr=Rptr+L; / Wptr=Wptr+Li
  2. Explicit addressing
    by location recorded in the file
    using keywords
    insert image description here

index file

  1. Create an index by keyword
    Create an index table for variable-length record files, sort
    the index table by keyword
    to achieve direct access
    本身是一个定长记录的顺序文件
  2. Index files with multiple index tables
    Create multiple index tables for sequential files
    Configure an index table for each data item that may become a retrieval condition

index sequence file

It effectively overcomes the shortcoming that variable-length record files are not convenient for direct access.
A key feature of sequential files is preserved: records are organized in the order of keys.
The average number of disk visits of the first-level directory = 1/2 the number of disk blocks
insert image description here
insert image description here

File Directory

The file directory is also a data structure, which is used to mark the files and their physical addresses in the system for retrieval - mainly to achieve access by name

File Control Block (FCB) and Inodes

File Control Block (FCB): manages and controls the data structure of files, and corresponds to files one by one
File directory: a collection of file control blocks, that is, a file control block is a file directory
item directory file: a file directory is also regarded as a document

Information contained in the FCB

  1. Basic information category: file name, file physical location, file logical structure, file physical structure
  2. Access control information category: the access rights of the file owner, the access rights of approved users, and the access rights of general users
  3. Use information category: the date and time of file creation, the date and time of the last modification of the file, and current use information

index node

insert image description here

Directory Structure

Single-level directory: all user files are in the same directory, the entire system has only one directory table, and each file occupies a directory entry
Two-level file directory:

  • MFD: the main file directory, which records the user name and the storage location of the corresponding user file directory
  • UFD: user file directory, composed of the user's file FCB

Tree structure: The tree directory structure of file management is a common file organization method, which uses a tree structure to represent the hierarchical relationship between files and directories in the file system

tree structure

  1. Root Directory: The root directory is the top directory of the tree directory structure, and it is the starting point of the entire file system. The root directory can contain multiple subdirectories and files.
  2. Directory: A directory is a container in the file system for organizing and storing files. Each directory can contain other directories and files, forming a directory hierarchy. Directories can be used to classify, classify and manage files, providing better organization and search functions.
  3. Subdirectory: A subdirectory is a directory located within other directories, which extends the directory hierarchy. Subdirectories can further contain other subdirectories and files, forming deeper hierarchies.
  4. File: A file is an entity that stores data, which can be text files, image files, executable files, etc. Files are stored in directories and can be located and accessed through the directory hierarchy.
  5. Path (Path): A path is a string used to uniquely identify the location of a file or directory in a tree directory structure. It consists of a sequence of directory hierarchies from the root directory to the target file or directory, usually using a slash (/) as a separator.
  6. Create and delete directories: The tree directory structure allows users to create new directories or delete existing ones. Directories can be created and deleted through commands or file management interfaces provided by the operating system.
  7. Moving and Renaming Files or Directories: The tree-structured directory structure allows users to move files or directories to different locations, or modify the names of files or directories. In this way, files and directories can be organized and managed to suit different needs.
  8. File and directory rights management: The tree directory structure can apply the rights control mechanism to limit the access and operation of files and directories. Users can protect the security of sensitive files and directories through permission settings.
    (It smells like a B tree)
    insert image description here

File Sharing

  1. Directed acyclic graph implementation: In the tree directory, a file is allowed to have multiple parent directories, and the file directory contains information such as the physical address of the file. Use index nodes to achieve sharing, and set the link count count
  2. Symbolic link implementation: Allows a file to have multiple parent directories, but only one of them is the main parent directory, and the others are linked to it through symbolic links

file protection

Use the access matrix to describe the access control of the system

permissions

  1. Copy right
    Use copy right to extend access(ii) owned by a field to other fields in the same column
  2. Ownership
    Use ownership (owner right) to increase certain access rights, or to remove certain access rights
  3. Control rights
    are used to change the access rights of the same row (domain) in the matrix, that is, to change the access rights of processes running in a domain to different objects

Chapter 9 Disk Storage Management

Organization of external storage

continuous organization

Allocate a set of contiguous (contiguous) disk blocks for each file
. Advantages:

  • Sequential access is easy - only the starting block number and length are required
  • Sequential access is fast: the least amount of head movement - the fastest
  • Support random access

shortcoming:

  • It is required to allocate continuous storage space, which will generate many external fragments, so the utilization rate of external storage space is low
  • The length of the file must be known in advance
  • No flexibility to delete and insert records
  • Files cannot grow dynamically

link organization

Allocate discontinuous disk blocks for the file, link all the disk blocks of a file together through the link pointer, and the formed physical file is called a linked file Link
method:

  • Implicit linking: pointers are stored in each disk block, only suitable for sequential access
  • Explicit link: The pointer is explicitly stored in the memory's file allocation table FAT

advantage:

  • Eliminates external fragmentation of the disk and improves the utilization of external memory
  • It is very easy to insert, delete and modify records
  • Files can grow dynamically

index organization

The system creates an index table for each file, and the index table records each physical block corresponding to the file. The disk blocks stored in the index table are called index blocks. The disk blocks where file data is stored are called data blocks.

  1. Link method: If the index table is too large to fit in one index block, multiple index blocks can be linked together for storage.
    Disadvantages: If the file is very large and the index table is very long, many index blocks must be linked together. To find the index block i, you must first
    read the index block 0~i-1 at a time, which leads to disk I/O Too many times, the search efficiency is low
  2. Multi-level index: establish a multi-level index so that the first-level index blocks point to the second-level index blocks. If the file is very large, three, four or even more levels of indexing can also be used. If the K-level index structure is adopted, and the top-level index table is not loaded into the memory, then only K+1 disk read operations are required to access a data block Disadvantage: Even for small files, access to a data block still requires
    K+1 disk read operations
  3. Incremental indexing: a mixture of various indexing methods. For example, the top-level index table of a file includes both direct addressing (directly pointing to the data block), primary indirection (pointing to the single-level index table), and secondary indirection (pointing to the two-level index table),
    etc. : For small and medium files, fewer disk times are required to access a data block

Management of file storage space

  1. Free area table method
    The system creates a free list for all free areas on the external memory, which belongs to the continuous allocation method
  2. free list method
    insert image description here
    insert image description here
  3. group chaining
    insert image description here
  4. bitmap
    The bitmap is more important, so take it out separately

bitmap

Use a binary bit to indicate the usage of a disk block in the disk
- when the value is 0, it means that the corresponding disk block is free
- when the value is 1, it means that it has been allocated

All disk blocks on the disk have a binary bit corresponding to them, so that a set of bits corresponding to all disk blocks is formed, which is called the bitmap block
insert image description here
number: b=n(i-1)+j —— n represents the number of digits in each row
and the row number i and column number j corresponding to the block number b
i=(b-1)/n+1
j=(b-1)/n+1

Disk allocation

  1. Scan the bit map sequentially to find a binary bit or a group of bits whose value is 0
  2. Convert the found one or a group of binary bits into the corresponding disk block number. Assume that the found binary bit whose value is 0 is located in the i-th row and column of the bitmap, and its corresponding disk block number is: b=n(i-1)+j
  3. Modify the bitmap, let map[i, j]=1

Recycling of disks

  1. Convert the disk block number b of the recovered disk block into the row number and column number in the bitmap
    i = (b - 1 ) / n + 1
    j=(b - 1 ) % n + 1
  2. Modify the bitmap, let map[i , j] = 0

Formulas involved in the operating systemFormulas involved in the operating system

Guess you like

Origin blog.csdn.net/weixin_53811934/article/details/131404363