Operating system learning summary (Part 1)

Table of contents

Chapter 1 Introduction to Operating Systems

Operating system goals

The role of the operating system

As an interface between the user and the computer hardware system

 Act as a manager of computer system resources

OS abstraction of computer resources

 mind Mapping

The five main development processes of the operating system (incomplete)

Computer system without operating system configured

 Offline I/O technology (for single-channel batch processing systems)

Single pass batch operating system

multi-batch operating system

time sharing system 

real time system

mind Mapping

Four basic characteristics of operating systems

1 concurrent

2 shares

3 virtual

4 asynchronous

mind Mapping

Operating system kernel (the core, close to the hardware, and the underlying software for computer configuration)

big core

microkernel

Dual operating mode of the processor

User mode

core state 

A brief introduction to interrupts 

system call 

Chapter 1 Mind Map

Chapter 2 Process Description and Control

Description of the process

process concept

The difference between process and program

Process composition

1PCB The operating system controls the management process through the PCB, so the PCB should contain all the information needed by the system to control the management process. 

2 data segments

3 program segments

process definition

Characteristics of the process

How the process (PCB) is organized

linear approach

Link method

Index mode

Process status and status transition 

Five basic states of process

Summarize 

Introducing a suspended state (suspended is intermediate scheduling)        

 process control

Definition of process control

 Creation of process

process termination

Process wake-up and blocking 

Process switching

A rough diagram of process control

process communication

What is process communication

 shared memory system

pipe communication system

messaging system

client-server system

The introduction of threads

Why introduce threads

What is a thread

Introducing changes in threads and comparing threads and processes

Thread status and thread control blocks

How threads are implemented

The combination of two threads

Chapter 3 Processor Scheduling and Deadlock

Why processors need scheduling

Three levels of scheduling 

Advanced Scheduling (Job Scheduling)

Intermediate scheduling (memory scheduling)

Low-level scheduling (process scheduling)

 Comparison of three types of transfers

Summarize

Job and role scheduling 

process scheduling 

When does process scheduling occur?

When can process scheduling and switching not occur?

Process Scheduling Mode

The difference between process scheduling and process switching 

 Summarize

Measurement of Processor Scheduling Algorithms

Goals of Process Level Scheduling Algorithms

important point

Summarize

Scheduling Algorithm

 First come, first served (FCFS)

 Short job first scheduling algorithm (SJF)

High response ratio algorithm (HRRN)

Summarize 

Round robin scheduling algorithm (RR algorithm)

priority scheduling algorithm 

Multi-level feedback queue scheduling algorithm

Comparison of three interactive algorithms

deadlock 

understand

Definition of deadlock

Four necessary conditions for deadlock to occur

When does a deadlock occur?

Deadlock solution 

 Deadlock prevention

Avoid Deadlock (Banker's Algorithm)

 Deadlock detection and release 

Summarize

Chapter 4 Synchronization of Processes

Process synchronization and mutual exclusion

Why introduce synchronization mechanism

Process Synchronization Concept

Two forms of constraints

critical section

Four Principles for Implementing Critical Sections

Summarize

Software synchronization mechanism

The idea of ​​implementing the method

single standard method

 Double sign method​​​​​​​​​​​​ (to solve the problem of free entry)

Double-flag post-checking method (wait to solve busy situation)

Peterson algorithm

Summarize

Hardware synchronization mechanism 

interrupt hiding method

TestAndSet directive

Swap command

Summarize

semaphore mechanism 

Why was the semaphore mechanism introduced?

What is the semaphore mechanism

Several semaphore forms 

Summarize

Application of semaphore mechanism 

Implement process mutual exclusion

Implement synchronization operations

The hex of the implementation process realizes the relationship between the predecessors

 Summarize

Several classic relationships between process mutual exclusion and synchronization

producer-consumer problem

multi-producer-consumer problem

reader problem

Monitor mechanism 

Chapter 5 Memory Management 

Multi-layer structure of memory

Basic three layers 

storage unit

Basic principles of command execution

(1) How the instruction works

Logical address and physical address

Compile, link and load 

 compile

 Three link methods

 Three kinds of loading

 Summarize

How does the operating system manage memory? 

Allocation and recycling of memory space

Expansion of memory space (implementing virtuality)​

address translation

memory protection

 Summarize

Swapping and overlay technology 

coverage technology

swap technology 

Summarize

Contiguous allocation storage management

​​​Single continuous allocation

Fixed partition allocation

Dynamic partition allocation (variable partition allocation)

 dynamic partitioning algorithm

first fit algorithm

 best fit algorithm

Worst (Large) Fit Algorithm

Proximity Adaptation Algorithm (Loop First Adaptation)

Inductive comparison of four algorithms

Partition allocation and deallocation operations

Internal Fragmentation vs. External Fragmentation

compact technology 

Paging storage management method

Discontinuous Allocation Management

Why Paging Storage

 How to perform address translation

 Logical structure of paging storage

Summarize

Address Change Mechanism for Paged Memory Management 

page table register

Address translation process

Summarize

Change of address agency with fast table

Secondary page table

Why introduce a secondary page table?

The principle and address structure of the secondary page table

Summarize

Segmented storage management method 

purpose of segmentation

what is segmentation

Segmented Logical Address Structure

segment table

address translation

Comparison of segmented and paging management

Segmented sharing of information

Summarize

Segmented page storage management method 

Analysis of the advantages and disadvantages of paging and segmentation

Segmentation + paging = segmented page management

Logical address structure of segment page management

Segment table and page table of segment page storage

Three memory accesses in the address translation process of segment page management



Chapter 1 Introduction to Operating Systems

Operating system goals

1. Convenience. OS was created to facilitate users to use computer hardware. Computers without OS are very difficult to use.

2 Effectiveness, two layers of meaning, the first layer is to improve the resource utilization of the system, and the second layer is to improve the system throughput

3 Scalability, os needs to be updated continuously to meet higher requirements

4 Openness means that the design of the os should meet international standards, which is conducive to interconnection

The role of the operating system

As an interface between the user and the computer hardware system

There are three ways for users to use computers through os

1 command mode

The command mode allows users to directly use

1 Online command interface (the user speaks a sentence, and the computer does a sentence)

2 offline command interface (the user says a bunch, the computer does a bunch)

2 system calls

The program interface is used indirectly through the program

Program interface = system call = generalized instruction

3 images/window

For example, if we drag the desktop file into the recycle bin, it will be deleted.

 Act as a manager of computer system resources

Take an example where we use qq

1 File management is responsible for file access, sharing and protection (we must first find the location of the QQ installation package when using QQ)

2Memory management is responsible for memory allocation and recovery (we are going to open QQ.exe, put the relevant data of the modified program into the memory, the operation of a process must be put into the memory first, and then the CPU can process it)

3 Processor management is responsible for the allocation and control of processors (the QQ program runs normally, and the QQ process is running on the CPU at this time)

4I/O management is responsible for the management and allocation of I/O devices (we use QQ to make video calls, and the CPU allocates the camera to QQ)

OS abstraction of computer resources

The expansion of the hardware, the tool of the hardware, the OS is a craftsman

 mind Mapping

The five main development processes of the operating system (incomplete)

Computer system without operating system configured

Manual operation mode

The early operation method will realize the perforated paper bag, put it into the paper bag input machine, and then input the program and data on the paper bag into the computer, and then start the computer to run

Features

1 user has exclusive access to the entire machine, and all resources of the entire machine are exclusively owned by the user on the machine.

2. The CPU waits for manual operation (the CPU needs to wait for the user to load and unload paper bags, which seriously wastes CPU resources). This is a human-machine conflict. In order to solve the problem that the CPU processing speed is much higher than that of I/O devices, offline I/O technology is proposed.

 Offline I/O technology (for single-channel batch processing systems)

Input the data and programs of the input device to the tape through the peripheral machine, put a lot of data and programs on the tape, and then enter the data and programs into the processor for processing. The processed data is first put into the tape, and then passed through the peripheral machine. , put into the output device

Features

1 Reduce the idle time of the CPU (because the time of loading and unloading the tape and sending it from the low-level I/O device to the high-speed tape is completed offline and does not occupy the CPU time)

2Improved I/O speed

Single pass batch operating system

Why was the single-channel batch processing system introduced? In order to replace manual operations and realize continuous processing of jobs, the simplest single-channel batch processing system was formed by adding a supervisory program to the offline method.

Why is it called a single-channel batch processing system. Because there is only one program running in the memory, and the next program can only be loaded after one program ends, the CPU has a lot of time when it is idle (for example, when a program calls I/O resources, it will also occupy the CPU, so a single program The batch system resources cannot be fully utilized. 

multi-batch operating system

In order to further improve resource utilization and system throughput

In this system, jobs submitted by users will first be stored in external memory and placed in a queue. Then the job scheduler will select several jobs from the backup queue according to a certain algorithm and transfer them into the memory so that they can share the CPU. and system resources to make it work concurrently ( running in parallel at the macro level, running serially at the micro level, using the CPU alternately)

Features

High resource utilization because multiple programs run concurrently in the system and share system resources

The system throughput is high   because the CPU and other system resources are always busy. Switching is only performed when the job is completed or cannot continue, and the system overhead is small.

Long average turnaround time

No interactive capabilities, because everything is done by the system itself and the user cannot control it.

Problems encountered in multi-channel batching

1 Contention processor issue

2Memory allocation and protection issues

3I/O device allocation problem

4. Organization and management of files

5 Job management issues

6 User and system interface issues

In order to solve these problems, the operating system was introduced, so the OS is a set of programs that effectively organizes and manages computer hardware and software resources, and reasonably schedules various jobs to facilitate user use.

time sharing system 

The time-sharing system is cited to solve the problem of UAV interactive function of multi-channel batch processing system

The concept of a time-sharing system is that the computer serves each user/job in units of time slices, but it cannot prioritize some urgent tasks.

Function

1 Human-computer interaction, being able to control the computer manually and modify some data and errors

2 Shared hosting, multiple terminals share the host, but each user cannot directly feel the presence of other users

Conditions for realization

1. Receive in time, to be able to receive commands and data entered by multiple users in a timely manner.

2. Process in time, 1. Use the method of directly entering the job into the memory. 2. Use the method of rotating each job. In order to prevent a job from occupying the host for a long time, the concept of time slice is introduced. The system stipulates that each program can only run one time slice at a time.

Features

Multiplexing means that multiple terminals are connected to one host and serve each user according to the time-sharing principle.

Independence means that the system provides a user environment where each user operates on their own terminal without interfering with each other.

Timeliness means that the user's request can be responded to in a short period of time, which is determined by the user's waiting time for reception.

Interactivity means that users can conduct extensive human-computer dialogue with the system through the terminal, such as requesting system services, such as editing files and data processing, accessing the file system in the system, and requesting to print operation results.

real time system

The real-time system was introduced to solve the problem that urgent tasks cannot be prioritized in time-sharing systems.

The most important feature of a real-time system is to use time as a key parameter to process certain signals it receives in a timely and real-time manner. This means that the system can respond to requests for external events in a timely manner and complete the processing of the event within the specified time. and control all real-time tasks to run in a coordinated manner

Two real-time task types

1 Hard real-time tasks , which must be completed within strict time limits, such as industrial control and weapons control systems

2 Soft real-time tasks that can occasionally receive time violations, such as 12306 ticket booking system

Features

1Multiple channels, unlike time-sharing systems, the system periodically collects multiple sites and controls multiple objects or multiple actuators.

2. Independence. In the real-time system, the collection of information and the control of objects will not affect each other.

3 timeliness, the real-timeness of the real-time control system is determined by the cut-off time required by the control object

4. Check the book for interactivity  

5Reliability

mind Mapping

Four basic characteristics of operating systems

1 concurrent

The concurrency of the OS refers to the existence of multiple running programs in the computer system at the same time.

The difference between parallelism and concurrency is that parallelism means that two or more events occur at the same time, while concurrency means that two or more events occur at the same time interval.

Multiple computer programs are carried out simultaneously at the macro level, but alternately at the micro level

The concept of process is introduced under the premise of concurrency

  • A single core (CPU) can only execute one program at a time, so the operating system will coordinate multiple programs so that they execute alternately (these programs occur simultaneously at the macro level and alternately at the micro level)
  • The operating system is accompanied by "the emergence of multi-programming technology", so the operating system and concurrency are born together

2 shares

Refers to the resources in the system that can be shared by multiple concurrently executing processes in memory

Two ways to share

1. Mutually exclusive sharing mode. Certain resources in the system are only allowed to be used by one process within a period of time (critical resources). QQ and WeChat can only be used by one process at the same time.

2. Simultaneous sharing mode:   A type of resource in the system that allows multiple processes to access it "simultaneously". Simultaneity here refers to simultaneous access at the macro level, and alternating access at the micro level. For example, using QQ and WeChat to send file B at the same time.

Without concurrency and sharing, there is no virtualization and asynchrony. Concurrency and sharing are the conditions for the existence of each other

3 virtual

On the OS, a physical entity is transformed into a logically corresponding function through a certain technology. For example, a CPU can handle several processes at the same time, and 4GB of running memory can run more than 4GB of applications.

Time division multiplexing technology (the fundamental reason for improving resource utilization is that it will serve other users during its free time serving one user)

1Virtual processor technology 2Virtual device technology

space division multiplexing technology

4 asynchronous

Only concurrency can lead to asynchrony

Due to resource constraints and other factors, the process usually cannot be completed in one go, but will stop and go.

For example, process A is occupying the CPU for calculations, and process B also wants to occupy the CPU for calculations. Process B can only wait until process A has finished calculating, and process A goes to access disk resources. At this time, process B will occupy the CPU for calculations, and process B is still busy. Before the calculation is completed, process A takes out resources from the disk. Process A finds that B is occupying the CPU at this time. At this time, process A needs to wait until B completes the calculation before continuing to the CPU for calculation. Since the time each process occupies resources is not fixed, the execution of the process advances at an unpredictable speed.
 

mind Mapping

Operating system kernel (the core, close to the hardware, and the underlying software for computer configuration)

big core

The big kernel code is huge, has a messy structure, and is difficult to maintain, but has high performance.

Features included

1Clock management

Realizing timing functions, such as time slice rotation scheduling, deadline control of real-time systems, and maximum running time control of batch processing all rely on clock management.

2 Interrupt management

Responsible for implementing the interrupt mechanism, such as various types of system calls, keyboard command input, process scheduling, device drivers, etc.

3 primitive operations

The so-called atomic operation means that all actions in an operation are either done or not done at all. Atomic operations are used when realizing process synchronization.

4Process management

5Memory management

6Equipment management, etc.

microkernel

Few functions, but clear structure, but requires frequent switching between user mode and core, low performance

1Clock management

2 primitive operations

3Interrupt management

Dual operating mode of the processor

User mode

User mode can only execute unprivileged instructions

core state 

Core state privileges and non-privileges can both execute

Privileged instructions such as starting external devices, setting system clock time, turning off interrupts, etc. Instructions to switch to user mode are also privileged instructions.

A brief introduction to interrupts 

The occurrence of an interruption means that the operating system needs to be accessed to carry out management work and achieve concurrent execution.

From user mode to core mode is achieved through interrupts

1 After the interrupt occurs, the CPU enters the core state

2. When an interrupt occurs, the current process is suspended and the operating system kernel handles the interrupt.

3 different interrupt signals, different processing

Execute a privileged command from core mode to user mode

system call 

System calls are interfaces left by the operating system to applications. Applications can request services from the operating system through system calls.

 System calls are made in core mode

 1. The trap instruction is executed in user mode. After executing the trap instruction, an internal interrupt is triggered immediately, causing the CPU to enter the core state.

2. The request to issue a system call is in the user mode, and the corresponding processing of the system call is performed in the core mode.

3. The trap instruction is the only instruction that can only be executed in user mode and cannot be executed in core mode.

Type of system call

1Device management

2File management

3 process control

4 process communication

5Memory management

Chapter 1 Mind Map

Chapter 2 Process Description and Control

Description of the process

process concept

A program is a sequence of instructions. The code of the program is placed in the program segment, and the data required by the program and the output data are placed in the data segment. In a single-channel processor, there is only one program, so there is no need to find the program. program segment and data segment.

However, after the introduction of multiprogramming technology, multiple programs, program segments and data segments of different programs are placed in the memory, so how does the OS need to find the data segments and program segments corresponding to each program? Here Under the conditions, the concept of process is introduced

( In order to enable programs to be executed concurrently and to describe and control concurrently executed programs )

The difference between process and program

1The process is dynamic and the program is static

2. Processes are independent and can be executed concurrently, but programs cannot

3 There is no one-to-one correspondence between the two

4 Processes can execute asynchronously and restrict each other, but programs cannot) Processes run asynchronously and restrict each other; programs do not have this feature.
However, processes and programs are closely related: a process cannot be detached from a specific program, and a program stipulates the actions to be completed by the corresponding process.
5 compositions are different. A process includes PCB, program segment, and data segment. A program consists of data and instruction codes.
6A program is a static entity that contains all instructions and data. In addition to occupying disk storage space, it does not occupy system operating resources such as CPU and memory.
7 The process consists of program segments, data segments and PCB, which will occupy system running resources such as CPU and memory.
8A program can start multiple processes to complete together.
Connection: The process cannot be detached from the specific program. The program stipulates the actions to be completed by the corresponding process.

 

Process composition

1PCB The operating system controls the management process through the PCB, so the PCB should contain all the information needed by the system to control the management process. 

The function  of the PCB is to make a program (including data) that cannot run independently in a multi-programming environment become a basic unit that can run independently and a process that can be executed concurrently with other processes.

1 as a symbol of independent operating basic unit

2 Implement intermittent operation mode

3 Provide information required for process management

4. Improve process scheduling information

5Achieve synchronization and communication with other processes

PCB is the only sign of the existence of the process

Each process configures a PCB

 For example, when the OS wants to schedule a process for execution, it must check its current status and priority from the PCB of the process; after scheduling a process, it must set the process to resume based on the processor status information saved in its PCB. At the running site, the program and data are found based on the memory address of the program and data in the PCB;
        during the execution of the process, when synchronization, communication or access to files needs to be achieved with the process it cooperates with, it is also required. Access the PCB;
        when the process suspends execution for some reason, the processor environment of the breakpoint must be saved in the PCB.
        It can be seen that throughout the life of the process, the system always controls the process through the PCB, that is, the system perceives the existence of the process based on the PCB of the process rather than anything else.
 

content

1Process description information

2Process control and management information

3 Resource Allocation List

4 Processor related information

2 data segments

Where the data required by the program and the output data are stored

3 program segments

where the program code is stored

process definition

A process is the execution process of a program (process entity) and an independent unit for resource allocation and scheduling in the system (the definition emphasizes the dynamics of the process)

The program segment, data segment, and PCB make up the process entity. Generally, the process entity is also called a process. For example, creating a process means creating the PCB in the process entity, and canceling the process means canceling the PCB of the process entity.

Strictly speaking, processes and process entities are different. Process entities are static, while processes are dynamic.

Characteristics of the process

Dynamics  Dynamics is the most basic characteristic.   A process is an execution process of a program. A process is executed by creation and destroyed by cancellation. A process has a certain life cycle, and a program is just a set of ordered instructions.

Concurrency There are multiple process entities in the memory that can be executed concurrently

An independent process is a unit that runs independently, obtains resources independently, and receives scheduling independently. Therefore, programs that have not established a PCB cannot be executed concurrently as an independent unit.

Asynchrony Each process is independent and proceeds at an unpredictable speed, so the operating system must use a synchronization mechanism to complete the synchronization of the processes

Structural A process corresponds to a PCB, and a process consists of a program segment, a data segment, and a PCB

How the process (PCB) is organized

linear approach

Organizing all PCBs in the system into a linear table is simple to implement, but each search requires scanning the entire table, so it is suitable for systems with few process numbers.

Link method

Through the link words in the PCB, the PCBs of processes with the same status are linked into one queue respectively, which will form several blocking queues and idle queues in the ready queue. A single CPU execution pointer queue has only one process, because one CPU can only run a process

Index mode

The system creates several index tables based on the different statuses of all processes, such as the ready index table and the blocking index table, and records the starting and ending addresses of each index table in the memory in some dedicated units of the memory. Each index table entry is , recording the address of a certain PCB of the corresponding topic

Process status and status transition 

Five basic states of process

The ready state means that the process is in a state of being ready for execution, that is, it has all the required resources except the CPU (everything is ready, only the CPU is missing)

Blocked state: The executing process cannot continue to execute due to an event such as an I/O request and application for a buffer. At this time, process scheduling occurs and the OS allocates the CPU to other processes. This process is in a blocked state and can only become a running state . blocking state

The execution state refers to the state in which the program is executing after acquiring the CPU. In a single processor state, there is only one execution process, while a multi-processor has multiple execution states.

Transition between three states

 The creation state  process is being created, initializing the PCB, then the OS allocates resources to it, and finally converts the resources of the process to the ready state and inserts it into the ready queue.

Termination status The process is being withdrawn from the system, the OS reclaims its resources, and the PCB is withdrawn

 


Blocking until ready is a passive behavior

Running until blocking is an active behavior

You cannot go directly from the blocking state to the running state, you must go through the ready state

It cannot go from the ready state to the blocking state, because blocking is a behavior actively required by the process and must be in the running state.

Summarize 

Introducing a suspended state (suspended is intermediate scheduling)        

In order to meet the needs of the system and users for process observation and analysis, suspension is introduced. When a process is suspended, it means that the process is in a static state at this time. For example, if the process is being executed, the execution of the process will be suspended. If the process In the ready state, it does not accept scheduling for the time being. Suspension means moving the process from memory to external memory.

Why introduce suspension

1. The needs of end-users. End-users find that the programs they are running have suspicious problems and wish to suspend the programs so as not to modify and study them.

2. Needs of the parent process Sometimes the parent process wants to suspend its child programs in order to inspect and modify the child processes or coordinate the activities of the child processes.

3. The need for load adjustment. When the workload in the real-time system is large, some unimportant processes must be suspended to ensure that they can run normally.

4OS needs, the OS sometimes wants to suspend certain processes in order to check the resource usage or accounting during the running process.

5 Swapping is required, which will be discussed in the memory chapter.      

The transition of the five basic states of the process after the introduction of the suspend operation

Suspend primitive Suspend and activate Active

If a quiescent blocked waiting event completes it becomes quiescent ready

Executing processes become quiescent ready after being suspended 

 process control

Definition of process control

Process control is the most basic function in process management. It is responsible for creating new processes, terminating completed processes, and implementing process state transitions. Generally, process control is implemented by primitives in the OS kernel.

Implementing process control using primitives

The operation of the primitive must be synthesized in one go and cannot be interrupted. This kind of operation is called an atomic operation, and the atomic operation is implemented by turning on and off interrupts.

The right to open and close interrupts is very large, so it should be run in the kernel

 Creation of process

Process creation primitives

1. Apply for a blank PCB (apply for a unique digital identifier for the new process). The supplementary digital identifier is the internal identifier, which is convenient for the system to use the process, and the external identifier is also a kind of process identifier, which is for the convenience of the user. For the use of processes, in order to describe the family relationship of the process, the parent process identifier and child process identifier should also be set, and the user identifier can also be set to indicate the user owned by the process

2. Allocate the resources required for the new process to run. The resources here include physical and logical resources, such as memory, files, I/O devices and CPU time, etc.

3 Initialize PCB initialization standard information, initialize processor status information, initialize processor control information

4 If the process ready queue can accept the new program, insert the new process into the ready queue

Events that cause process creation

The user logs in to the time-sharing system. If the login is successful, the system will create a process for the user and insert it into the ready queue.

Job Scheduling In a multi-channel batch processing system, when job scheduling schedules certain jobs according to the appropriate algorithm, the program is loaded into the memory, then a process is created for it, and it is inserted into the ready queue.

Provide service When the running user program makes some kind of request, the system will create a special process to provide service for the user, if the user process wants to print the output result, then create a printing process to serve it

The application requests that the user process itself actively create a child process, so that the child process and the parent process can be executed concurrently to complete the characteristic tasks. For example, a user process needs to continuously read data from the keyboard terminal, then process the input data accordingly, and finally display the processing results on the screen in a table. In order to complete the user process concurrently and quickly, a keyboard input process can be created. Processing and table output processes

process termination

process termination primitive

1 According to the identifier of the terminated process, index the PCB of the process from the PCB collection

2. If the 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 Terminate all descendant processes of the process to prevent them from becoming uncontrollable processes

4Return all resources of the terminated process to its parent process or the operating system

5Remove PCB

Event that causes process termination

1 ends normally

Indicates that the process has ended normally and is ready to exit.

2 abnormal end

Out-of-bounds error The storage area accessed by the program has exceeded the range of the storage area occupied by the process.

Protection error: The process attempts to access resources or files that the process is not allowed to access, or accesses them in an incorrect way, such as the process trying to write to a read-only file.

Instruction error : The program attempts to execute an instruction that does not exist (the reason for this phenomenon may be that the program is transferred to the data area and the data is treated as an instruction)

Privileged Instruction Error A process attempts to execute an instruction that is only allowed to be executed by the OS kernel.

Run timeout The running time exceeds the set maximum value

Wait timeout The process's wait for an event exceeds the specified maximum.

Arithmetic error The process attempted to perform a prohibited operation, such as division by zero.

I/O error refers to an error occurring in the I/O device process

3External intervention

Operator or OS intervention For example, if a deadlock occurs on a computer, the operator or OS will terminate the deadlock

The parent process requests that when the child process completes the work required by the parent process, the parent process can request that the child process be terminated.

Parent process terminated When the parent process terminates, all child processes should also be terminated

Process wake-up and blocking 

Process blocking and waking up appear in pairs and must be used together.

The blocking primitive is the result of self-invocation by the blocked process and is an active behavior.

The wake-up primitive is implemented by the cooperation of an awakened process or by being called by other related processes.

Events that cause processes to block and wake up

1. Requesting shared resources from the system fails. For example, if a process requests a printer, but the system has allocated the printer to another process, the requesting process is blocked. The process will be awakened only after the other process releases the printer.

2 Waiting for the completion of a certain operation. For example, if a process starts an I/O device, if the process can only be executed after the I/O device operation task, the process will automatically enter the blocking state after starting the I/O device. To wait, the interrupt handler will wake up the process after the I/O operation is completed.

3 The new data has not arrived. For example, there are two processes AB, A is used to input data, and B is used to process the input data. If A has not finished inputting the data, B will block because there is no required data.

4. Waiting for the arrival of new tasks. For example, the sending process in the network environment, its task is to send data packets. If the existing data packets have been sent and there is no new data packet to be sent, the sending process will block itself. Only The sending process will be awakened only when a new data packet arrives.

blocking primitive process

1 For a running process, if an event that causes blocking occurs, find the PCB of the process to be blocked and then use the blocking primitive Block to block itself. Blocking a process is an active behavior

2 After entering the Block, first save the on-site environment of the process operation into the PCB, then immediately suspend the operation of the process, and then change the current state in the PCB from running to blocked

3 Insert the PCB into the corresponding blocking queue

wake up primitive process 

1 Other processes call the Wakeup primitive to find the PCB of the process in the event blocking waiting queue

2 Remove the PCB from the blocking queue and change the status to ready

3 Insert the PCB into the ready queue and wait for the process to be scheduled

Process switching

From the running state to the blocking state/ready state, a process will change from the ready state to the running state

Store the parameters of the operating environment in the PCB

The PCB is moved into the corresponding queue

Select another process PCB and execute that process

Restore the scene according to the PCB of the process to be run

Process blocking only changes the running process to the blocked state, and does not arrange for another process to occupy the PCB. Process switching not only changes the process to the blocked state or the ready state, but also arranges for other processes to occupy the PCB. Use the CPU because the conditions that cause the process to block and the process to reach the ready state are different.

调度It refers to the behavior of deciding which process to allocate resources to. It is a decision-making behavior. It
切换refers to the behavior of actual allocation and execution behavior.
Generally speaking, there is existing resource scheduling and process switching later. 

   What process control has in common

A rough diagram of process control

Summarize

process communication

Why introduce process communication

A process is the basic unit for allocating system resources, so the address spaces used by each process for memory are independent of each other (for example, if we want to share the pictures in the album with others through QQ, the QQ process needs to access the album process, but cannot directly access , so use process communication)

What is process communication

Process communication refers to the exchange of information between processes, which can be divided into high-level and low-level

 shared memory system

Definition: Processes that communicate with each other share certain data structures or memories to achieve information exchange between processes.

1 Communication method based on shared data structure

Each process is required to share certain data structures to realize information exchange between processes, such as the bounded buffer in the producer-consumer problem, the OS is responsible for providing shared memory, and programmers are required to set up and correct Processes are processed synchronously , which is a low-level process communication, suitable for transferring small amounts of data , for example, the shared data structure is an array of length 10

2 Communication method based on shared memory

In order to transmit a large amount of data, a shared storage area is designated in the memory, and each process can exchange information by reading/writing the shared storage area to achieve communication. The form and location of the data are in the charge of the process, not the OS, which is Advanced communication methods

 Sharing a space that everyone can access. Only one process can perform read or write operations at a time. It is a mutually exclusive use of the shared space.

pipe communication system

Definition A pipe (Pipe) is a shared file used to connect a reading process and a writing process to realize their communication.

important point

A pipe can only achieve half-duplex communication (only one-way transmission can be achieved during a certain period of time). If you want to achieve simultaneous two-way transmission, you need two pipes.

1. Mutually exclusive processes must access channels if they want to be mutually exclusive.

2 Synchronization : The writing process fills the pipe with a certain amount of data, then the writing process blocks, and then the reading process reads the channel data. After reading the channel data (the pipe is empty), the reading process blocks.

3. Determine whether the other party exists . Communication can only be carried out if the other party exists.

4If it is not full, it cannot be read, and if it is not empty, it cannot be written.

5 Once the data is read, it will be discarded from the pipeline, indicating that there can only be one reading process, otherwise there may be a possibility of reading wrong data

messaging system

Defined in this mechanism, the process does not need to use any data structure or memory, but encapsulates the communication data in the message Li in units of formatted messages, and uses a set of communication commands (primitives) provided by the OS, in Information is transferred between processes and data exchange between processes is completed.

direct communication

Define the sending process to use the sending primitive provided by the OS to directly send the message to the target process

direct communication primitives

Symmetric addressing mode  requires that both the sending process and the receiving process must provide each other's identifier explicitly.

There are usually two communication commands

1send(receiver,message)     2receive(sender,message)

send(P2,m1) means sending information m1 to the receiving process P2 receive(P1,m1) means receiving message m1 from P1

Disadvantage: Once you change the name of a process, you need to check the definitions of all other processes.

Asymmetric addressing mode

Some processes need to communicate with multiple processes, and the name of the other process cannot be determined in advance. In this method, there is no need to name the sending process in the primitive of the receiving process, but only the source information needs to be filled in. Not only the return value after completing the communication, the sender also needs to name the receiving process.

send(P,message) sends a message to process P

receive(id,message), receives messages from any process. The id variable can set the ID or name of the communication sender process.

Process synchronization method

When communicating between processes, a process synchronization mechanism is also required so that each process can coordinate communication.

three possibilities

1 The sending process is blocked and the receiving process is blocked. This is used for tight synchronization between processes without buffers between them.

2. The sending process does not block, and the receiving process blocks. This is the most commonly used method. The sender does not block, so one or more messages can be sent to multiple targets as quickly as possible, while the receiving process is blocked and will not be awakened until there is a message.

3 Neither the sending process nor the receiving process blocks. This will only block them when they cannot run.

communication link

1 One-way communication link, only allows the sending process to send messages to the receiving process, or vice versa

2 Two-way communication link, allowing A to send information to B, and B can also send information to A

indirect communication method

p59

Both the sending process and the receiving process send and receive messages by sharing an intermediate entity (mailbox).

client-server system

Summarize

The introduction of threads

Why introduce threads

Some processes may need to do many things "simultaneously", but traditional processes can only execute a series of programs serially (for example, when you use QQ, you log in to your account, which is equivalent to creating a process. You can type while While video chatting, these are carried out "simultaneously", but traditional processes cannot achieve such a function, so threads are introduced to speed up concurrency)

If the purpose of introducing processes into the OS is to enable multiple programs to execute concurrently and improve resource utilization and system throughput, then the purpose of introducing threads into the OS is to reduce the time and space spent on concurrent execution of programs. Overhead, allowing the OS to have better concurrency (allowing processes to complete multiple functions concurrently)

What is a thread

Threads can be understood as lightweight processes (this reflects the ultimate principle of computers - matryoshka dolls)

Thread is a basic CPU execution unit and the smallest unit of program execution flow.

After the introduction of threads, not only programs can be executed concurrently, but processes can also be executed concurrently, thereby improving system concurrency.

After the thread is introduced, the process is the allocation unit of system resources other than the CPU , such as printers, and the memory space is allocated to the process

Introducing changes in threads and comparing threads and processes

Resource allocation and scheduling

When threads are not introduced, the process is the basic unit of scheduling and resource allocation. After threads are introduced, the process is the allocation unit of resources (except CPU), and the thread is the basic unit of scheduling.

Concurrency

In traditional OS, only processes can run concurrently. After threads are introduced, threads between processes can also execute concurrently.

have resources

A process can own resources and can be used as a basic unit of resources in the system, but a thread can basically not own resources. The little resources it owns are also to ensure that they can be independently allowed, but threads can share what they belong to . resources , because all threads belonging to the same process have the same address space, which means that threads can freely access every virtual address in the address space, and can access all resources of the process to which they belong

independence

The independence of threads is much lower than that of processes. In order to prevent mutual interference between processes, each process has an independent address space and other resources. In addition to sharing global variables, they do not allow processes other than themselves to access the host address space. address , but threads of a process can share address space and resources. Threads are created to improve concurrency and satisfy inter-process cooperation.

system overhead

When canceling (creating) a process, the system needs to cancel (allocate) the PCB and other resources for it. The system overhead is larger than that of threads. When switching processes, context switching is involved. The thread switching cost of the same process is relatively small . Because it has fewer resources, but the thread switching overhead of different processes is high, because it will lead to process switching, because the thread synchronization and communication of the same thread are simpler than processes, because they share an address space and resources. In some OS, thread switching, synchronization and communication do not require the intervention of the OS kernel.

properties of threads

A process is already a non-executable entity , because after the thread is introduced, the thread is regarded as the basic unit of independent operation. A process is actually "executed" by a thread of the process. In addition, the imposition on the process is related to the state of the process The operation will also affect threads. For example, if a process is suspended, all threads of the process will be suspended.

Thread status and thread control blocks

There are also constraints on shared resources and mutual cooperation between processes, so there will also be discontinuities.

Three states (these three states are similar to process state switching, but thread switching does not require kernel operation)

1 execution state

2 ready state

3 blocking state

thread control block

The system configures a TCB for each thread, and all information about controlling and managing threads is placed in the TCB

Contains 1 thread identifier

2 sets of registers

3 thread execution status

4 priority

5 thread-specific memory, used to store on-site protection information and statistical information related to the thread when switching threads

6 signal jammer

7 stack pointer

How threads are implemented

user-level thread

Threads visible from user perspective

ULT is implemented in user space, and its series of operations on threads do not require kernel support because the kernel does not know the existence of its user threads.

For systems where ULT is set, the scheduling is based on processes ( because from the perspective of the kernel, these processes are single-threaded ). For example, when using the time rotation scheduling algorithm, each process takes turns to execute a time slice, which looks like It is fair to each process, but if process A has only one thread and process B has 100 threads, then A actually runs 100 times faster than B. However, if the KST system is set, the scheduling will be in thread units. , the time obtained by B is 199 times that of A, and process B can enable 100 threads to execute concurrently

advantage

1 Thread switching does not require conversion to kernel space

2 The scheduling algorithm can be dedicated to the process. In the case of not interfering with the OS system scheduling, different processes can choose different scheduling algorithms to manage the threads between them, regardless of the low-level algorithm of the OS

3 The implementation of user threads has nothing to do with the OS platform

shortcoming

1 The blocking problem of system calls, when a thread is blocked, all threads belonging to the process will be blocked

2 In the simple ULT implementation, multi-threaded applications cannot take advantage of multi-processor multi-processing, because the kernel only assigns one CPU to a process at a time, so only one thread can execute.

 

 

Kernel-level thread (kernel-level thread is the unit that the system allocates CPU)

Threads that can be seen from the perspective of the operating system kernel system

In OS, user processes and system processes all run with the support of the OS kernel and are closely related to the kernel. Similarly, kernel-level threads (KST) also run with the support of the kernel. Their creation and blocking are Undoing and switching are all implemented in the kernel space.

 

 

 advantage

1 In a multi-processor system, the kernel can schedule multiple threads of the same process to run in parallel.

2. If a thread in a process is blocked, the kernel can schedule other threads in the process to occupy the processor and run, or it can run threads in other processes.

3 cores support threads with very small data structures and stacks, all switching is fast and has little overhead

4 The core itself can also use multi-threading technology to improve the execution speed of the system.

shortcoming

For user threads, the cost of mode switching is relatively large. In the same process, switching from one thread to another requires execution from user mode to kernel mode.

The combination of two threads

Many-to-many threading method

Summarize

Chapter 3 Processor Scheduling and Deadlock

Why processors need scheduling

The essence of scheduling is actually the allocation of resources. Processor scheduling is the allocation of CPU resources. When resources are limited and there are a large number of tasks, so many tasks cannot be processed at the same time, so it is necessary to arrange the order in which these tasks are processed. This This is an issue that needs to be studied in scheduling.

Three levels of scheduling 

Advanced Scheduling (Job Scheduling)

His operation object is a job (a job contains a program) . According to a certain algorithm, it selects a suitable job from the backup queue and transfers it into the memory, creates a process for it, allocates necessary resources, and puts it into the ready queue. , advanced scheduling is mainly used in multi-channel batch processing systems, while time-sharing systems and real-time systems do not set advanced scheduling and transfer jobs from external storage to memory. 

The job will only be loaded into the memory once and loaded out once, which occurs less frequently.

Intermediate scheduling (memory scheduling)

The purpose of introducing intermediate scheduling is to improve memory utilization and system throughput, and transfer those processes that are temporarily unable to run to external storage to wait. The state at this time is called the suspended state. When the running conditions are met and the memory is slightly idle, Use intermediate scheduling to transfer from external memory to memory. Intermediate scheduling is equivalent to the swap function in memory management

Definition : According to a certain rule, select the appropriate process from the suspension queue and transfer its process into the memory, but the PCB of the process will not be transferred into the external memory.

Intermediate scheduling has a higher frequency than advanced scheduling

Low-level scheduling (process scheduling)

According to a certain rule, selecting a process from the ready queue to assign a processor to it is the most basic scheduling . It exists in multi-channel batch, time-sharing, and real-time systems. It is to put the process from the memory into the processor . What happens The frequency is also the highest

 Comparison of three types of transfers

Summarize

Job and role scheduling 

A job is a broader concept than a program. It not only includes programs and data, but also is equipped with a job instruction. The system controls the operation of the program based on the instruction. In a multi-channel batch processing system, the job is used as the basic Units are transferred from external storage into memory

Job Control Block (JCB)

He said that the logo of a job in the system saves all information about managing and scheduling jobs, including job logo, user name, user account, job type, job status, scheduling information, resource demand, and resource usage.

Every time a job is scheduled, two decisions need to be made: how many jobs to accept and which jobs to accept.

process scheduling 

When does process scheduling occur?

1 Actively give up the CPU 

The process terminates normally. An exception occurs during the running process and the terminated process actively requests blocking.

2 Passively give up CPU

When the time slice allocated to the process is used up, more urgent matters need to be processed, and a higher-level process enters the ready queue.

When can process scheduling and switching not occur?

1 During the process of handling interrupts 2 The critical section of the operating system kernel program, but the critical section can 3 During the atomic operation

Process scheduling of OS kernel program critical section and ordinary critical section

Process Scheduling Mode

When a process is running on a processor, there are more urgent processes that need to be processed, that is, processes with higher priority enter the ready queue. How to allocate processors at this time?

1Non-preemptive scheduling method

Once the processor is assigned to a process, it will continue to execute. The processor that will not preempt the process due to clock interruption or other reasons cannot passively force the CPU to give up.

Reasons for process scheduling: 1. The executing process completes normally. 2. The running process makes an I/O request and is paused.

3. Perform certain primitive operations during process communication or synchronization, such as the BLOCK primitive

2 Preemptive scheduling method

Allows the scheduler to suspend a running process according to a certain principle and allocate the CPU allocated to the process to another process

Principles to be followed Priority principle 2 Short process principle 3 Time slice principle

The difference between process scheduling and process switching 

 Therefore, generalized process scheduling includes 1. Saving CPU on-site information , which is the information of various CPU internal registers 2. Selecting a process according to a certain algorithm  is to select a process from the ready queue according to a certain algorithm and change its status to running status 3. Allocate the CPU To the process: Load the relevant CPU scene information in the PCB of the selected process into each register of the CPU, and give the control of the CPU to the process. So it can resume running from the last breakpoint

 Summarize

Measurement of Processor Scheduling Algorithms

Goals of Process Level Scheduling Algorithms

1CPU utilization

2System throughput (batch processing)

 Also consider turnaround time when batch processing

1Average turnaround time

Turnaround time = the time the job waits in the external memory backup queue + the time the process waits to be scheduled on the ready queue + the time it takes the process to execute on the CPU + the time it takes to wait for the I/O device to complete the operation (blocking)

2Average rights turnaround time

The time the I/O device is running is also considered the actual running time of the job.

 waiting time

When being served by an I/O device, although it is in a blocked state, the process is being served, so it is not counted in the waiting time. 

The waiting time of jobs and processes is different from
the response time (requirements in time-sharing real-time systems - timeliness)

 There are three parts: 1. The time from when the request information is input from the keyboard until it is sent to the processor; 2. The time when the processor processes the request; 3. The time when the response information is returned to the terminal display

important point

1. CPU utilization = busy time / total time
2. Turnaround time = completion time - arrival time
3. Weighted turnaround time = (completion time - arrival time) / running time
4. Waiting time = start running time - arrival time
5 . Response ratio = (waiting time + running time) / running time
= 1 + (waiting time / running time

(Test point) The difference between response time and turnaround time:
Response time: This is when the process gets a response.
For example: RR algorithm:
= waiting time (for example: previous process) + (first time) running time. (Not the running completion time of the process in the RR algorithm.)
Turnaround time = in the RR algorithm, the last running completion time of the process - arrival time.

Under batch OS: response time = turnaround time. (Non-preemptive)
Response time is generally used to evaluate time-sharing OS performance indicators.
Cutoff time is generally used to evaluate real-time OS.

Summarize

Scheduling Algorithm

 First come, first served (FCFS)

When used for jobs , the system uses the time when jobs arrive in the backup queue as the scheduling standard. The FCFS algorithm selects a few jobs that first enter the queue from the backup queue, transfers them into memory, and creates processes and allocates resources for them. , and finally put it into the ready queue

When used for processes,  the system uses the order in which processes arrive at the ready queue as the scheduling standard. The FCFS algorithm selects a process that first enters the queue from the ready queue, allocates the CPU to the process, and makes it run until the process has been running. Process scheduling will allocate the CPU to other processes when the operation is completed or an event is blocked.

 Short job first scheduling algorithm (SJF)

 The running time of the job must be known in advance. Each schedule selects the job that has arrived and has the shortest running time.

Non-preemptive SJF

Preemptive SJF

 Notes on SJF algorithm

High response ratio algorithm (HRRN)

Because the FCFS algorithm is not friendly to short jobs and only considers the job waiting time, SJF is not friendly to long jobs and only considers the job running time, and starvation may occur, so the HRRN algorithm was invented to consider the job waiting time and job running time to achieve relative fairness

 

Summarize 

 

 The above algorithms were used in early batch processing systems because they could not distinguish the urgency of tasks and had poor interactivity for users.

Round robin scheduling algorithm (RR algorithm)

The principle is to set a time slice for each process to run. When a process finishes executing a time slice, the CPU will be deprived of it. If a running process is completed before the time slice is used up, the process is immediately deleted from the ready queue. Then schedule the first process of the ready queue to run and start a new time slice. If a time slice is used up, the interrupt handler in the timer will be activated. At this time, the process has not finished running, and the scheduler will send it to the ready queue. the end of.

 The focus is on the arrangement of time slice size

 

priority scheduling algorithm 

Preemptive

non-preemptive 

 

type of priority 

Static priority (priority never changes) g

Based on 1. Process type  . System process priority is higher than that of general user processes. 2. Process demand for resources.   Processes with small resource requirements should be given high priority. 3. User requirements . Based on the urgency of the process and the amount of fees paid by the user.

dynamic priority 

The priority will be adjusted according to the situation. For example, it can be specified that the processes in the ready queue will increase as the waiting time increases.

Multi-level feedback queue scheduling algorithm

The above algorithms have their own advantages and disadvantages. FCFS has the advantage of being fair. SJF has the advantage of processing short jobs as quickly as possible, with excellent average turnaround time and waiting time. The RR algorithm can allow each process to respond in a timely manner, and the priority algorithm can be flexible. By adjusting the order of process services, more urgent tasks can be processed first, and the multi-level queue feedback algorithm neutralizes the above algorithm to obtain an excellent algorithm.

example

 

Comparison of three interactive algorithms

 

deadlock 

understand

 

Definition of deadlock

If each process in a group of processes is waiting for an event to occur that can only be caused by other processes in the group of processes, then the group of processes is deadlocked. A group of processes are waiting for each other to wait for resources, causing each process to Unable to move forward.

The difference between deadlock, starvation and infinite loop

Four necessary conditions for deadlock to occur

 1. Mutual exclusion condition . The process uses the allocated resource exclusively. Within a period of time, a certain resource can only be occupied by one process.

2 Request and hold conditions : The process has occupied a resource, but has made a new resource request, but the requested resource has been occupied by other processes. At this time, the requesting process is blocked, but it will not release the resources it already owns. (Eating from his own bowl and looking at others)

3 Non-preemptive conditions The resources that the process has acquired cannot be preempted by other processes before they are used up, and can only be released after completing the task

4 Circular waiting conditions are just like the dining philosophers problem above. When a deadlock occurs, there must be a "process-resource circular chain". However, if there is a deadlock, there must be a circular waiting chain, but if there is a circular waiting chain, there must be a deadlock. , there may be more than one resource of the same type

When does a deadlock occur?

Introduction to several resources

A reusable resource is a resource that can be reused multiple times by users and has the following properties:

1 Each unit in a reusable resource can only be allocated to one process and is not allowed to be shared by multiple processes. 

2. If a process wants to request a reusable resource, it first requests the resource. If the request fails, the process will block or wait in a loop, then use the resource, the process operates on the resource, and finally releases the resource.

3. The number of reusable resource units in the system is relatively fixed. A process cannot create or delete resources during operation.

Consumable resources   Consumable resources are also called temporary resources. They are dynamically created and consumed by the process while the process is running.

Preemptible resources mean that after such resources are obtained, such resources can be preempted by other processes or systems. Processors and memory are preemptible resources and will not cause deadlock.

Non-preemptive resources Non-preemptive resources mean that after such resources are assigned to a process, they cannot be taken back forcibly, but can only be released after the process is used up. Tape drives, printers, etc. are non-preemptive resources.

Competition for consumable resources can also cause deadlocks

Deadlock solution 

 Deadlock prevention

Break mutual exclusion condition

The key point is that the mutual exclusion condition is a must for non-shared devices. Not only cannot it be changed, but it should also be guaranteed. Therefore, when preventing deadlock, pay attention to destroying the three conditions after the deadlock occurs.

Destruction of request and hold conditions

The first protocol. Before all processes start running, they must apply for all the resources they need during the entire running process at one time.

This method will seriously waste resources, 1 seriously reduce resource utilization, 2 process will starve.

The second protocol only allows a process to start running after obtaining only the resources required for the initial stage of operation. During the running process, the process will gradually release all the resources that have been allocated to itself and have been used, and then apply for new ones. resource

Breaking the non-preemption condition

Option 1: When a process that has saved some non-preemptible resources makes a new resource request and cannot be satisfied, it must release all the resources it holds and apply again when needed in the future.

Option 2: When the resources required by a certain process are occupied, the required resources can be forcibly occupied with the assistance of the operating system.

 

 break the circular queue condition

The sequential resource allocation method can be used to linearly sort  all resource types in the system , assign them differences, and stipulate that each process can only request resources in the order of increasing sequence numbers.

If a process has applied for some resources with higher serial numbers and later wants to access some resources with lower serial numbers, it must release all resources with the same or higher serial numbers before it can apply for resources with lower serial numbers.

Avoid Deadlock (Banker's Algorithm)

what is security status

 The amount of a resource in the system is regarded as how much money the bank has. The needs of each customer are equivalent to the size of the resources occupied by each process. As long as you can find a method to safely borrow money like the one above, it is called a safe state. Borrowing money The method is called safety sequence. The system has more than one resource, so to implement the banker's algorithm, you need to convert the one-dimensional resource table into a multi-dimensional one.

, 

 If the system is in a safe state, deadlock will not occur, but if it is in an unsafe state, deadlock may occur

Implementation of Banker's Algorithm 

 

 Deadlock detection and release 

 Deadlock detection

If all edges can be eliminated, it is a safe state, and deadlock will definitely not occur at this time.

 If all the edges in the graph cannot be eliminated, it means that a deadlock has occurred. Now that a deadlock has been discovered, we must find a way to eliminate the deadlock.

 deadlock release

1 Seize resources 

2 terminate the deadlock process  

1 Terminate all resources

2 Terminate deadlock processes one by one (the process cost of each termination should be minimal)

1 Process priority 2 How long the process has been executed 3 The number of resources the process has used 4 Is the nature of the process interactive or batch processing 5 How much time will it take to finish

3 process rollback method

Summarize

Chapter 4 Synchronization of Processes

Process synchronization and mutual exclusion

Why introduce synchronization mechanism

Because of the introduction of processes, multiple programs in the system can be executed concurrently. Since concurrent execution leads to the asynchronous execution of programs, a synchronization mechanism is introduced to enable multiple processes to run in an orderly manner. If a synchronization mechanism is not adopted, these concurrent processes will cause harm to the system. Disorderly competition for system resources causes chaos to the system

Process Synchronization Concept

In an asynchronous environment, a group of concurrent processes can directly control and send messages to each other, cooperate with each other, wait for each other, and make a group of processes execute at a certain speed called process synchronization (the concept of process synchronization is a large category, collaborative process The constraint relationship between can be called process synchronization. According to different forms, it can be divided into synchronization relationship and mutual exclusion relationship. Mutual exclusion is a special case of synchronization. Synchronization emphasizes the sequence relationship between process operations, while mutual exclusion The emphasis is on mutually exclusive access to critical resources ),

The task of process synchronization is to coordinate multiple collaborative processes in the order of execution, so that many collaborative processes executing concurrently can share system resources according to certain rules or timing, and can cooperate well with each other, thereby improving the execution of the process . reproducible

Two forms of constraints

Direct restriction relationship (synchronous relationship)

In order to complete certain tasks, an application program will establish two or more processes . These processes cooperate with each other to complete the same task. The direct constraint relationship between processes comes from their cooperation, so this relationship can also be called Synchronization relationship

Synchronization is also called a direct constraint relationship.
In a multiprogramming environment, processes are executed concurrently, and different mutual constraints exist between different processes. In order to coordinate the mutual restriction relationship between processes, such as waiting, passing information, etc., the concept of process synchronization is introduced. Process synchronization is to solve the asynchronous problem of the process.
A simple example to understand this concept.
For example, let the system calculate 1 + 2x3, assuming that the system generates two processes: one is the addition process, and the other is the multiplication process. In order for the calculation results to be correct, the addition process must occur after the multiplication process. However, in fact, the operating system is asynchronous. If not restricted, it is absolutely possible for the addition process to occur before the multiplication process. Therefore, certain conditions must be formulated. Mechanism to constrain the addition process so that it occurs only after the multiplication process is completed.
 

Indirect mutual restriction relationship (mutually exclusive relationship)

Multiple programs are executed concurrently . Due to shared system resources, such as CPU, I/O devices, etc., these concurrently executed programs will form a mutually restrictive relationship. For system resources such as printers and tape drives, multiple Processes have mutually exclusive access to it, thus forming a so-called indirect mutual restriction relationship for the sharing of such resources among these processes, which is called a mutually exclusive relationship.

Mutually exclusive, also known as indirect restriction relationship. Process mutual exclusion means that when one process accesses a critical resource, another process that wants to access the critical resource must wait. The process that currently accesses the critical resource ends its access, and after the resource is released, another process can access the critical resource.

critical section

Here we need to review the concept of critical resources.
We call resources that are only allowed to be used by one process within a period of time as critical resources. Many physical devices (such as cameras and printers) are critical resources. In addition, there are many variables, data, memory buffers, etc. that are critical resources.
Access to critical resources must be mutually exclusive.
 

The critical section is the code segment where the process accesses critical resources.

Four Principles for Implementing Critical Sections

Free to let in

If no process is in the critical section, it indicates that the critical resource is idle, and a process immediately enters the critical section and can use the critical resource

If you are busy, wait

If there are already processes using resources in the critical area, other processes trying to enter the critical area must wait for the used process to end access to the critical area resources before entering the critical area to ensure mutually exclusive access to critical resources

limited wait

Processes that require waiting to enter the critical area should ensure that they enter their own critical area within a limited time, so as not to fall into a dead wait.

Give up the right to wait (in principle, to follow, but not necessary)

It is required that when the process cannot enter its own critical section, the CPU should be released immediately to prevent the process from falling into a busy waiting state

Summarize

Software synchronization mechanism

The idea of ​​implementing the method

  • The idea of ​​the software implementation method: set and check some flags in the entry area to indicate whether there is a process in the critical area. If there is already a process in the critical area, wait in the entry area through loop checking, and exit after the process leaves the critical area. District modification flag.入区通过循环检查进行等待,进程离开临界区后则在退出区修改标志。

单标准法

violation of free time

 Double sign method​​​​​​​​​​​​ (to solve the problem of free entry)

                                                     But it will violate busy-then-wait

Double-flag post-checking method (wait to solve busy situation)

                                                    Violates idle giving and finite waiting

Peterson algorithm

Violates the principle of giving up power and waiting 

 According to the order of 123678, 1 means that P0 is willing to enter the critical area.

2 means that you can let P1 enter the critical area first

3 Since it expresses the willingness to let the other party advance but the other party has no intention to enter, P0 enters the critical area.

6P1 has the intention to enter the critical area

7 indicates the willingness to let the other party advance

8 Because the other party is willing to enter and I am also willing to let the other party advance, the cycle continues.

According to the order of 1623,  1 indicates that P0 wants to enter the resources of the critical section.

6 P1 also expressed that he wanted to enter the resources in the critical area

2P0 means that the opponent's P1 can advance

3 Since the other party is willing to enter the critical section and he is also willing to let the other party advance, he keeps waiting in a loop until the time slice of P0 is used up, and then executes P1.

  7 means that P0 can be advanced

8 Since the other party is willing to enter the critical area and also expresses that it will allow the other party to advance, the cycle continues until the time slice is exhausted.

Then execute 3. Since the process turn=0, process P0 has no intention to let the other party advance, so it jumps out of the loop and enters the critical section.

Summarize

Hardware synchronization mechanism 

interrupt hiding method

Implemented using on and off interrupts


shortcoming 

1This method is not suitable for multi-CPU systems , because turning off interrupts on one CPU does not prevent the execution of the same critical section code on other CPUs. 

2 is only applicable to the system kernel process (because the interrupt mechanism is a privileged function and can only be implemented in the kernel state)

3. Excessively long interrupt time will affect system efficiency, which in turn will affect the ability of CPU cross-execution

TestAndSet directive

When executing the TSL instruction, its internal operating logic:
Assume that lock is now false, which means critical resource A is idle, then I can access this resource, and set lock=true to remind other processes that I am using this critical resource A. Let them wait.
Assume that lock is true, which means that the critical resource is being used by someone, so I have to wait, and setting lock=true does not affect anything, so it doesn't matter. It is just to allow locking when lock is false. The check is done in a TSL instruction.

 The TS instruction is completed in hardware and is completed in one go. The execution process cannot be interrupted. However, if the wait is still not satisfied, busy waiting will occur.

Swap command

old is a step that every process must perform. You must set old=true
to analyze the reason for this:
because lock is a shared variable of a specific critical resource. When each process is ready to access this specific critical resource, it is initialized. old=true, and then enters the while loop for exchange. If the current lock is false, after the exchange old=false, the current process can jump out of the loop and enter the critical section code section. At the same time, due to the exchange, lock=old=true is locked and is not allowed to If other processes come to disturb, other processes will wait in the while loop because lock becomes true. When I finish using the critical resources, I will set lock=false. At this time, other processes can judge old= by exchanging old and lock. false, you can break out of the loop and use critical resources.

 Because Swap is also completed by hardware, it is completed in one go and cannot be interrupted. However, there will still be a phenomenon of unsatisfied transfer of rights and waiting.

Summarize

semaphore mechanism 

Why was the semaphore mechanism introduced?

Because none of the above methods can perfectly solve the critical section problem 

What is the semaphore mechanism

Several semaphore forms 

Shaped semaphore

wait(s) and signal(s) are atomic operations and cannot be interrupted 

record semaphore

How to achieve

Example

 Let’s sort out the knowledge points of record semaphores (P, V)

This is the best way to achieve process synchronization 

Summarize

Application of semaphore mechanism 

Implement process mutual exclusion

Process mutual exclusion is to achieve mutual exclusion of access to critical resources by each process. Why is it called critical resources? This kind of resource can only be used by one process in a period of time, which is equivalent to the number of resources being 1, so the mutual exclusion signal is The value of mutex is set to 1.

step

1 Set mutex as a mutual exclusion semaphore, its initial value is 1 , and the range of mutex is (-1, 0, 1) . When it is 1 , it means that no one accesses the resource in the critical area, and it can be used directly. When it is 0 , it means There is a process running in the critical area, and the other must wait. When it is hung in the blocking queue, when it is -1, it means that one process is running in the critical area, and the other process is blocked in the semaphore queue because of waiting. It needs to be blocked by the current Wake up when a process running in a critical section exits

2. Perform P operation before entering the critical section.

3. Perform V operation after exiting the critical section

Set different mutually exclusive semaphores for different critical section resources

 After the lack of P operation , the number of resources will not decrease, which is equivalent to not being able to lock the critical section, so mutual exclusive access to the critical section cannot be guaranteed

The lack of V operation means that the resource number is not returned to 1, which is equivalent to not unlocking the critical section, so the process that enters the blocked state cannot be awakened.

Implement synchronization operations

To realize synchronization operation, multiple processes can advance in an orderly manner in order to achieve the same task (program).

Synchronization is to realize the operations that need to be implemented sequentially between processes 

step 

1Set the synchronization semaphore S and set the initial value to 0

2 After the previous operation, implement the V operation

3. Complete the P operation before the subsequent operation.

The hex of the implementation process realizes the relationship between the predecessors

Set a synchronization variable for each predecessor relationship

 Summarize

Several classic relationships between process mutual exclusion and synchronization

producer-consumer problem

1) Problem description

  • 一组生产者进程There are sums in the system 一组消费者进程, the producer process 生产一个puts the product into the buffer every time, and the consumer process produces and uses it from the buffer every time 取出一个. (Note: The "product" here is understood as some kind of data)
  • The producer and consumer 共享are initially empty and of size n 缓冲区.
  • The product can be buffered only 没满when the buffer is available , otherwise it must wait.生产者放入
  • Only 不空when the buffer is available 消费者can products be made from it 取出, otherwise it must wait.
  • The buffer is a critical resource that must 互斥be accessed by each process.

analyze 

1 Find each process and analyze the synchronization and mutual exclusion relationships between them

2 Organize your thoughts

3Set the semaphore

The producer process of a synchronization relationship must precede the consumer process

Mutually exclusive relationship: producers and consumers must have mutually exclusive access to the buffer

The number of free buffers is limited synchronization

Product quantity is limited and synchronized

Limited number of buffers mutually exclusive

Implementation 

The mutually exclusive P operation must be implemented after the synchronous P operation. 

 However, the order of mutually exclusive and synchronous V operations will not cause the process to block, so the order can be exchanged.

multi-producer-consumer problem

Problem Description

 (2) Problem analysis

 3 has a mutex

 No mutex

③ Why is it the same with mutex and without mutex?

  • The reason is: the buffer size in this question is 1. At any time, at most one of the three synchronization semaphores apple, orange, and plate is 1. Therefore, at any time, the P operation of at most one process will not be blocked and enter the critical section smoothly...

 

(4) Knowledge summary and important test points

  • 总结:In the producer_consumer problem, if the buffer size is 1, then it is possible to achieve mutually exclusive access to the buffer without setting a mutually exclusive semaphore. Of course, 这不是绝对的specific issues need to be analyzed in detail.
  • 建议: If you don’t have time to analyze it carefully during the exam, you can add a mutually exclusive semaphore to ensure that each process will access the buffer mutually. But it should be noted that · 实现互斥的P操作一定要在实现同步的P操作之后·, otherwise it may cause · “死锁”·.

reader problem

insert image description here

(2) Problem analysis

insert image description here The core problem is solved. Reading and reading are not mutually exclusive, but mutual exclusion between reading and writing introduces a count counter.

(3) Implementation method

① Add mutex mutually exclusive access to count

  • Let’s talk about why we need to add mutex.
  • For example: when count=0, the first reader process executes to p(rw), rw=0, assuming that the time slice is up at this time, switch to the second reader process, and the second process finds that count=0, then execute p(rw), but at this time rw=0, so the second process is blocked in p(rw), similarly, there may be multiple processes blocked in p(rw), only when the first process Get the time slice again, execute count++, let the count not be 0, then other processes can directly bypass the if and directly perform count++ to access the file, but the third reader process and the following ones may be blocked in p(rw) A reader process must wait until count- reaches 0 before it can compete with the writing process again to access the file. The access to count is not done in one go, which will cause some processes to be blocked in p(rw).

insert image description here

 ② Add a w to implement the "reading and writing fairness method"

  • In the above algorithm, the reading process is given priority, that is, when there is a reading process, the write operation will be delayed, and as long as one reading process is active, subsequent reading processes will be allowed to access the file. This method will cause the writing process to wait for a long time, and there will be “饿死”situations where the writing process is blocked.
  • If you want the writing process to take priority, 即当有读进程正在读共享文件时,有写进程请求访问,这时应禁止后续读进程的请求,等到已在共享文件的读进程执行完毕,立即让写进程执行,只有在无写进程执行的情况下才允许读进程再次运行. To this end, by adding a semaphore and a pair of PV operations in the writer() and reader() functions of the above program, you can get a solution that prioritizes the writing process.
  • 4) Knowledge review and important test points

Monitor mechanism 

Why introduce the monitor

Although semaphores are a convenient and effective process synchronization mechanism, each process that wants to access critical resources must synchronize operations P and S. In this way, a large number of synchronization operations are scattered among various processes, which not only affects the process ' Management brings trouble, and improper synchronization operations may lead to system deadlock , so the encapsulation idea of ​​pipe process is introduced.

 The composition of the tube

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

3. The tube process implements the producer-consumer problem
insert image description here

insert image description here

 Features of the tubeinsert image description here

Java process thinking

1

 Summarize

insert image description here

Chapter 5 Memory Management 

Multi-layer structure of memory

Basic three layers 

1CPU register

2 main registers

3 auxiliary registers

 executable register                

In computer memory systems, registers and main memory are called executable registers. The information placed in them is accessed in different ways than the information placed in auxiliary storage, and the time it takes is also different. For storage and memory The information in the executable memory can be accessed in a very short clock cycle using a load and store instruction, but the information in the auxiliary memory must be accessed through the I/O device.                                                                                                 

main memory             

Also known as main memory and memory, also known as executable memory, it is the main component of the computer system and is used to save programs and data while the process is running. Usually the processor will fetch instructions and data from the memory, put the corresponding data into the data register, put the read instructions into the instruction register, or perform the opposite operation. However, the memory access speed is much slower than the CPU's instruction execution speed. Based on this contradiction, caches and registers were introduced.       

register

The register is a small storage area inside the CPU, which is used to temporarily store instructions, data and operation results involved in operations. The register has the same speed as the CPU and can fully coordinate with the CPU. 

 cache       

It is a memory between registers and memory. It uses the principle of program locality to back up some commonly used data to reduce the number of accesses to memory by the processor, which can greatly improve the execution speed of the program.         

disk cache

Since the current I/O speed is far lower than the access speed to memory, in order to alleviate the problem of speed mismatch between the two, a disk cache is set up, which is mainly used to temporarily store a part of frequently used disk data to reduce the time required to access the disk. times, but disk cache is different from cache. It is not an actual memory, but uses part of the storage space in the memory to temporarily store information read or written from the disk. 

storage unit

insert image description here

Programs must be placed in memory before they can be processed by the CPU

 Several common quantity units & memory addressesinsert image description here

 How does the OS manage the memory system?

1 Responsible for the allocation and recovery of memory space 2 Need to provide some technology to logically expand the memory space 3 Address translation 4 Memory protection

Basic principles of command execution

(1) How the instruction works

—Operation code + several parameters (may include address parameters)

Let’s take a rough look at the execution process of the instruction from X=X+1
insert image description here
insert image description here
insert image description here
insert image description here

Logical address and physical address

insert image description here

Compile, link and load 

insert image description here

 compile

Turn the code we write into instructions (into machine language) into a language that computers can understand, forming several target modules

Link

The linker links the compiled set of modules and the library functions it requires together to form a complete logic

Three link methods

1Static link

Before the program is run, each module and library function is linked into a complete assembly module and will not be disassembled later.

Modify relative addresses and transform external call symbols

insert image description here

2Dynamic linking at load time

It is linked while loading during runtime, and is linked directly in the memory. When loading the memory, if an external module call event occurs, the loader will be introduced to find the corresponding external target module and load it into the memory.

Easy to modify and update, easy to share the target module

insert image description here

3Dynamic linking at runtime

When the application is running, the modules to be run may be different each time. However, since it is impossible to know in advance which modules will be run this time, all the modules that may be run can only be loaded into the memory and linked together when loading. Together, this is inefficient

Therefore, runtime dynamic linking is used to defer the linking of certain modules until the program is executed.

insert image description here

 Three kinds of loading

Also called loading, the loader loads the load module into memory and forms a physical address after loading.

Note on loading into memory

If you load it directly into the memory without modifying the instruction address in the loaded module:
insert image description here
insert image description here

Three ways to load

1Absolutely mount

insert image description here

2Static relocation loading method 

The addresses used in instructions and the addresses where data is stored are all logical addresses relative to the start and end addresses.

Absolute loading can only load the target module into a specified location in the memory. This is only applicable to a single-programming environment. In a multi-programming environment, the compiler cannot know in advance where the compiled target module will be placed. Where is the memory, so use the static relocation loading method

insert image description here

3Dynamic relocation 

Its implementation relies on a relocation register (which stores the starting position of the loaded module)

The static relocation loading method can load the load module into all locations in the memory and can be used in a multiprogramming environment. However, this method does not allow the program to move the location in the memory when allowed. In order to solve this problem, dynamic run relocation is used. Positioning method

insert image description here

insert image description here

insert image description here

 Summarize

insert image description here

How does the operating system manage memory? 

Allocation and recycling of memory space

insert image description here

Expansion of memory space (implementing virtuality)insert image description here

address translation

insert image description here

three ways

insert image description here

memory protection

Ensure that each process has a separate memory space

insert image description here

 two ways

The first method is to set the upper and lower limit registers

insert image description here

 Set the base address register and limit register

insert image description here

 Summarize

insert image description here

Swapping and overlay technology 

coverage technology

In order to make the size of the process larger than the memory space it allocates, you can use overlay technology. His idea is to only store the required instructions and data in memory at any time. When other instructions and data are needed, they will be The memory space occupied by instructions and data that are just not needed is loaded

insert image description here

 insert image description here

swap technology 

The introduction of swaps

In a multiprogramming environment, some processes in the memory may be blocked because some events have not yet occurred, but they will occupy a large amount of memory. In order to solve this problem, swap technology is introduced, which is the corresponding hang. state

Thought

It refers to transferring the temporarily unavailable processes or temporarily unused programs and data in the memory to the external memory in order to free up enough memory space, and then storing the programs and data required by the processes or processes that are already capable of running into the memory. , and then realize the exchange

insert image description here

insert image description here

insert image description here Type of swap

 The intermediate scheduling of the overall swap  processor is actually the memory swap function. The purpose is to solve the memory shortage problem and improve the memory utilization and system throughput . In the intermediate scheduling, the swap is based on the entire process, so Also known as process swap

Page (segment swapping) If the swap is based on a page or segment unit of the process, it is the basis for request paging and segmented storage management.

With the swap function, the disk space is divided into

file area

The file area occupies most of the disk space and is used to store various files. Since files will be stored in external memory for a long time and the frequency of system access is low, the management of the file area is to improve the utilization of file storage space. The next step is to improve the speed of accessing files, so the access management method of discrete allocation should be adopted.

swap area

The swap area only occupies a small part of the disk space and is used to store processes swapped out from the memory. Since these processes are in external memory for a short period of time, the frequency of swap operations is high, so the main purpose of the swap area is The purpose is to improve the speed of memory access, and secondly to improve the storage space utilization of files. Therefore, the management of the swap area adopts the continuous allocation storage management method.

Process swapping out and swapping in

Swap out the selection process 1. First select which processes are blocked or sleeping. 2. When there are multiple such processes, select the process with the lowest priority as the swapped out process. 3. For low-priority processes to be transferred into memory quickly It will be transferred into the memory soon and then transferred out 4 If there is no blocking process, but the memory space still does not meet the needs, select the ready process with the lowest priority to swap out

The steps of swapping out a process will only swap out the non-shared programs and data segments of the process. Shared programs and data will not be swapped out as long as the process needs them, and the process PCB will not be swapped out and will remain in the memory .

Process swapping 

1. Find the process that is in the ready state but has been swapped out. 2. When there are many such processes, select the process that has been on the disk for the longest time as the swap-in process and apply for memory space for it.

Timing to start swap

The swap program will not be started when the processor is running normally, but if it is found that many processes are running with page faults and the memory is tight, the swap program will be started until the page fault rate of all processes has been reduced. Significantly reduced, when the system throughput drops, the swap process can be suspended.

Summarize

insert image description here

 insert image description here

Contiguous allocation storage management

Contiguous allocation storage management

This method is to allocate a continuous memory space to a user process, that is, the code and data in the program are logically adjacent and physically adjacent.

​​​Single continuous allocation

Used for single-pass batch processing stages where only one process is running

insert image description here

 Fixed partition allocation

Divide the entire user space into several fixed-size partitions, and transfer only one job to each partition in order to load multiple jobs into the memory.

The partition sizes are equal in both ways. The partition sizes are unequal.

insert image description here

Fixed partition data structure---- fixed partition usage table  

insert image description here

3. Dynamic partition allocation (variable partition allocation)

Dynamically allocate memory space according to the actual needs of the process

insert image description here

insert image description here

insert image description here

What data structure should the system use to record memory usage? ----Dynamically allocated data structure

insert image description here

 dynamic partitioning algorithm

When multiple free partitions can meet the requirements, which partition should be selected for allocation? --Dynamic partitioning algorithm

insert image description here

first fit algorithm

Start searching from a low address each time and find the first free partition that can meet the size.

insert image description here

insert image description here

insert image description here

 best fit algorithm

The free partitions are linked in increasing order of capacity, and each time memory is allocated, the free partition chain or free partition table is sequentially searched to find a free partition that can satisfy

insert image description here

insert image description here

insert image description here

3. Worst (large) adaptation algorithm

Free partitions are linked in descending order of capacity, and each time memory is allocated, the free partition chain or free partition table is sequentially searched to find the first free partition with a size that satisfies the

insert image description here
insert image description here
insert image description here
insert image description here

4. Proximity adaptation algorithm (cyclic first adaptation)

When allocating memory space for a process, we no longer start searching from the beginning of the chain every time, but start searching from the next free partition after the last free partition until we can find a free partition that meets the requirements.

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Inductive comparison of four algorithms

insert image description here

Partition allocation and deallocation operations

How to distribute?
insert image description here
insert image description here
insert image description here
How to recycle? Corresponding to four recycling situations
insert image description here
insert image description here
insert image description here
insert image description here

4. Internal fragmentation and external fragmentation

 insert image description here

 insert image description here

compact technology 

Solve the problem of external fragmentation, splicing many small partitions that were originally scattered into one large partition, so that jobs can be loaded into partitions, and the moved data and programs need to be relocated after each compaction

Dynamic relocation is implemented using the relocation register. The real address is the relative address plus the data in the relocation. The relocation register stores the starting address of the program in memory.

Dynamic relocation partition allocation algorithm

The compact function was added to the original dynamic relocation algorithm. When the algorithm cannot find a free partition large enough to meet the demand, if the total capacity of all small free partitions is greater than or equal to the user's requirements, it will be compacted. If it is less than, A failure message is returned.

Paging storage management method

Discontinuous Allocation Management

insert image description here

Why Paging Storage

To resolve external and internal fragmentation, fully utilize memoryinsert image description here

 basic idea

The paging storage management method divides the address space of the process into several pages, called pages , and numbers each page, starting from 0, and correspondingly divides the memory space into several blocks , called page frames or physical blocks , and Also numbered starting from 0, memory is allocated to the process in blocks.

insert image description here

 Some points to note

name 

Page size settings

insert image description here

 How to perform address translation

Know the physical block number and page offset corresponding to the page number. The starting address of the physical block number plus the page offset is the actual address.

insert image description here

 insert image description here

insert image description here

 How to calculate page number and page offset

 insert image description here

Why is the page size generally set to an integer power of 2? 

Convenient calculation, it is easy to convert binary to page number and page offset

If the page size is set too large , although the length of the page table can be reduced and the efficiency of page swapping in and out can be improved, but choosing a page size that is too large will make the fragments in the page too large. It cannot be too small . Although being small can reduce the size of page fragments and improve memory utilization, it will cause each process to occupy too large a page, causing the page table to be too long, taking up a lot of memory, and also reducing page swapping. Output efficiency

insert image description here

insert image description here

 Logical structure of paging storage

insert image description here

 How to know the starting address of a page in memory

Data structure for paging management----page tableinsert image description here

insert image description here

 The process page table is usually stored in a continuous memory block. For the setting of the size of the page table entry, it usually makes one page table entry occupy more bytes, so that each page can just hold an integer number of page table entries

Summarize

insert image description here

Address Change Mechanism for Paged Memory Management 

insert image description here

 The basic address change mechanism is implemented by hardware

1. Page table register

insert image description here

Address translation process

insert image description here

insert image description here
insert image description here
An example question to deepen the impression:
insert image description here

Further discussion of page table entries

insert image description here

Summarize

insert image description here

 Address change authority with fast table

insert image description here

The principle of locality introduces the fast table mechanism

insert image description here It is equivalent to using Cache cache

2. Fast table (TLB)

insert image description here

An example diagram to understand the address translation structure based on fast table

insert image description here
After the fast table is introduced, the text description of the address transformation process:
insert image description here

Introducing the effective access time of fast tables

3. Comparison of basic address transformation and fast table address transformation

insert image description here

Secondary page table

The principle is the matryoshka doll idea of ​​the ultimate principle of computers. 

Why introduce a secondary page table?

Because there are some problems with single-level page tables, there are two problems with introducing two-level page tables and multi-level page tables:

1. In order to solve the problem that page tables need to be stored continuously, sometimes the page table is too large to be stored in one physical block, so a secondary page table is used.

2 According to the principle of locality, there is no need to keep the entire page table in memory, because a process will only access a few specific pages in a period of time

insert image description here
insert image description here
These two problems were mentioned above, so let’s summarize them and propose solutions, introducing the concept of secondary page tables.
insert image description here

2. The principle and address structure of the secondary page table

  • Group the page table again. Compared with first-level paging, there is an additional page directory table, also called the top-level page table. The logical address used to store the page table corresponds to the physical address.
    insert image description here
  • The address structure and corresponding relationship of the secondary page table
    insert image description here
  • 3. How to implement address transformation of the secondary page table?

    insert image description here
    We have solved problem one in the above part, and next is problem two. Here is a brief description, and subsequent articles will continue to analyze it in depth. To solve the second problem, virtual memory needs to be solved.
    insert image description here

  • A few small details

  • insert image description here

    Summarize

  • insert image description here

Segmented storage management method 

insert image description here

 A segmented segment is a logical unit while a page is a physical unit The address of a page is one-dimensional while the address of a segment is two-dimensional

purpose of segmentation

1 To facilitate programming , users can divide it into multiple segments according to the logical relationship of their own jobs. Each segment is addressed starting with 0 and has its own name and length. Therefore, programmers urgently need to access the logical address consisting of the segment name and segment name. Determined by the internal address offset, this not only facilitates programming by programmers, but also makes the program more readable and intuitive.

2 Information sharing realizes the sharing of programs and data based on the logical unit of information. A page in the paging system is only a physical unit and does not have a complete logical relationship, so a process that can be shared may occupy several pages. , which increases the difficulty of sharing, and segmentation is a logical unit of information, so the shared data is a segment, which is very convenient for sharing.

3. Information protection Information protection is also based on the logic of information. All segments are easier to achieve information protection.

4. Dynamic linking In order to improve memory utilization, the system only loads the target programs that are actually run into the memory. In other words, dynamic linking does not link all the target programs before the job is run. This target program will transfer the segment into the memory for linking. It can be seen that the unit of dynamic linking is segment.

5 Dynamic Growth In actual applications, there are often some segments, especially data segments. As data continues to grow, data segments will grow dynamically, and the corresponding storage space will also grow dynamically. The segmentation management method will be very smooth. solve this problem

1. What is segmentation?

insert image description here

Segmented logical address structure

insert image description here

2. Segment table

insert image description here

3.Address transformation

insert image description here

insert image description here

Here, you need to pay attention to the out-of-bounds interruption, because the segment number is numbered from zero, so the length of the segment table is at least 1, and it is out-of-bounds when the segment number is less than the segment length

.Comparison of segmentation and paging management

insert image description here
insert image description here

Segmented sharing of information

Sharing can be achieved by just letting the segment table entries of each process point to the same segment.

insert image description here

Why is paging inconvenient for information sharing and protection?insert image description here

Summarize

insert image description here

Segmented page storage management method 

insert image description here

1. Analysis of the advantages and disadvantages of paging and segmentation

insert image description here
insert image description here

2. Segmentation + paging = segmentation and page management

Divide the user program into several segments first, then divide each segment into several pages, and assign a segment name to each segment

insert image description here

Logical address structure of segment page management

The segment page format is still two-dimensional, and the segments are visible to the user, but the page number and intra-page address are automatically divided into segment addresses by the hardware.

insert image description here

Segment table and page table of segment page storage

insert image description here

3. Three memory accesses in the address translation process of segment page management

insert image description here

Guess you like

Origin blog.csdn.net/qq_50985215/article/details/122120354