[Operating System] 2.4 Process Management (Scheduling Algorithm)

Evaluation index of scheduling algorithm

  1. CPU utilization

    Because the CPU is very expensive, people want the CPU to do as much work as possible

    • CPU utilization: Refers to the ratio of CPU busy time to total time
    • Utilization = busy time / total time
  2. System throughput

    For computers, it is hoped that as much work as possible can be processed in as little time as possible

    • System throughput: The number of jobs completed per unit time
    • System throughput = total number of jobs completed / total time spent
  3. Turnaround time

    For computer users, they are very concerned about how much time it took to complete their work from submission to completion

    • Turnaround time: The time interval from the user job submission to the start of the system and the completion of the job execution
    • It consists of four parts: (the last three items may occur multiple times during the entire processing of a job)
      1. The time that the job is waiting on the external storage backup queue while the job is scheduled
      2. The time the process waits for the process to be scheduled on the ready queue
      3. The execution time of the process on the CPU
      4. The time the process waits for the completion of the I/O operation
    • Turnaround time = job completion time-job submission time (For users, they are more concerned about their unit job turnaround time)
    • Average Turnaround Time = Sum of Turnover Time of Each Job / Number of Jobs (For the operating system, I care more about the overall performance of the system)

      Thinking : Some jobs have a short running time and some have a long running time. Therefore, under the same turnaround time, jobs with different running times will give users a different experience.
      For example , it takes 1min for A to run, and 10min for B to run. If A first After B, then B needs to wait 1min compared to the 10min required to run, which is relatively acceptable; if B first and then A, then A needs to wait 10min, compared to the 1min required to run, the user experience of A is relatively poor,
      so the introduction The concept of weighted turnaround time :
      Weighted turnaround time = Job turnaround time / actual running time of the job = (job completion time-job submission time) / actual running time of the job
      Average weighted turnaround time = Sum of weighted turnaround time for each job / number of jobs

      1. Weighted turnaround time must be> 1
      2. Weighted turnaround time and turnaround time are both as small as possible (when the turnaround time is the same, the actual running time is larger, and the user satisfaction is higher; when the actual running time is the same, the turnaround time is smaller, and the user satisfaction is higher)
  4. waiting time

    Computer users want to do their homework as little as possible waiting for the processor

    • waiting time: Refers to the sum of the time the process/job is waiting for the processor or I/O device to serve it . The longer the waiting time, the lower the user satisfaction.
    • distinguish:
      1. forprocessIn other words: the waiting time refers to the sum of the time waiting to be served after the process is established. In fact, the process is also being served while waiting for the completion of I/O , so the waiting time is not counted.
      2. foroperationIn other words: not only the waiting time after the process is established, but also the time waiting for the job to be transferred into the memory on the external storage backup queue
    • How long a job needs to be served by the CPU in total, and how long it is served by the I/O device is actually fixed.The scheduling algorithm really affects only the waiting time of the job/process
  5. Response time

    For computer users, it is hoped that the request submitted by themselves (such as entering a debugging command through the keyboard) will be served and responded to by the system as soon as possible

    • Response time: The time taken from the time the user submits the request to the first response

Scheduling Algorithm

1. First come first served (FCFS)

  1. Algorithmic thinking: mainly from the perspective of "fairness" (queue to buy things)
  2. Algorithm rules: perform services in the order of arrival of jobs/processes
  3. Used for job/process scheduling: when used for job scheduling, consider which job arrives at the backup queue first ; when used for process scheduling, consider which process arrives at the ready queue first
  4. Can it be preempted? Non-preemptive
  5. Advantages: fairness, simple algorithm implementation
  6. Disadvantages: short jobs/processes that are ranked behind long jobs/processes need to wait a long time , and the weighted turnaround time is very long (as in the exampleP3Process, the weighted turnaround time is 8 , it takes less time to run, but it has to wait a long time, the experience is poor), the user experience is very bad for short jobs, the weighted turnaround time is very large, for short jobs users The experience is not good, that is, the first-come, first-served algorithm⭐Good for long jobs but bad for short jobs
  7. Will it cause starvation (a job/process cannot be served for a long time) : No
    Insert picture description here
    note: The processes in this example are purely computational processes. A process is either waiting or running. in the case ofThere are calculations and I/O operations, Its waiting time = turnaround time-running time- I/O operation time

2. Short job priority (SJF)

  1. Algorithm idea: Pursue the shortest average waiting time, the least average turnaround time, and the least average weighted turnaround time
  2. Algorithm rules: choose every time you scheduleHas arrivedAndShortest running time Job/process
  3. Used for job/process scheduling: It can be used for job scheduling or process scheduling. It is called "short process first (SPF) algorithm" when used for process scheduling
  4. Can it be preempted? Both SJF and SPF are non-preemptive algorithms, but there are also preemptive ones—the shortest remaining time first algorithm (SRTN)
  5. Advantages: "shortest" average waiting time, average turnaround time
  6. Disadvantages: unfair,⭐Good for short jobs but bad for long jobs. In addition, the running time of the job is provided by the user, which is not necessarily true, and may not really give priority to short jobs.
  7. Will it cause hunger:⭐May cause hunger, long-term work may not be served for a long time, If you can’t get services all the time, it’s called "starved to death"
  • 2.1 Non-preemptive
    Insert picture description here

  • 2.2 Preemptive:
    Also known as "Shortest remaining time first algorithm"(SRTN): Every time a process is added to the ready queue, it needs to be scheduled. If the newly arrived basic remaining time is shorter than the remaining time of the currently running process, the new process will preempt the processor, and the current running process will return to Ready queue. In addition, when a process is completed, it also needs to be scheduled
    Insert picture description here
    Insert picture description here

  • note

    1. If the topic is a description, the default "short job/process priority" is non-preemptive
    2. "The average waiting time and average turnaround time of the SJF scheduling algorithm are the least." Strictly speaking, this statement is wrong. In the example above, the average waiting time and average turnaround time obtained by the shortest remaining time first algorithm (SRTN) are even more important. less
      1. One condition should be added:
        When all processes are runnable at the same time, The average waiting time and average turnaround time of the SJF scheduling algorithm are the least
        orWhen all processes arrive almost simultaneously, SJF scheduling algorithm has the least average waiting time and average turnaround time
      2. If no preconditions are added, it should be said:
        the average waiting time and average turnaround time of the preemptive short-job priority scheduling algorithm (the shortest remaining time priority algorithm) is the least
    3. Although strictly speaking, "SJF scheduling algorithm has the least average waiting time and average turnaround time" is not rigorous, but compared to other algorithms (such as FCFS), SFJ can still get less average waiting time and average turnaround time. , So when it appears in the options, if other options can be excluded, the option can be selected

3. High response ratio first (HRRN)

  1. Algorithm idea: To consider the job / process of waiting time and the required time services (run time)
  2. Algorithm rules: at each dispatchFirst calculate the response ratio of each job/process, and select the job/process with the highest response ratio to serve itResponse ratio = (waiting time + required service time)/required service time, Response ratio> 1
  3. Used for job/process scheduling: both for job scheduling and process scheduling
  4. Can it be preempted? Non-preemptive algorithm. becauseOnly when the currently running job/process actively abandons the processor (normally/abnormally completed, or actively blocked) does it need to be scheduled, and it is also necessary to calculate the response ratio
  5. advantage:Comprehensive consideration of waiting time and running time (required service time)
    1. When the waiting time is the same : The priority of the short service time is required (the advantage of SJF)
    2. When the same service time is required : the longer waiting time is preferred (FCFS priority)
    3. For long jobs , with the length of the waiting time, the response ratio will also increase, thus avoiding the problem of starvation.
  6. Disadvantages:
  7. Will it cause hunger: No
    Insert picture description here

⭐Summary

Insert picture description here

  • note:
    These algorithms focus on the indicators of user fairness, average turnaround time, and average waiting time to evaluate the overall performance of the system, butIt doesn’t care about the response time, nor does it distinguish the urgency of the task, so for the userInteractivity is terrible. So these three algorithmsSuitable for early batch processing systems. Of course, the FCFS algorithm industry is often used in combination with other algorithms

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

  1. Algorithm idea: Serve each process fairly and circulately, so that each process can get a response within a certain time interval
  2. Algorithm rules: According to the order in which each process arrives in the ready queue,Let each process execute a time slice (such as 100ms) in turn. If the process does not finish executing within a time slice, deprive its processor and put the process back inThe end of the ready queue, Re-queue
  3. Used for job/process scheduling: only used for process scheduling (only when the job is put into memory and the corresponding process is established, can it be allocated processor time slices
  4. Can it be preempted?Preemptive algorithm. If the execution is not completed within a time slice, the processor is deprived of. byThe clock device issues a clock interruptTo notify the CPU that the time slice has arrived
  5. Advantages : fairness, fast response, suitable for time-sharing systems
  6. Disadvantages : Due to the high frequency of process switching, there is a certain time overhead; the task urgency of the process is not distinguished
  7. Will it cause hunger: No
  • Note : Often used in time-sharing operating systems , more attention is paid to "response time" , so turnaround time is not calculated
  • Time slice size is 2:
    Insert picture description here
    Insert picture description here

Insert picture description here

  • Time slice size is 5 o'clock:

Insert picture description here

  • ⭐⭐Note:
    1. If the time slice is too large so that all processes can be completed in one time slice, the time slice round-robin scheduling algorithm is First come first serve Scheduling algorithm and will Increase process response time(For example: There are 10 concurrently executing processes in the system. If the time slice is 1 second, it may take 9 seconds for a process to be responded ... For example: If the user issues a debugging command through the keyboard outside the time slice of his own process , You may need to wait 9 seconds for the command to be responded to by the system). Therefore, the time slice cannot be set too large
    2. If the time slice is too small, yes Lead to frequent process switchingThe system will spend a lot of time(It takes time to save and restore the operating environment ) to handle process switching, soLead to a reduction in the proportion of time actually used for process execution. So the time slice cannot be too small

5. Priority scheduling algorithm

  1. Algorithm idea: With the development of computers, especially the time of real-time operating systems, more and more application scenarios need to determine the processing sequence according to the urgency of the task
  2. Algorithm rules: Each job/process has its own priority, and the job/process with higher priority is selected when scheduling
  3. Used for job/process scheduling: It can be used for job and process scheduling, and even used for I/O scheduling
  4. Can it be preempted? Both preemptive and non-preemptive types are available;
    1. Non-preemptive:Only when the process actively abandons the processorSchedule
    2. Preemptive: Also need to be when the ready queue changesTo check whether preemption will occur.
  5. advantage: Use priority to distinguish the urgency of tasks , suitable for time-sharing operating systems . Can flexibly adjust various operations / processes of the degree of preference
  6. Disadvantage: If a steady stream of high-priority processes arrive, it may cause starvation of low-priority processes
  7. Will it cause hunger: Yes
  • Preemptive:
    Insert picture description here

  • Non-preemptive:
    Insert picture description here

  • supplement:

    1. There may not be only one ready queue , and it can be organized according to different priorities. In addition, you can also arrange higher-priority processes closer to the head of the team
    2. According to whether the priority can be changed dynamically, the priority can be classified:
      1. Static priority: The creation process is determined, and it has not changed since then
      2. Dynamic priority: There is an initial value when creating a process, and then the priority will be dynamically adjusted according to the situation
    3. Thought 1: How to set the priority of various processes?
      1. System processHas a higher priority thanUser process
      2. Foreground processHas a higher priority thanbackstage process
      3. Operating system is more preferredI/O type process(Or CPU busy process) (as opposed to computing process)

        I/O devices and CPU can work in parallel. If I/O busy processes are given priority to run, the more likely I/O devices will be put into work as soon as possible, and resource utilization and system throughput will be improved.

    4. Thinking 2: If dynamic priority is adopted , when should it be adjusted ?
      1. Can be pursued fromfair, PromoteResource utilizationPerspective
      2. If a processWaited a long time in the ready queue, You can appropriatelyPromoteIts priority
      3. If a processTakes up the processor for a long time, You can appropriatelyreduceIts priority

6. Multi-level feedback queue scheduling algorithm

Thinking:
The advantage of FCFS algorithm is that the advantage of fair
SJF algorithm is that it can process short jobs as soon as possible, and the parameters such as average waiting time/average turnaround time are excellent. The
time slice rotation scheduling algorithm allows each process to get a timely response.
Priority scheduling algorithm Can flexibly adjust the opportunity for each process to be served.
Question:
Can an algorithm be found that can compromise and get a balanced algorithm with excellent performance?

  1. Algorithm idea: a trade-off with other algorithms
  2. Algorithm rules:
    1. Set upMulti-level priority queue, The priority of the queues at all levels is from high to low, and the time slice is from small to large
    2. When a new process arrives, it enters the first level queue first, According to the principle of FCFS to queue up and wait for the time slice
    3. If the process has not been completed after the time slice is used up, it will be transferred to the end of the next level of queue(If at this timeAlready in the lowest level of the queue, then put it back to the end of the current queue
    4. Only when the kth level queue is empty, will the process of the k+1 level queue be allocated a time slice
    5. The process of the preempted processor,Re-hang to the end of the current queue
  3. Used for job/process scheduling: used forProcess scheduling
  4. Can it be preempted?Preemptive algorithm——During the operation of the k-level queue process, if a new process is entered in the higher-level queue (level 1~k-1) , the new process will preempt the processor because the new process is in the higher-level queue . preempted processor process, the re-linked to the current queue (k level) the tail of the
  5. advantage:
    1. Relatively fair to each process (the advantages of FCFS)
    2. Each newly arrived process can be responded quickly (the advantage of RR)
    3. Short process can be completed in less time (the advantage of SPF)
    4. No need to estimate the running time of the process (to avoid user fraud)
    5. The opportunity for each process to be serviced can be flexibly adjusted (for example: set I/O-type process preference: the process blocked by I/O can be put back to the end of the current queue, so that its higher priority can be maintained)
  6. Disadvantages:
  7. Will it cause hunger:meeting(There are a steady stream of short processes arriving, and short processes are constantly executing in the high-level queue. Those processes that are transferred to the low-level queue may not get time slices for a long time)
    Insert picture description here
    Insert picture description here

⭐Summary

Insert picture description here

  • note:
    Compared with the early batch processing system computers, due to the substantial reduction in computer cost, the interactive operating systems (including time-sharing and real-time operating systems) that appeared later paid more attention to the system's response time, fairness, and balance. So these three algorithmsSuitable for interactiveOperating system . (For example, UNIX uses a multi-level feedback queue scheduling algorithm)

Guess you like

Origin blog.csdn.net/Qmilumilu/article/details/112689155
Recommended