Operating System 3 - Processor Scheduling and Deadlock

This series of blogs focuses on sorting out the core content of the operating system course of Shenzhen University, and the bibliography "Computer Operating System" (if you have any questions, please discuss and point out in the comment area, or contact me directly by private message).


 

synopsis

This blog mainly introduces the relevant knowledge of processor scheduling and deadlock in the third chapter of the operating system.

Table of contents

1. Basic concept of scheduling

1. Processor scheduling level

2. Scheduling principles and evaluation criteria

2. Scheduling algorithm

1. First come, first served (FCFS)

2. Short job (process) first (SF) scheduling algorithm

3. High Response Ratio Priority (HRN) Scheduling Algorithm

4. Highest Priority (HPF) Scheduling Algorithm 

5. Time slice round-robin (RR) scheduling algorithm

6. Multi-level queue scheduling algorithm

7. Multi-level feedback queue scheduling algorithm 

3. Real-time scheduling

1. Time slice round-robin scheduling algorithm

2. Non-preemptive priority scheduling algorithm

3. Preemptive priority scheduling algorithm based on time interrupt 

4. Immediately preemptive priority scheduling algorithm

5. Earliest Start Deadline First (EDF) Algorithm

6. Lowest Laxity First (LLF) Algorithm

4. Multiprocessor System Scheduling - Understand

Five, deadlock

1. Deadlock overview

2. Prevent deadlock

3. Avoid Deadlock - Banker's Algorithm

4. Deadlock detection and release


Let’s start with a small example:

If the system has only one cpu, and there are two processes to run. The running time of process A will be 1 hour, and the running time of process B will be 1 minute. So think it is reasonable for the operating system to let which process run first?

A. Let the long process A run first

B. Let the process B with the shortest time run first

C. It doesn't matter, it's all the same

D. It doesn’t matter what time it is, it’s best for the two to run in turn for a while

E. Well, I heard that the price of soy sauce is going to increase

Summary: It is related to the scheduling strategy of the operating system, that is, more emphasis is placed on factors such as priority, order of arrival, and duration.

1. Basic concept of scheduling

Scheduling is the allocation of processor (CPU) resources.

1. Processor scheduling level

The processor scheduling level is divided into high-level scheduling (long-range scheduling/job scheduling), low-level scheduling (short-range scheduling/process scheduling), and intermediate scheduling (memory scheduling). Different scheduling is mainly distinguished by operating frequency, and process scheduling is the most frequent, which is also the focus of research , the architecture of different schedules is as follows:

  • Advanced Scheduling: Decide which out-of-storage jobs are transferred into the memory and create processes and allocate resources , that is, one or a batch of jobs changes from the standby state to the running state.
  • Intermediate Scheduling: Deciding which processes can participate in competing for the CPU is actually to implement "suspend" and "activate" operations, mainly used to improve memory utilization and system throughput.
  • Low-level scheduling: Decide which process can get the CPU , pick a process from the active ready queue, turn it into the running state, and start the CPU to execute the process at the same time, which is a necessary scheduling in different systems.

Tips: There are generally two ways of process (low-level) scheduling:

1. Non-preemptive mode: After the process obtains the CPU, it will continue to execute or block automatically

2. Preemption method: After the process obtains the CPU, other processes are allowed to seize the CPU resources according to certain principles.

(1) Time slice principle (2) Priority principle (3) Short job (process) priority principle

The scheduling queue model with different scheduling levels is as follows:

1. Scheduling queue model with only low-level scheduling :

When each process is executed: (1) The process has completed within the time slice and enters the completed state (2) Not completed, it is placed after the ready queue (3) Due to an event, it enters the blocked state

2. Scheduling queue model with high-level and low-level process scheduling

3. Scheduling queue model with three-level scheduling :  

2. Scheduling principles and evaluation criteria

In different operating systems, the selection of scheduling strategies and algorithms depends on the type and design goals of the operating system.

The common goals of processing and scheduling algorithms are generally resource utilization, fairness, balance, and policy enforcement (security policy).

Tips: CPU utilization is CPU effective working time/CPU effective working time+CPU idle waiting time.

The evaluation criteria are generally different types of turnaround times, as follows:

1. Turnaround time :

The time from submission (entry moment) to completion of job (process) i is called the turnaround time Ti of the job

Ti = Time of Completion – Time of Entry

2. Average turnaround time:

The average turnaround time is the average of n job (process) turnaround times

T=\frac{1}{n}\left[\sum_{i=1}^{n} T_{i}\right]

3. Turnaround time with entitlement:

The ratio of the job (process) turnaround time Ti to the actual running time Tsi is called the weighted turnaround time Wi of the job

W_{i}=\frac{T_{i}}{T_{s i}}

4. Average turnaround time with rights:

The average weighted turnaround time is the average of the weighted turnaround times of n jobs (processes)

W=\frac{1}{n}\left[\sum_{i=1}^{n} \frac{T_{i}}{T_{s i}}\right]

Scheduling principles can generally be divided into two types according to objects:

1. For users:

Guaranteed Priority Criteria for Short Turnaround Time Fast Response Time Deadline

2. System-oriented:

High system throughput, good processor utilization, balanced use of various resources

2. Scheduling algorithm

Scheduling algorithm refers to the resource allocation algorithm stipulated according to the resource allocation strategy of the system

1. First come, first served (FCFS)

Core: Select the first job (process) to be scheduled.

Job scheduling: From the backup queue, select one or more jobs that enter the queue first

Process scheduling: From the ready queue, select a process that enters the queue first, assign the processor to it, and run until it runs out or automatically blocks

Summary: The advantages and disadvantages of the non-preemptive method are as follows:

  • Advantages: Simple, good for CPU-busy jobs (processes), good for long-time jobs (processes)
  • Disadvantages: bad for short-term jobs (processes), bad for I/O busy jobs (processes), bad for urgent jobs (processes) 

2. Short job (process) first (SF) scheduling algorithm

Core: Select the job (process) to be scheduled with the shortest estimated time.

Job scheduling: From the backup queue, select one or more jobs with the shortest estimated execution time

Process scheduling: From the ready queue, select a process that is estimated to require the shortest CPU time, and assign the processor to it

Summary: It can work in preemptive and non-preemptive modes. The advantages and disadvantages are as follows:

  • Advantages: Favorable for short-duration jobs (processes) In the preemptive mode, the shortest-duration jobs (processes) will be completed at the fastest speed
  • Disadvantages: ⑴, unfavorable for long-time jobs (processes), ⑵, without considering the urgency of jobs (processes), ⑶, in the preemptive mode, the shortest refers to the shortest total time required or the shortest remaining time (and is an estimated value) ⑷, in preemption In this way, even if a long job (process) is running, it may be delayed for a long time

3. High Response Ratio Priority (HRN) Scheduling Algorithm

Tips: Required service time = running time 

Core: Select the job (process) to be scheduled with the highest response ratio.

Summary: In the non-preemptive mode, when the waiting time is the same, it becomes SF, and when the running time is the same, it becomes FCFS, which achieves a good compromise. The advantages and disadvantages are as follows:

  • Advantages: Favorable for short-term jobs (processes) Favorable for first comers
  • Disadvantages: Rp must be calculated before each scheduling, increasing system overhead without considering the urgency of the job (process)

4. Highest Priority (HPF) Scheduling Algorithm 

Core: Select the job (process) with the highest priority to be scheduled.

Among them, the highest priority process scheduling method is divided into non-preemptive and preemptive, and the introduction is as follows:
1. Non-preemptive method of process scheduling : once the system allocates the CPU, it will continue until the process execution is completed or automatically enters the blocking state

2. The preemption method of process scheduling : as long as the priority of the process entering the ready queue is higher than the priority of the current process running on the CPU, the current process is placed at the end of the ready queue and the highest priority process is run

Similarly, the highest priority priority design is divided into dynamic and static, the brief introduction is as follows:

1. Static priority:

⑴ Assign the value before the job (process) is scheduled, and it will remain unchanged after that

(2) The value of the priority can be based on: job (process) type, resource requirements, user requirements, etc.

2. Dynamic priority:

⑴. Assign the initial value before the job (process) is scheduled

(2) Dynamically change the priority during the running of the job (process)

Summary: It can be used for job and process scheduling, but the dynamic priority algorithm is only used for process scheduling, and the preemptive method can be used for real-time systems. If the non-preemptive method is adopted (1), the priority is only based on the arrival time, then it becomes FCFS (2) and the priority Only based on the running time, it becomes SF ⑶, and the priority only depends on Rp, then it becomes HRN. The advantages and disadvantages are as follows:

  • Advantages: According to the requirements, the execution of the job (process) with the highest priority for the system and users can be taken care of
  • Disadvantages: the calculation of priority may be more complicated, increasing system overhead

5. Time slice round-robin (RR) scheduling algorithm

The RR scheduling algorithm is a preemptive process scheduling algorithm.

According to the principle of fair service, RR will serve each process once in a certain period of time

RR executes a time slice for a process each time

The time slice length T is determined manually and can be equal or unequal .

The core process of PR scheduling algorithm:

At the nth second (the length of each time slice), make the following three judgments: (1) enter the queue at the end of the queue, (2) adjust the queue under the CPU (the process of the previous service is placed at the end of the queue), (3) The head of the team enters the CPU to run

Summary: RR is used for process scheduling and is suitable for time-sharing systems. The longer the time slice, the better it is to shorten the turnaround time. If the time slice is too long, RR degenerates into FCFS. The advantages and disadvantages are as follows:

  • Advantages: Conducive to interactive and transactional processes Conducive to I/O busy processes
  • Disadvantages: The scheduling overhead is large, and the real-time response requirements are not considered

6. Multi-level queue scheduling algorithm

In order to improve the performance of the computer system, several operating systems may be configured in one system at the same time

Divide the ready queue into a variety of different queues (foreground rotation ready queue, background FCFS ready queue)

Each process is fixedly assigned to a queue

Different queues use different scheduling algorithms (the foreground ready queue uses the RR scheduling algorithm, and the background ready queue uses the FCFS algorithm)

Processes in the background queue are executed only when the foreground ready queue is empty

Features: There are multiple OSes in the same computer system, the advantages and disadvantages are as follows:

  • Advantages: It can take into account time-sharing and batch processing tasks at the same time
  • Disadvantage: does not take into account the urgency of the job or process 

7. Multi-level feedback queue scheduling algorithm 

Set multiple ready queues and give them different priorities from high to low

Each queue adopts the RR algorithm, and the time slice length increases from high priority to low priority (generally doubled) (S1<S2<...<Sn)

The core process of the algorithm is as follows:

  • The new process is placed at the end of the highest priority ready queue
  • If the process is not completed once in the current ready queue, it will be inserted at the end of the lower ready queue
  • Subsequent queue processes are executed only when the higher priority ready queues are all empty
  • If there is a higher priority process, the current process will be placed at the end of the current ready queue, and the process with higher priority will be executed instead (when it returns to execute, it will make up for the difference)

Summary: It is a relatively good process scheduling algorithm that can meet the needs of various types of users. The advantages and disadvantages are as follows:

  • Advantages: It can take into account real-time, time-sharing and batch processing jobs (processes) at the same time
  • Disadvantages: the scheduling algorithm is more complicated, and the scheduling overhead is larger

3. Real-time scheduling

Real-time scheduling can meet the deadline requirements of real-time task teams, and the basic conditions are as follows:

1. Provide the necessary information:

(1) Ready time. (2) Start deadline and finish deadline. (3) Processing time. (4) Resource requirements. (5) Priority.

2. The system has strong processing ability:
Assuming that there are m periodic hard real-time tasks in the system, their processing time can be expressed as Ci, and the cycle time can be expressed as Pi, then in the case of a single processor, the following constraints must be met ( i.e. for tasks whose average processing time > average cycle time): 

\sum_{i=1}^{m} \frac{C_{i}}{P_{i}} \leq 1

For multiprocessor systems (eg N), the restrictions are as follows:

\sum_{i=1}^{m} \frac{C_{i}}{P_{i}} \leq N

3. Adopt a preemptive scheduling mechanism: so that high-priority tasks can meet the deadline requirements
4. Have a fast switching mechanism:

 (1) Quick response to external interrupts, (2) Fast task dispatching

1. Time slice round-robin scheduling algorithm

It can meet the real-time control with a response time of several seconds, and can be applied to general real-time systems

2. Non-preemptive priority scheduling algorithm

Can meet the real-time control with a response time of hundreds of milliseconds, and can be used for real-time control systems with less stringent requirements

3. Preemptive priority scheduling algorithm based on time interrupt 

It can meet the real-time control with a response time of several milliseconds to tens of milliseconds, and can be used in most real-time control systems

4. Immediately preemptive priority scheduling algorithm

It can meet the real-time control with a response time of nanoseconds to several milliseconds, and basically meet the requirements of all real-time control systems 

5. Earliest Start Deadline First (EDF) Algorithm

Prioritize tasks based on their start deadlines. The earlier the start deadline, the higher the priority

6. Lowest Laxity First (LLF) Algorithm

The priority of the task is determined according to the urgency (slack) degree of the task, and preemption occurs in the critical state (the slack degree is 0)

Slack = completion deadline - processing time - current time 

Example: In a real-time system, there are two periodic real-time tasks A and B. Task A is required to be executed every 20 ms, and the execution time is 10 ms; task B is only required to be executed every 50 ms, and the execution time is 25 ms. Apply the lowest slack first algorithm scheduling:

  • Each task is executed only once in its own cycle
  • Preemption occurs with slack of 0 

4. Multiprocessor System Scheduling - Understand

The types of multiprocessor systems are generally divided into: (1) Tightly coupled (Tightly Coupted) MPS, (2) Loosely coupled (Loosely Coupled) MPS

The multiprocessor system can be divided into (1) symmetric multiprocessor system SMPS (Symmetric MultiProcessor System), (2) asymmetric multiprocessor system

Among them, the process allocation methods in the symmetric multiprocessor system can be divided into (1) static allocation and (2) dynamic allocation

Most of the process allocation methods in asymmetric MPS adopt the master-slave (Master-Slave) type OS

Its process (thread) scheduling methods are mainly (1) self-scheduling mechanism, (2) group scheduling, and (3) dedicated processor allocation.

Five, deadlock

1. Deadlock overview

Deadlock refers to a deadlock caused by multiple processes competing for resources . Without external forces, these processes will never be able to move forward. 

There are two main reasons for deadlocks:

1. Competing resources: when two or more processes need two or more resources (mainly non- deprived resources or temporary resources )

1. Deprivable resources: After a process obtains this type of resource, it can be recorded by other processes or systems (such as CPU, RAM, etc.) 2. Non-deprivable resource: After a certain process obtains this type of resource, other processes or systems cannot preemptive, can only release itself after the process is done with it (such as printers, etc.)

3. Temporary resources: resources generated by a process and temporarily used by another process (such as semaphores)

2. Illegal progress sequence of processes: the sequence of requesting and releasing resources is improper 

Necessary conditions for a deadlock to occur:

1. Mutually exclusive conditions: the requested resource is a critical resource

2. Request and maintain conditions: apply for new resources and maintain old resources

3. Non-deprivation conditions: The obtained resources will not be deprived by external forces before they are used up

4. Loop waiting condition: waiting for each other for resources 

The basic methods to deal with deadlock are:

1. Prevent deadlock:

Set some restrictions to destroy one or more of the necessary conditions for deadlock

2. Avoid deadlock:

During the dynamic allocation of resources, use some method to prevent the system from entering an unsafe state

3. Detect deadlock:

Through the detection mechanism, timely detect the occurrence and cause of deadlock, and determine the relevant processes and resources

Take measures to remove the deadlock

4. Remove the deadlock:

Deprive a resource or reclaim some resources by undoing a process.

Deadlock removal and deadlock detection are mutually cooperative.

Supplement: The strategy of most operating systems for deadlock is to ignore (ostrich strategy) and leave it to the user.

2. Prevent deadlock

Core: Set certain restrictions and destroy one or more of the necessary conditions for deadlock

1. Abandon the "request and hold" condition: allocate resources all at once

  • Pros: simple, easy to implement and safe
  • Disadvantages: serious waste of resources, delayed running of processes

2. Abandon the "no deprivation" condition:

Release allocated resources when requested resources are not satisfied

Disadvantages: All the previous work is invalidated, the cost is high, repeated applications, and performance decline

3. Abandon the "loop waiting" condition:

Requests for resources must be made in a certain order

Disadvantages: The resource serial number needs to be relatively stable, the resource obtained first may be idle for a long time, and there are restrictions on user programming (resource application)

4. Abandon the "mutual exclusion" condition:

Time-sharing virtual-merge buffer

3. Avoid Deadlock - Banker's Algorithm

The core of deadlock avoidance is to determine whether the system will enter an unsafe state (unsafe area, dangerous area, and restricted area) before dynamically allocating resources, and if so, make the process wait .

The safe state means that the system can allocate the required resources for each process Pi according to a certain process sequence (P1, P2, ..., Pn) (called <P1, P2, ..., Pn> sequence as a safe sequence), Until the maximum resource demand of each process is met, each process can be successfully completed. If the system cannot find such a safe sequence, the system is said to be in an unsafe state.

If it is not allocated according to the <P2,P1,P3> safe sequence, deadlock will occur.

The mainstream algorithm to avoid deadlock is the banker's algorithm idea . Core: decide whether to allocate resources according to whether the system is in a safe state.

The specific implementation of the banker's algorithm mainly consists of the following data structures:

1. Available resource vector Available:

An array containing m elements, each of which represents the number of resources available for a class

2. Maximum demand matrix Max:

An n×m matrix that defines the maximum demand for resources of type m by each of the n processes in the system

3. Allocation matrix Allocation:

An n×m matrix that defines the number of resources currently allocated to each process for each type of resource in the system

4. Demand matrix Need:

An n×m matrix, used to represent the number of various resources required by each process

Need[i,j]=Max[i,j]-Allocation[i,j]

Let Requesti be the request vector of process Pi. If Requesti[j]=K, it means that process Pi needs K resources of type Rj. When Pi sends a resource request, the system checks according to the following steps:

(1) If Requesti[j]≤Need[i,j] , turn to step 2; otherwise, it is considered an error, because the number of resources it needs has exceeded the maximum value it announced.        

(2) If Requesti[j]≤Available[j] , turn to step (3); otherwise, means that there are not enough resources, and Pi must wait.

 (3) The system tentatively allocates resources to process Pi, and modifies the values ​​in the following data structure:

① Available[j]∶=Available[j]-Requesti[j];

② Allocation[i,j]∶=Allocation[i,j]+Requesti[j];

③ Need[i,j]∶=Need[i,j]-Requesti[j];

(4) The system executes the security algorithm to check whether the system is in a safe state after the resource allocation. If it is safe, the resource is formally allocated to the process Pi to complete the allocation; otherwise, the tentative allocation is invalidated, the original resource allocation state is restored, and the process Pi waits. 

For the inspection of the overall security status of the system, a security algorithm is introduced, and the core implementation is as follows:

 (1) Set two vectors: ① Work vector Work: It indicates the number of various resources that the system can provide to the process to continue running. It contains m elements. When the security algorithm starts, Work:=Available; ② Finish : It indicates whether the system has enough resources allocated to the process to make it run to completion. At the beginning, do Finish[i]:=false; when there are enough resources allocated to the process, then make Finish[i]:=true.

 (2) Find a process from the process set that can meet the following conditions:  ① Finish[i]=false; ② Need[i,j]≤Work[j]; If found, execute step (3), otherwise, Execute step (4).         

(3) After the process Pi obtains resources, it can execute smoothly until it is completed, and releases the resources allocated to it, so it should be executed: Work[j]:=Work[i]+Allocation[i,j]; Finish[i]:=true;go to step 2;

  (4) If Finish[i]=true of all processes is satisfied, it means that the system is in a safe state; otherwise, the system is in an unsafe state.

Assuming that there are five processes {P0, P1, P2, P3, P4} and three types of resources {A, B, C} in the system, the number of each resource is 10, 5, 7 respectively, the resource allocation at time T0 as the picture shows:

 A sample security sequence is as follows:

The disadvantages of the banker's algorithm are as follows:

1. Few processes can know the maximum value of resources they need before running

2. And the number of processes is not fixed, it is often changing (such as new user login or exit)

3. Originally available resources may suddenly become unavailable (for example, the tape drive may be broken)

4. The banker's algorithm has a large overhead, and the real-time performance is not very good 

4. Deadlock detection and release

Deadlock detection is mainly for resource allocation diagrams, which generally include the following information:

1. Process: circle 2. Resource: box 3. Request side: from process to resource 4. Allocation side: from resource to process

The deadlock detection mainly uses the deadlock theory , and the core process is as follows:

  • Step 1: Look at the R1 resource first. It has three arrows pointing outward, so it allocates a total of 3 resources to the process. At this time, R1 has no free resources left.
  • Step 2: Looking at the R2 resource again, it has an arrow pointing outward, so it allocates a total of 1 resource to the process. At this time, R2 still has a free resource that has not been allocated.
  • Step 3: Look at the resources, then look at the process, first look at the process P2, it only applies for one R1 resource, but at this time the R1 resource has been used up, so the process P2 enters the blocked state, therefore, the process P2 cannot be isolated temporarily point.
  • Step 4: Looking at process P1 again, it only applies for one R2 resource. At this time, the system still has one R2 resource left unallocated, so the application of P1 can be satisfied. In this way, the process P1 has obtained all the resources it needs, so it will not enter the blocking state and can run forever. After it finishes running, we will release all its resources. It is equivalent to: all the edges of P1 can be removed and turned into an isolated point.
  • Step 5: After the process P1 finishes running, it releases the resources it occupies (2 R1 resources and 1 R2 resource). After the system reclaims these resources, the idle resources become 2 R1 resources and 1 R2 resource. Since process P2 has been applying for an R1 resource, the system can satisfy its application at this time. In this way, the process P2 has obtained all the resources it needs, so it will not enter the blocking state and can run all the time. After it finishes running, we will release all its resources. It is equivalent to: all the edges of P2 can be removed and turned into an isolated point.

Tips: The left and right arrows only indicate application and allocation, and do not contain responses. The detailed explanation of deadlock definition can be seen: deadlock theory and resource allocation diagram simplification method_Cuzblind's blog-CSDN blog 

The following figure is an example of a deadlock:

As for the lifting of the deadlock, that is (1) deprivation of resources, (2) cancellation of the process .

The practical and convenient method is to undo those processes with the least cost one by one , or minimize the number of undo processes until enough available resources are obtained to remove the deadlock. 

If the current process is running ( ), the system will execute the process scheduling primitive. A. Execute a transfer instruction B. Request to increase the main memory space, which is considered safe after the system calls the banker’s algorithm. C. Executes a An I/O instruction requires input data D. An I/O completion interrupt occurred during program execution. 

Guess you like

Origin blog.csdn.net/weixin_51426083/article/details/131370380