[Operating System] Process Management (3) - Process Scheduling

The concept and level of scheduling

insert image description here

When there are a bunch of tasks to be processed, but due to limited resources, these things cannot be processed at the same time. This requires determining some kind of rules to determine the order in which these tasks are processed, which is the problem of "scheduling" research.

Three Levels of Scheduling - Advanced Scheduling

Assignment: A specific task.
The user submits a job to the system ≈ the user asks the operating system to start a program (to handle a specific task)

Advanced scheduling (job scheduling) . According to a certain principle, select a job from the job backup queue in the external memory to load it into the memory, and create a process. Each job is only called in once and called out once . The PCB will be created when the job is called in, and the PCB will be canceled when the job is called out.

Simplified understanding: The memory space is limited, and sometimes all the jobs submitted by the user cannot be put into the memory. Several programs need to be started, which one should be started first.

Three levels of scheduling - intermediate scheduling

Intermediate Scheduling (Memory Scheduling) - According to a certain strategy, which process is in the suspended state is reassigned to memory.

A process may be transferred out and transferred into memory many times, so the frequency of intermediate scheduling is higher than that of advanced scheduling.

When the memory is not enough, the data of some processes can be transferred out of the external memory. When the memory is free or the process needs to run, it will be reloaded into the memory.

The state of the process that is temporarily transferred to the external memory to wait is the suspended state . Suspended process PCBs are organized into pending queues.

Suspended state of process and seven-state model

The state of the process that is temporarily transferred to the external memory to wait is the suspended state (suspend state, suspend). The suspended state can be further subdivided into two states : ready suspension and blocking suspension .

Pay attention to the difference between "suspended" and "blocked". In both states, the service of the CPU cannot be obtained temporarily, but in the suspended state, the process image is transferred to the external storage, while in the blocked state, the process image is still in memory.

Some operating systems will divide the ready hang and block hang into two hang queues, and even further subdivide the block hang process into multiple queues according to different blocking reasons.

insert image description here

Three levels of scheduling - low-level scheduling

Low-level scheduling (process scheduling/processor scheduling) - select a process from the ready queue according to a certain strategy, and assign a processor to it.

Process scheduling is the most basic type of scheduling in an operating system, and process scheduling must be configured in a general operating system.

The frequency of process scheduling is very high, usually every tens of milliseconds.

The connection and comparison of three-tier scheduling

insert image description here

insert image description here

Process scheduling switching and process

Timing of process scheduling

Process scheduling (low-level scheduling) is to select a process from the ready queue to assign a processor to it according to a certain algorithm.

insert image description here

insert image description here

The method of process scheduling

Non-preemptive scheduling , also known as non-preemptive scheduling. That is, only processes are allowed to voluntarily relinquish the processor . Even if more urgent tasks arrive during the running process, the current process will continue to use the processor until the process terminates or actively requests to enter the blocking state.

The implementation is simple, the system overhead is small, but it cannot handle urgent tasks in time, and is suitable for early batch processing systems.

The deprivation scheduling method is also called the preemption method. When a process is executing on the processor, if there is a more important or more urgent process that needs to use the processor, immediately suspend the executing process and assign the processor to the more important and urgent process.

More urgent processes can be prioritized, and the function of allowing each process to execute in turn according to time slices can also be realized (through clock interrupts). It is suitable for time-sharing operating system and real-time operating system.

Process switching and process

The difference between "narrow process scheduling" and "process switching":
narrow process scheduling refers to selecting a process to run from the ready queue. (This process can be the process that has just been suspended from execution, or it can be another process. In the latter case, process switching is required)

Process switching refers to the process in which one process relinquishes the processor and another process occupies the processor.

Generalized process scheduling includes two steps of selecting a process and process switching.

The process of process switching is mainly completed:

  1. Save various data of the original running process
  2. Restoration of various data of the new process
    (such as: program counter, program status word, various data registers and other on-site information of the processor, which are generally stored in the process control block)

Note: Process switching is costly, so if the process scheduling and switching are performed too frequently, the efficiency of the entire system will inevitably be reduced, so that most of the system's time is spent on process switching, and the time actually spent on executing processes reduce.

insert image description here

Evaluation Indicators for Scheduling Algorithms

insert image description here

CPU utilization

Since early CPUs were extremely expensive, one would want to make the CPU work as much as possible.

CPU Utilization: Refers to the ratio of CPU "busy" time to the total time.

insert image description here

Eg: A computer only supports a single program. A job needs to be run on the CPU for 5 seconds at the beginning, then printed out by a printer for 5 seconds, and then executed for another 5 seconds before it ends. During this process, what are the CPU utilization and printer utilization respectively?

Answer:

CPU利用率 =5+5/5+5+5= 66.66%
打印机利用率 = 5/15 = 33.33%

system throughput

For a computer, it is hoped that it can process as many jobs as possible in as little time as possible.

insert image description here

It takes a total of 100 seconds for a computer system to process 10 jobs, what is the system throughput?
Answer: 10/100 = 0.1 track/second

Turnaround time

Turnaround time is the time interval between when a job is submitted to the system and when it is completed .

It includes four parts: the time the job waits for job scheduling (advanced scheduling) on ​​the external memory backup queue, the time the process
waits for time the process executes on the CPU, and the process waits for I/O The time when the O operation completed. The latter three items may occur multiple times throughout the processing of a job.

insert image description here

insert image description here
The weighted turnaround time must be ≥ 1; the smaller the weighted turnaround time and turnaround time, the better.

  • For two jobs with the same turnaround time, the job with the longer actual running time will be serviced more in the same time, the weighted turnaround time will be smaller, and the user satisfaction will be higher.
  • For two jobs with the same actual running time, the weighted turnaround time with short turnaround time is smaller and user satisfaction is higher.

waiting time

Computer users want their jobs to wait for the processor as little as possible. The waiting time refers to the sum of the time that the process/job is in the state of waiting for the processor . The longer the waiting time, the lower the user satisfaction.

insert image description here

  • For a process , the waiting time refers to the sum of the waiting time for the process to be served after the process is established. The process is actually being served while waiting for I/O to complete, so it is not included in the waiting time.
  • For a job , not only the waiting time after the process is established, but also the waiting time of the job in the backup queue of the external memory should be added.

How long a job needs to be served by the CPU and how long it is served by the I/O device is generally determined and unchanged, so the scheduling algorithm actually only affects the waiting time of the job/process. Of course, similar to the previous indicators, there is also an "average waiting time" to evaluate the overall performance.

Response time

For computer users, they hope that their submitted requests (such as entering a debugging command through the keyboard) will be serviced and responded by the system as soon as possible.

Response time refers to the time from when a user submits a request to when the first response is generated .

Scheduling Algorithm

Tips: Learning ideas for various scheduling algorithms

  1. algorithm thinking
  2. algorithm rules
  3. Is this scheduling algorithm used for job scheduling or process scheduling ?
  4. Preemptive? Non-preemptive?
  5. pros and cons
  6. Will it cause starvation (a process/job is not served for a long time)

First come first served (FCFS)

Algorithm thinking: mainly from the perspective of "fairness" (similar to the example of queuing up to buy things in our lives).

Algorithm rule: Serve jobs/processes in the order they arrive.

Used for job/process scheduling : When used for job scheduling, it considers which job arrives at the backup queue first; when used for process scheduling, it considers which process arrives at the ready queue first.

Whether it can be preempted: non-preemptive algorithm

Advantages: Fairness and simple algorithm implementation.

Disadvantages: short jobs that are queued behind long jobs (processes) need to wait for a long time, and the turnaround time of entitlement is very large, and the user experience is not good for short jobs. That is, the FCFS algorithm is good for long jobs and bad for short jobs.

Will it cause hunger: no

Shortest Job First (SJF)

Algorithm idea: pursue the least average waiting time, the least average turnaround time, and the least average weighted turnaround time.

Algorithm rules: The shortest job/process is served first (the so-called "shortest" refers to the shortest service time required).

For job/process scheduling: It can be used for both job scheduling and process scheduling. When used for process scheduling, it is called "Short Process First (SPF, Shortest Process First) algorithm".

Is it preemptible? : SJF and SPF are non- preemptive algorithms. But there is also a preemptive version - the shortest remaining time first algorithm (SRTN, Shortest Remaining Time Next).

Pros and Cons
Pros: "Shortest" average wait time, average turnaround time.
Cons: Not fair. Good for short jobs, bad for long jobs . Starvation may occur. In addition, the running time of the job/process is provided by the user, which is not necessarily real, and it may not be possible to achieve real short job priority.

Will cause hunger: Yes. If a steady stream of short jobs/processes arrives, long jobs/processes may not be served for a long time, resulting in "starvation". If it is not served all the time, it is called "starvation".

insert image description here

insert image description here

  • If not specifically stated in the title, the mentioned "short job/process priority algorithm" is non-preemptive by default .
  • When all processes can run at the same time, the average waiting time and average turnaround time of the SJF scheduling algorithm are the least.

Highest Response Ratio First (HRRN)

insert image description here

Algorithm idea:
The waiting time of the job/process and the time required for service should be considered comprehensively.

Algorithm rule: Calculate the response ratio of each job/process at each scheduling, and select the job/process with the highest response ratio to serve it.

For job/process scheduling: It can be used for both job scheduling and process scheduling.

Is it preemptible? : Non-preemptive algorithm. Therefore, only when the currently running job/process actively gives up the processor, scheduling is required and the response ratio needs to be calculated.

Advantages and disadvantages
Comprehensive consideration of waiting time and running time (required service time) When the
waiting time is the same, the shortest service time is preferred (the advantage of SJF) When the service time is required to be the same, the long waiting time is given priority (the advantage of FCFS) For long jobs In other words, as the waiting time gets longer, the response ratio will become larger, thus avoiding the problem of long job starvation.

Will it cause hunger: No.

insert image description here

Note: These algorithms mainly care about the fairness to users, average turnaround time, average waiting time and other indicators for evaluating the overall performance of the system, but they do not care about "response time" and do not distinguish the urgency of tasks, so for users Say, interactivity sucks. Therefore, these three algorithms are generally suitable for early batch processing systems. Of course, the FCFS algorithm is often used in combination with other algorithms, and it also plays a very important role now. It is suitable for scheduling algorithms used in interactive systems.

Time slice rotation (RR, Round-Robin)

priority scheduling algorithm

Multilevel Feedback Queue Scheduling Algorithm

Guess you like

Origin blog.csdn.net/weixin_43848614/article/details/127132249
Recommended