Processor Scheduling for Operating System Review

Table of contents

2.2.1 The concept of scheduling

2.2.2 Method of process scheduling

2.2.3 Scheduling evaluation index

2.2.4 Typical Scheduling Algorithms✳

【exercise】:


2.2.1 The concept of scheduling

(1) Basic concepts

In a multi-programming system, a process can obtain a processor and execute only after it is scheduled by the processor, so how to assign a processor to a process is the core issue of the operating system.

Processor scheduling is the process of selecting a process from the ready queue to obtain a processor according to a certain scheduling algorithm. It is both the foundation of multiprogramming operating systems and the core of operating system design.

(2) Three-level scheduling

    Different operating systems may require different levels of scheduling, but usually, a job needs to go through three levels of scheduling from submission to completion:

    ①Advanced scheduling (job scheduling). According to a specific algorithm, some jobs are selected from the backup queue of the external memory and transferred into the memory, and processes are created and necessary resources are allocated for them. The newly created processes are then inserted into the ready queue so that they gain the right to compete for the processor. Simply put, advanced scheduling is the scheduling between memory and storage, requiring a job to be transferred in and out only once. In a multi-channel batch system, most jobs are subject to job scheduling, while other systems usually do not need to set up job scheduling. The execution frequency of job scheduling is low, and generally a job scheduling takes several minutes.

② Intermediate scheduling (memory scheduling). Mid-level scheduling is introduced to improve intrinsic utilization and system throughput. That is, the system transfers the temporarily blocked process in the memory to the external memory to wait, and at the same time changes the state of the process to the suspended state. When the process is ready to run and the memory is free, the intermediate scheduling will reload those ready processes on the external storage that are ready to run into the memory according to a certain scheduling algorithm, and modify their state to be ready, and hang them on the ready queue to wait.

③ Low-level scheduling (process scheduling). Its main function is to select a process in the ready queue according to a certain algorithm, and assign a processor to it. Process scheduling is the most basic scheduling in the operating system. It has a high execution frequency and only takes tens of milliseconds for a process scheduling. This scheduling must be configured in multi-processing, time-sharing, and real-time operating systems.

[Comparison of three-level scheduling]:

 (3) Scheduling timing and switching process

       Both process scheduling and switching programs are operating system kernel programs. When the event requesting scheduling occurs, the direction can run the process scheduler, and when the new ready process is scheduled, the switching between processes can be done. Process switching often occurs immediately after the scheduling is completed. It requires saving the site information of the current switching point of the original process and restoring the site information of the scheduled process. When the site is switched, the operating system kernel pushes the site information of the original process to the kernel stack of the current process to save them and update the stack pointer. After the kernel completes the on-site information of the new process loaded from the kernel stack of the new process, updates the current running process space pointer, resets the PC register and other related work, it starts to run the new process.

       However, in the actual design of the operating system, when the system kernel program is running, at some point a factor that causes process scheduling occurs, and the scheduling and switching may not performed immediately :

①In the process of interrupt processing: the interrupt processing process is complicated, it is difficult to achieve process switching in implementation, and interrupt processing is part of the system work, logically does not belong to a certain process, and should not be deprived of processor resources.

②The process is in the critical area of ​​the operating system kernel program: after entering the critical area, it needs to access the shared data exclusively. In theory, it must be locked to prevent other parallel programs from entering. Before unlocking, it should not switch to other processes to run, so as to speed up the release of the shared data.

③Other atomic operations that need to completely shield interrupts: such as atomic operations such as locking, unlocking, interrupting site protection, and recovery. During the atomic operation, all interrupts must be masked, and of course process scheduling and switching cannot be run.

If the conditions that cause scheduling occur in the above process, the system's request scheduling flag should be set, and the corresponding scheduling and switching will not be performed until the above process is over.

In modern operating systems, the conditions for process scheduling and switching should be as follows :

① When a scheduling condition occurs and the current process cannot continue to run, scheduling and switching can be performed immediately. If the operating system schedules processes only in this case, it is non-preemptive scheduling.

②When the interrupt processing is completed or the self-trap processing is completed, before returning to the user state program execution site of the interrupted process, if the request scheduling flag is set, the process scheduling and switching can be performed immediately. Preemptive scheduling is achieved if the operating system supports running the scheduler in this case.

[True question]:

1. [2012 Unified Exam Questions] If there are multiple ready processes in a single-processor multi-process system, the following statement about processor scheduling is wrong ( ).C

A. Processor scheduling can be performed at the end of the process        

B. Processor scheduling can be performed after creating a new process

C. Processor scheduling cannot be performed when the process is in the critical section

D. Processor scheduling can be performed when the system calls and returns to user mode

[analyze]:

·When is process scheduling required?

The current running process actively abandons the processor: the process terminates normally, the running process is blocked, and the process actively requests to block (waiting for I/O)

The current running process passively gives up the processor: the time slice allocated to the process is used up, there are more urgent things to deal with (such as I/O interrupts), and a higher priority process enters the ready queue

· When can't process scheduling be performed?

In the process of processing interrupts: the interrupt processing process is complex and closely related to hardware, it is difficult to switch processes during interrupt processing

The process is in the critical section of the operating system kernel program

During the atomic operation (primitive language): the atomic operation cannot be interrupted and must be completed in one go (such as modifying the process status in the pcb and putting the PCB into the corresponding queue )

When the process is in the critical area, the process is occupying the processor, as long as the usage rules of critical resources are not broken, it will not affect the scheduling of the processor

2.2.2 Method of process scheduling

(1) Non-deprivational scheduling

When a process is running on the processor, even if a process with higher priority enters the ready queue, the current process will not assign the processor to the new process and continue to execute until the process is completed or waits for some event to occur and enters the blocking state. Then the processor is assigned to the process with higher priority.

The advantage is that it is simple to implement and the system overhead is small. It is suitable for most batch processing systems. But it cannot be used on legacy systems and most real-time systems.

(2) Deprivation scheduling

When a process is running on the processor, if a process with higher priority enters the ready queue, the current process immediately suspends the running process and assigns the processor to the process with higher priority.

The advantages can improve system throughput and response efficiency to a certain extent, but "deprivation" cannot be executed at will, and must follow principles such as priority, short process priority, and time slice.

2.2.3 Scheduling evaluation index

(1) CPU utilization

The CPU is the most important and expensive resource of the operating system, so it should be kept as busy as possible. The CPU utilization is an important indicator to evaluate the quality of the scheduling algorithm.

(2) System throughput

System throughput indicates the number of jobs completed by the CPU per unit time. The length of the job determines the system throughput to a certain extent. Therefore, choosing different scheduling algorithms according to the length of the job will also have a greater impact on the throughput of the system to a certain extent.

(3) Turnaround time

In the system, for each job, the time required to complete the job is an index to measure the performance of the system, generally measured by turnaround time or weighted turnaround time.

①Turnaround time

Turnaround time is the time elapsed from job submission to job completion. Including the sum of the time spent waiting for jobs, queuing in the ready queue to run on the processor, and performing input/output operations.

Turnaround time T = job completion time - job submission time

②Average turnaround time

The average turnaround time is the average of the turnaround times for multiple jobs.

Average turnaround time for n jobs T=(T 1 +T2 +... +T n ) /n

③ Turnaround time with rights

The weighted turnaround time refers to the ratio of the job turnaround time to the actual running time of the job.

Job's weighted turnaround time w= job's turnaround time T/ actual running time T s

④Average weighted turnaround time

The average weighted turnaround time is the average of the weighted turnaround times of multiple jobs.

Average weighted turnaround time W = ( w1 + w 2 +... + wn ) /n

(4) Waiting time

Waiting time is the sum of the time a process is in the state of waiting to acquire a processor. The processor scheduling algorithm does not actually affect the time for job execution or input/output operations, only the time a job spends waiting in the ready queue. Therefore, waiting time is also one of the criteria to measure the pros and cons of a scheduling algorithm.

(5) Response time

Response time is the time from when a user submits a job request to when the system responds for the first time. For users, the scheduling strategy should minimize the response time, so that the response time is within the acceptable range of users.

2.2.4 Typical Scheduling Algorithms✳

(1) First come first serve FCFS

FCFS scheduling algorithm is the simplest scheduling algorithm, which can be used for both job scheduling and process scheduling.

In job scheduling, the algorithm selects one or several jobs that enter the queue first from the backup job queue each time, transfers them into memory, allocates necessary resources, creates processes and puts them into the ready queue.

In process scheduling, the FCFS scheduling algorithm selects the process that first enters the queue from the ready queue each time, assigns the processor to it, and puts it into operation, and releases the processor until it is completed or blocked for some reason.

Features:

① FCFS belongs to the inalienable algorithm, the algorithm is simple, but the efficiency is low;

②It is more beneficial to long jobs, but disadvantageous to short jobs (compared to SPF and high response ratio). If a long job arrives at the system first, it will make many short jobs behind wait for a long time, so it cannot be used as the main scheduling strategy for time-sharing systems and real-time systems;

③ It is beneficial to CPU busy jobs, but not conducive to I/0 busy jobs.

(2) Short job priority SJF

The short job (process) priority scheduling algorithm refers to the algorithm for priority scheduling of short jobs (processes). The algorithm selects one or several processes with the shortest estimated running time from the ready queue, assigns a processor to it, and executes it immediately, and releases the processor until it is completed or blocked by an event.

Features:

① The algorithm is unfavorable to long jobs, and the turnaround time of long jobs in the SJF scheduling algorithm will increase. If a long job enters the backup queue of the system, since the scheduler always prioritizes scheduling short jobs, the long job will not be scheduled for a long time, and "starvation" may occur.

② The urgency of the job is not considered, so there is no guarantee that the urgent job will be processed in time.

③ The length of the job is only determined according to the estimated execution time provided by the user, and the user may shorten the estimated running time of his job intentionally or unintentionally, and the algorithm may not be able to truly achieve short job priority scheduling.

④ The average waiting time and average turnaround time of the SJF scheduling algorithm are the least.

(3) High response ratio priority

    The high response ratio priority scheduling algorithm is a comprehensive balance of FCFS scheduling algorithm and SPF scheduling algorithm, taking into account the waiting time and estimated running time of each job at the same time. In each job scheduling, the response ratio of each job in the backup job queue is calculated first, and the job with the highest response ratio is selected and put into operation.

Response ratio = (waiting time + requested service time) / requested service time

Features:

① When the waiting time of the job is the same, the shorter the service time is, the higher the response ratio is, which is beneficial to the short job.

②When the service time is required to be the same, the response ratio of the job is determined by its waiting time. The longer the waiting time, the higher the response ratio. Therefore, the algorithm realizes first-come-first-served.

③ For long jobs, the response ratio of the job can increase with the increase of the waiting time. When the waiting time is long enough, the response ratio can be raised to a high level, and a processor can also be obtained. Therefore, the starvation state is overcome and long-term jobs are taken into account .

(4) Priority scheduling

In process scheduling, the priority scheduling algorithm selects the process with the highest priority from the ready queue each time, assigns the processor to it, and puts it into operation.

According to whether the new higher priority process can preempt the executing process, the scheduling algorithm can be divided into the following two types:

① Non-preemptive priority scheduling algorithm. When a process is running on the processor, even if a more important or urgent process enters the ready queue, the running process is still allowed to continue running until the task is completed or the processor is actively given up by waiting for an event before the processor is allocated to a more important or urgent process.

② Deprivation priority scheduling algorithm. When a process is running on the processor, if a more important or urgent process enters the ready queue, the running process will be suspended immediately, and the processor will be assigned to the more important or urgent process.

According to whether the priority of a process can be changed after it is created, the process priority can be divided into the following two types:

① Static priority. Priority is determined when a process is created and remains constant throughout the lifetime of the process . The main basis for determining the static priority is the process type, the resource requirements of the process, and the user requirements.

② Dynamic priority. During the running process, the priority is dynamically adjusted according to the change of the process situation. The main basis for dynamically adjusting the priority is the length of CPU time occupied by the process and the length of time the ready process waits for the CPU.

The setting of process priority can refer to the following principles:

·System Process>User Process. As the manager of the system, the system process should have a higher priority.

• Interactive process > non-interactive process. When you usually use your mobile phone, the process running in the foreground that is interacting with you should respond to you more quickly, so naturally it needs to be prioritized, that is, it has a higher priority.

• I/O type process > Compute (CPU) type process. We know that the processing speed of the I/0 device is much slower than that of the CPU. If the priority of the I/0 type process is set higher, it is more likely that the I/0 device will start working as soon as possible, thereby improving the overall efficiency of the system.

(5) Time slice rotation

The time slice round-robin scheduling algorithm is mainly applicable to time-sharing systems. In this algorithm, the process scheduler always selects the first process in the ready queue to execute according to the order of arrival time, that is, the principle of first come, first served, but only runs for one time slice. After using a time slice, even if the process has not completed its execution, it must release (deprived) the processor to the next ready process, and the deprived process returns to the end of the ready queue and requeues, waiting to run again.

Features:

① If the time slice is large enough so that all processes can be executed in one time slice, then degenerate into the FCFS algorithm.

②If the time slice is small, the processor switches frequently, the overhead increases, and the process usage time decreases.

(6) Multi-level feedback queue

The multi-level feedback queue scheduling algorithm is the synthesis and development of the time slice round-robin scheduling algorithm and the priority scheduling algorithm. By dynamically adjusting the process priority and time slice size, the multi-level feedback queue scheduling algorithm can take into account various system goals. For example, take care of short processes to improve system throughput and shorten average turnaround time; take care of I/0-type processes to obtain better I/0 device utilization and shorten response time; at the same time, it is not necessary to estimate the execution time of processes in advance.

Features:

① Terminal-type job users mostly have short jobs, so short jobs are given priority and interactivity is good.

②For users with short batch processing jobs, the jobs are basically completed in the specified time slice of the first queue, and the turnaround time is short.

③ For users of long batch processing jobs, time slice rotation is used for long jobs, and users will eventually get all processed.

【exercise】:

1. The following processes need to be scheduled for execution (see the table below):

1) If the non-preemptive short process priority scheduling algorithm is used, what is the average turnaround time of these 5 processes?

2) If the preemptive short process priority scheduling algorithm is used, what is the average turnaround time of the 5 process name arrival time send-off time processes?

A. 8.62; 6.34        B.8.62; 6.8

C.10.62; 6.34        D.10.62; 6.8

2. [2012 Unified Examination Questions] There are only two jobs P1 and P2 in a multi-channel batch processing system. P2 arrives 5ms later than P1. Its calculation and I/O operation sequence is as follows:

P1: calculation 60ms, I/O 80ms, calculation 20ms

P2: calculation 120ms, I/O 40ms, calculation 40ms

If the scheduling and switching time are not considered, the minimum time required to complete the two jobs is ( ) . B

A.240ms       B.260ms    C.340ms     D.360ms

3. [2016 Unified Examination Questions] There are 1 input device and 1 output device in a single CPU system, and there are 3 concurrently executed jobs. The input, calculation and output time of each job are 2ms, 3ms and 4ms respectively, and they are all executed in the order of input, calculation and output. The minimum time required to execute the 3 jobs is ( ). B

A.15ms     B.17ms    C.22ms       D.27ms

 

4. [2017 Unified Exam Questions] Among the following descriptions about time slice-based process scheduling, the error is ( ). B

A. The shorter the time slice, the more times the process switches, and the greater the system overhead

B. After the time slice of the current process is used up, the process state changes from the execution state to the blocking state

C. After the clock interrupt occurs, the system will modify the remaining time of the current process in the time slice

D. The main factors affecting the size of the time slice include response time, system overhead, and the number of processes.

After the time slice of the current process is used up, if the process has not finished running, it will return to the end of the ready queue and re-queue, and the process status will change from running to ready.

5. [2018 Unified Examination Questions] A system adopts a priority-based non-preemptive process scheduling strategy, and the system time overhead for completing a process scheduling and process switching is lμs. There are three processes P, P, and P3 in the ready queue at time T. Their waiting time in the queue, required CPU time, and priority are shown in the following table. If the process with a large priority value gets the CPU first, and the system starts process scheduling from time T, the average turnaround time of the system is ( ). D.

A.54μs     B. 73μs        C.74μs        D.75μs

· Scheduling according to the priority value, then the P2 process starts running. P2 turnaround time = 15+1+24=40μs; then run P3 process, P3 turnaround time=18+1+36+1+24=80μs; ​​finally P1 turnaround time=30+1+12+1+24+1+36=105μs, so average turnaround time=(40+80+105)/3=75μs.

6. [2019 Unified Examination Questions] The system uses the second-level feedback queue scheduling algorithm for process scheduling. The ready queue Q1 adopts the time slice round-robin scheduling algorithm, and the time slice is 10ms; the ready queue Q2 adopts the short process priority scheduling algorithm; the system schedules the processes in the Q1 queue first. When Q1 is empty, the system will schedule the processes in Q2; the newly created process first enters Q1. The system starts process scheduling after creating processes P1 and P2 in sequence. The CPU time required by P1 and P2 is 30ms and 20ms respectively. Then the average waiting time of processes P1 and P2 in the system is ( ).C

A.25ms          B.20ms         C.15ms        D、10ms

7.[2020 Unified Exam Questions] Among the following factors related to process scheduling, what needs to be considered when designing a multi-level feedback queue scheduling algorithm is ( ).ABCD

A. The number of ready queues B. The priority of the ready queues C. The scheduling algorithm of each ready queue D. The migration conditions of processes between the ready queues

The multi-level feedback queue scheduling algorithm needs to comprehensively consider the number of priorities, the conversion rules between priorities, etc. The number of ready queues will affect the final completion time of long processes, A is correct; the priority of the ready queue will affect the order of process execution, B is correct; the scheduling algorithm of each ready queue will affect the scheduling order of processes in each queue, C is correct; the migration conditions of processes in the ready queue will affect the execution time of each process in each queue, D is correct.

8. [2021 Unified Exam Questions] In the following kernel data structures or programs, the time-sharing system needs to use ( ).ABC

A. Process control block B. Clock interrupt handler C. Process ready queue D. Process blocking queue

In the time slice round-robin scheduling of the time-sharing system, when the system detects a clock interruption, it will lead to a clock interruption handler. The scheduler selects a process from the ready queue to allocate a time slice to it, and modifies the process information in the process control block of the process. At the same time, the process that has run out of time slices is put into the ready queue or terminated. ABC is correct. The process in the blocking team pair can participate in the scheduling only after being woken up and entering the ready queue, so the scheduling process does not use the blocking queue.

9. [2021 Unified Exam Questions] Among the following events, the one that may cause the process scheduler to execute is ( ). ABCD

A. The interrupt processing ends B. The process is blocked C. The process execution ends D. The time slice of the process is exhausted

In the time slice scheduling algorithm, after the interrupt processing is completed, the system detects whether the time slice of the current process is used up. If it is used up, it will be set to the ready state or let it stop running. If the ready queue is not empty, schedule the head process of the ready queue to execute. A is possible; when the current process is blocked, put it into the blocking queue. If the ready queue is not empty, schedule a new process to execute. The end of the process will cause the current process to release the CPU, and select a process from the ready queue to obtain the CPU, C may. When the time slice of the process is used up, the current process will give up the CPU, and at the same time, the process at the head of the ready queue will be selected to obtain the CPU. D is possible.

Guess you like

Origin blog.csdn.net/weixin_46516647/article/details/124499666