PS: In a multi-process concurrent environment, although conceptually, there are multiple processes executing at the same time, but under a single CPU, at any time Only one process can be in the execution state, and the other processes are in the non-execution state. So the question is, how do we determine which process is executed at any time and which is not executed? This involves an important part of process management: process scheduling, follow this article to review process scheduling together!
First, process scheduling basics
1.1 Process scheduling definition
Process schedulingis an important part of operating system process management, its The task is to select the next process to run.
1.2 Process scheduling goals
First of all, the general program tasks are divided into three types: CPU computing intensive For different types of programs, scheduling needs to achieve different goals. For IO-intensive, response time is the most important; for CPU-intensive, turnaround time The most important; and for the balanced type, a balance between response and turnover is more important.
Therefore, the goal of process scheduling is to minimize the average response time, maximize the system throughput, keep the various functional components of the system busy, and provide a seemingly fair mechanism .
PS: Why do you want to keep all the functional parts of the system busy? Because the CPU is very expensive, leaving it idle is a waste, so keeping the CPU busy is very important. Just as life is very precious, we will not waste our lives unless we keep learning to stay fulfilled.
2. Basic scheduling algorithm
2.1 First come first serve algorithm
First-come, first-served (FCFS) algorithm is one of the most common algorithms, it is a fair concept in human nature. The advantage is that simple and easy to implement, the disadvantage is that short jobs may become Very slow, because there is a long work in front of it, which will cause the user’s interactive experience to be relatively poor.
For example, when queuing for business, the business you want to handle can be done in a few minutes, but the one person in front of you is very complicated and takes 1 hour, then you need to be behind him Waiting for a long time, so you think: if everyone takes turns to handle 10 minutes of affairs, then how great! So the time slice rotation algorithm appeared.
2.2 Time slice rotation algorithm
Time slice rotation is an improvement of the FCFS algorithm. Its main purpose is to improve the response time of short programs. The implementation method is Periodic process switching. Time slice rotation the focus is on the choice of time slice, and many factors need to be considered: if it’s running When there are many processes, the time slice needs to be shorter; when the number of processes is small, the time slice can be appropriately longer. Therefore, the choice of time slice is a comprehensive consideration, weighing the interests of all parties and making an appropriate compromise.
However, the system response time of time slice rotation is not always shorter than the response time of FCFS. Time slice rotation is a kind of big pot meal, but in real life it is taking the route of “some people get rich first, get rich first lead to get rich later”. For example, if there are 30 tasks, one of the tasks only takes 1 second to execute, while the other 29 tasks take 30 seconds to execute. If for some reason, this one-second task ranks behind the other 29 tasks. Rotation, it needs to wait for 29 seconds to execute (assuming the time slice is 1 second). As a result, the response time and interactive experience of this task become very poor. Therefore, the short-task priority algorithm is proposed.
2.3 Short task priority algorithm
The core of the short task priority algorithm is All tasks are not all the same, but are differentiated by priority. Specifically, short tasks have higher priority than long tasks span>, and we always schedule high priority tasks to run first.
Short task priority algorithm is divided into two types: one is non-preemptive, the other is Preemptive. Non-preemptive type When a task that is already running on the CPU ends or is blocked, from candidate tasks Select the process with the shortest execution time to execute. and preemptive is every time a new process is added, all processes (including processes running on the CPU) need to be checked , Whoever runs in a short time.
Since short tasks always run the program with the shortest execution time, the average system response time is in the above algorithms Medium is the best, which is also the advantage of the short-task priority algorithm. But the short task priority algorithm also has disadvantages: First, it may cause long tasks to fail to get CPU time and cause “muscle hungry”. The second is How do I know how long each process needs to run? So in order to solve the first shortcoming, a priority scheduling algorithm was proposed. The second shortcoming can be estimated by some heuristic methods, and many artificial intelligence algorithms can do this.
2.4 Priority scheduling algorithm
Priority scheduling algorithm gives each process a priority, and each process needs to be switched When, find a process with the highest priority for scheduling. In this way, if a long process is given a high priority, the process will no longer be “starved”. In fact, the short task priority algorithm itself is a priority scheduling, but it gives higher priority to short processes.
The advantage of this algorithm is that it can give important processes a high priority to ensure that important tasks can get CPU time, its disadvantages There are two: one is low-priority processes may be “starved”, and the other is Response time cannot be guaranteed. The first shortcoming can be solved by dynamically adjusting the priority of the task. For example, if a process waits for too long, its priority will surpass the priority of other processes due to continuous improvement. Level to get the CPU time. The second shortcoming can be solved by setting the priority of a process to the highest, but even if the priority is set to the highest, if everyone sets their process priority to the highest, the response time is still not guaranteed.
2.5 Hybrid scheduling algorithm
The previous algorithms have certain shortcomings, so can there be a hybrid algorithm? Their advantages, abandon their shortcomings, this is the so-called hybrid scheduling algorithm. The hybrid scheduling algorithm divides all processes into different categories, and each category is a priority. If two processes are in different categories, the process in the higher priority category will be executed first; if they are in For the same category, the time slice rotation algorithm is used to execute. The schematic diagram of the hybrid scheduling algorithm is shown in the figure below:
2.5 Process scheduling process
< img alt="share picture" style="display: block; margin-left: auto; margin-right: auto;" src="/wp-content/uploads/images/os/ostheory/1626797042241.png" > p>
3. Priority inversion for scheduling exceptions
3.1 What is priority inversion?
Priority inversion refers to is a low-priority task holding a shared resource required by a high-priority task. In this way, high-priority tasks are blocked due to lack of resources until the low-priority tasks release resources. This actually caused the priority of these two tasks to be upside down.
3.2 The manifestation of priority inversion
(1)Low priority processes that do not hold resources hinder the need for resources The execution of the high-priority process of ;
(2)The priority process that holds the resource hinders the high-priority process that needs the resource The execution of the process;
3.3 Prevention of priority inversion
(1) For the first form, you can use The method of interrupt prohibition, its core is to protect the critical section by disable interruption.
(2) For the second form, cannot let low-priority processes hold the resources needed by high-priority processes , You can through the upper limit of priority and priority inheritance to achieve.
Priority upside down problem, we will learn in detail later, and then we will talk about it in detail. Of course, If you want to know what’s going on, please see the next breakdown.
Reference materials
Zou Hengming, “The Philosophical Principles of Operating System”, Machinery Industry Press
Author: Zhou Xulong
Source: http://edisonchou.cnblogs.com
PS: In a multi-process concurrent environment, although conceptually, there are multiple processes executing at the same time, but under a single CPU, only one process can be in execution at any time, and other processes are in non-execution state. So the question is, how do we determine which process is executed at any time and which is not executed? This involves an important part of process management: process scheduling, follow this article to review process scheduling together!
First, process scheduling basics
1.1 Process scheduling definition
Process schedulingis an important part of operating system process management, its The task is to select the next process to run.
1.2 Process scheduling goals
First of all, the general program tasks are divided into three types: CPU computing intensive For different types of programs, scheduling needs to achieve different goals. For IO-intensive, response time is the most important; for CPU-intensive, turnaround time The most important; and for the balanced type, a balance between response and turnover is more important.
Therefore, the goal of process scheduling is to minimize the average response time, maximize the system throughput, keep the various functional components of the system busy, and provide a seemingly fair mechanism .
PS: Why do you want to keep all the functional parts of the system busy? Because the CPU is very expensive, leaving it idle is a waste, so keeping the CPU busy is very important. Just as life is very precious, we will not waste our lives unless we keep learning to stay fulfilled.
2. Basic scheduling algorithm
2.1 First come first serve algorithm
First-come, first-served (FCFS) algorithm is one of the most common algorithms, it is a fair concept in human nature. The advantage is that simple and easy to implement, the disadvantage is that short jobs may become Very slow, because there is a long work in front of it, which will cause the user’s interactive experience to be relatively poor.
For example, when queuing for business, the business you want to handle can be done in a few minutes, but the one person in front of you is very complicated and takes 1 hour, then you need to be behind him Waiting for a long time, so you think: if everyone takes turns to handle 10 minutes of affairs, then how great! So the time slice rotation algorithm appeared.
2.2 Time slice rotation algorithm
Time slice rotation is an improvement of the FCFS algorithm. Its main purpose is to improve the response time of short programs. The implementation method is Periodic process switching. Time slice rotation the focus is on the choice of time slice, and many factors need to be considered: if it’s running When there are many processes, the time slice needs to be shorter; when the number of processes is small, the time slice can be appropriately longer. Therefore, the choice of time slice is a comprehensive consideration, weighing the interests of all parties and making an appropriate compromise.
However, the system response time of time slice rotation is not always shorter than the response time of FCFS. Time slice rotation is a kind of big pot meal, but in real life it is taking the route of “some people get rich first, get rich first lead to get rich later”. For example, if there are 30 tasks, one of the tasks only takes 1 second to execute, while the other 29 tasks take 30 seconds to execute. If for some reason, this one-second task ranks behind the other 29 tasks. Rotation, it needs to wait for 29 seconds to execute (assuming the time slice is 1 second). As a result, the response time and interactive experience of this task become very poor. Therefore, the short-task priority algorithm is proposed.
2.3 Short task priority algorithm
The core of the short task priority algorithm is All tasks are not all the same, but are differentiated by priority. Specifically, short tasks have higher priority than long tasks span>, and we always schedule high priority tasks to run first.
Short task priority algorithm is divided into two types: one is non-preemptive, the other is Preemptive. Non-preemptive type When a task that is already running on the CPU ends or is blocked, from candidate tasks Select the process with the shortest execution time to execute. and preemptive is every time a new process is added, all processes (including processes running on the CPU) need to be checked , Whoever runs in a short time.
Since short tasks always run the program with the shortest execution time, the average system response time is in the above algorithms Medium is the best, which is also the advantage of the short-task priority algorithm. But the short task priority algorithm also has disadvantages: First, it may cause long tasks to fail to get CPU time and cause “muscle hungry”. The second is How do I know how long each process needs to run? So in order to solve the first shortcoming, a priority scheduling algorithm was proposed. The second shortcoming can be estimated by some heuristic methods, and many artificial intelligence algorithms can do this.
2.4 Priority scheduling algorithm
Priority scheduling algorithm gives each process a priority, and each process needs to be switched When, find a process with the highest priority for scheduling. In this way, if a long process is given a high priority, the process will no longer be “starved”. In fact, the short task priority algorithm itself is a priority scheduling, but it gives higher priority to short processes.
The advantage of this algorithm is that it can give important processes a high priority to ensure that important tasks can get CPU time, its disadvantages There are two: one is low-priority processes may be “starved”, and the other is Response time cannot be guaranteed. The first shortcoming can be solved by dynamically adjusting the priority of the task. For example, if a process waits for too long, its priority will surpass the priority of other processes due to continuous improvement. Level to get the CPU time. The second shortcoming can be solved by setting the priority of a process to the highest, but even if the priority is set to the highest, if everyone sets their process priority to the highest, the response time is still not guaranteed.
2.5 Hybrid scheduling algorithm
The previous algorithms have certain shortcomings, so can there be a hybrid algorithm? Their advantages, abandon their shortcomings, this is the so-called hybrid scheduling algorithm. The hybrid scheduling algorithm divides all processes into different categories, and each category is a priority. If two processes are in different categories, the process in the higher priority category will be executed first; if they are in For the same category, the time slice rotation algorithm is used to execute. The schematic diagram of the hybrid scheduling algorithm is shown in the figure below:
2.5 Process scheduling process
< img alt="share picture" style="display: block; margin-left: auto; margin-right: auto;" src="/wp-content/uploads/images/os/ostheory/1626797042255.png" > p>
3. Priority inversion for scheduling exceptions
3.1 What is priority inversion?
Priority inversion refers to is a low-priority task holding a shared resource required by a high-priority task. In this way, high-priority tasks are blocked due to lack of resources until the low-priority tasks release resources. This actually caused the priority of these two tasks to be upside down.
3.2 The manifestation of priority inversion
(1)Low priority processes that do not hold resources hinder the need for resources The execution of the high-priority process of ;
(2)The priority process that holds the resource hinders the high-priority process that needs the resource The execution of the process;
3.3 Prevention of priority inversion
(1) For the first form, you can use The method of interrupt prohibition, its core is to protect the critical section by disable interruption.
(2) For the second form, cannot let low-priority processes hold the resources needed by high-priority processes , You can through the upper limit of priority and priority inheritance to achieve.
Priority upside down problem, we will learn in detail later, and then we will talk about it in detail. Of course, If you want to know what’s going on, please see the next breakdown.
Reference materials
Zou Hengming, “The Philosophical Principles of Operating System”, Machinery Industry Press
Author: Zhou Xulong
Source: http://edisonchou.cnblogs.com
PS: In a multi-process concurrent environment, although conceptually, there are multiple processes executing at the same time, but Under a single CPU, only one process can be in an executing state at any time, while other processes are in a non-executing state. So the question is, how do we determine which process is executed at any time and which is not executed? This involves an important part of process management: process scheduling, follow this article to review process scheduling together!
First, process scheduling basics
1.1 Process scheduling definition
Process schedulingis an important part of operating system process management, its The task is to select the next process to run.
1.2 Process scheduling goals
First of all, the general program tasks are divided into three types: CPU computing intensive For different types of programs, scheduling needs to achieve different goals. For IO-intensive, response time is the most important; for CPU-intensive, turnaround time The most important; and for the balanced type, a balance between response and turnover is more important.
Therefore, the goal of process scheduling is to minimize the average response time, maximize the system throughput, keep the various functional components of the system busy, and provide a seemingly fair mechanism .
PS: Why do you want to keep all the functional parts of the system busy? Because the CPU is very expensive, leaving it idle is a waste, so keeping the CPU busy is very important. Just as life is very precious, we will not waste our lives unless we keep learning to stay fulfilled.
2. Basic scheduling algorithm
2.1 First come first serve algorithm
First-come, first-served (FCFS) algorithm is one of the most common algorithms, it is a fair concept in human nature. The advantage is that simple and easy to implement, the disadvantage is that short jobs may become Very slow, because there is a long work in front of it, which will cause the user’s interactive experience to be relatively poor.
For example, when queuing for business, the business you want to handle can be done in a few minutes, but the one person in front of you is very complicated and takes 1 hour, then you need to be behind him Waiting for a long time, so you think: if everyone takes turns to handle 10 minutes of affairs, then how great! So the time slice rotation algorithm appeared.
2.2 Time slice rotation algorithm
Time slice rotation is an improvement of the FCFS algorithm. Its main purpose is to improve the response time of short programs. The implementation method is Periodic process switching. Time slice rotation the focus is on the choice of time slice, and many factors need to be considered: if it’s running When there are many processes, the time slice needs to be shorter; when the number of processes is small, the time slice can be appropriately longer. Therefore, the choice of time slice is a comprehensive consideration, weighing the interests of all parties and making an appropriate compromise.
However, the system response time of time slice rotation is not always shorter than the response time of FCFS. Time slice rotation is a kind of big pot meal, but in real life it is taking the route of “some people get rich first, get rich first lead to get rich later”. For example, if there are 30 tasks, one of the tasks only takes 1 second to execute, while the other 29 tasks take 30 seconds to execute. If for some reason, this one-second task ranks behind the other 29 tasks. Rotation, it needs to wait for 29 seconds to execute (assuming the time slice is 1 second). As a result, the response time and interactive experience of this task become very poor. Therefore, the short-task priority algorithm is proposed.
2.3 Short task priority algorithm
The core of the short task priority algorithm is All tasks are not all the same, but are differentiated by priority. Specifically, short tasks have higher priority than long tasks span>, and we always schedule high priority tasks to run first.
Short task priority algorithm is divided into two types: one is non-preemptive, the other is Preemptive. Non-preemptive type When a task that is already running on the CPU ends or is blocked, from candidate tasks Select the process with the shortest execution time to execute. and preemptive is every time a new process is added, all processes (including processes running on the CPU) need to be checked , Whoever runs in a short time.
Since short tasks always run the program with the shortest execution time, the average system response time is in the above algorithms Medium is the best, which is also the advantage of the short-task priority algorithm. But the short task priority algorithm also has disadvantages: First, it may cause long tasks to fail to get CPU time and cause “muscle hungry”. The second is how do I know how long each process needs to run? 于是为了解决第一个缺点,优先级调度算法被提出。而第二个缺点则可以采取一些启发式的方法来进行估算,目前很多的人工智能算法都可以做这个事。
2.4 优先级调度算法
优先级调度算法给每个进程赋予一个优先级,每次需要进程切换时,找一个优先级最高的进程进行调度。这样如果赋予长进程一个高优先级,则该进程就不会再“饥饿”。事实上,短任务优先算法本身就是一种优先级调度,只不过它给予短进程更高的优先级而已。
该算法的优点在于可以赋予重要的进程以高优先级以确保重要任务能够得到CPU时间,其缺点则有二:一是低优先级的进程可能会“饥饿”,二是响应时间无法保证。第一个缺点可以通过动态地调节任务的优先级解决,例如一个进程如果等待时间过长,其优先级将因持续提升而超越其他进程的优先级,从而得到CPU时间。第二个缺点可以通过将一个进程优先级设置为最高来解决,但即使将优先级设置为最高,但如果每个人都将自己的进程优先级设置为最高,其响应时间还是无法保证。
2.5 混合调度算法
之前的算法都存在一定缺点,那么可否有一个算法混合他们的优点,摒弃它们的缺点,这就是所谓的混合调度算法。混合调度算法将所有进程分为不同的大类,每个大类为一个优先级。如果两个进程处于不同的大类,则处于高优先级大类的进程优先执行;如果处于同一个大类,则采用时间片轮转算法来执行。 混合调度算法的示意图如下图所示:
2.5 进程调度的过程
三、调度异常之优先级倒挂
3.1 何为优先级倒挂
优先级倒挂指的是一个低优先级任务持有一个被高优先级任务所需要的共享资源。 这样高优先级任务因为资源缺乏而处于受阻状态,一直到低优先级任务释放资源为止。 这样实际上造成了这两个任务的优先级倒挂。
3.2 优先级倒挂的表现形式
(1)不持有资源的低优先级进程阻碍需要资源的高优先级进程的执行;
(2)持有资源的优先级进程阻碍需要资源的高优先级进程的执行;
3.3 优先级倒挂的预防办法
(1)针对第一种形式,可以使用中断禁止的方法,其核心是通过禁止中断来保护临界区。
(2)针对第二种形式,不能让低优先级进程持有高优先级进程所需要的资源,则可以通过优先级上限和优先级继承来实现。
优先级倒挂问题,后面会详细学习,到时再详细说说。当然,欲知后事如何,请看下回分解。
参考资料
邹恒明,《操作系统之哲学原理》,机械工业出版社
作者:周旭龙
出处:http://edisonchou.cnblogs.com
PS:在多进程并发的环境里,虽然从概念上看,有多个进程在同时执行,但在单个CPU下,在任何时刻只能有一个进程处于执行状态,而其他进程则处于非执行状态。那么问题来了,我们是如何确定在任意时刻到底由哪个进程执行,哪些不执行呢?这就涉及到进程管理的一个重要组成部分:进程调度,跟随本篇来一起复习下进程调度吧!
一、进程调度基础
1.1 进程调度定义
进程调度是操作系统进程管理的一个重要组成部分,其任务是选择下一个要运行的进程。
1.2 进程调度目标
首先,一般的程序任务分为三种:CPU计算密集型、IO密集型与平衡(计算与IO各半)型,对于不同类型的程序,调度需要达到的目的也有所不同。对于IO密集型,响应时间最重要;对于CPU密集型,则周转时间最重要;而对于平衡型,进行某种响应和周转之间的平衡就显得比较重要。
因此,进程调度的目标就是要达到极小化平均响应时间、极大化系统吞吐率、保持系统各个功能部件均处于繁忙状态和提供某种貌似公平的机制。
PS:为何说要保持系统各个功能部件均处于繁忙状态? 因为CPU非常昂贵,让其闲置是一种浪费,因此保持CPU繁忙十分重要。就像生命也非常珍贵,我们只有一直保持学习保持充实的状态,才算不浪费生命。
二、基本调度算法
2.1 先来先服务算法
先来先服务(FCFS)算法是一种最常见的算法,它是人的本性中的一种公平观念。其优点就是简单且实现容易,缺点则是短的工作有可能变得很慢,因为其前面有很长的工作在执行,这样就会造成用户的交互式体验也比较差。
例如排队办理业务时,你要办理的业务只需要几分钟就可以办好,但是你前面的一个人办理的事情很复杂需要1个小时,这时你需要在他后面等很久,于是你就想到:要是每个人轮流办理10分钟事务的话,那该多好!于是就出现了时间片轮转算法。
2.2 时间片轮转算法
时间片轮转是对FCFS算法的一种改进,其主要目的是改善短程序的响应时间,实现方式就是周期性地进行进程切换。时间片轮转的重点在于时间片的选择,需要考虑多方因素:如果运行的进程多时,时间片就需要短一些;进程数量少时,时间片就可以适当长一些。 因此,时间片的选择是一个综合的考虑,权衡各方利益,进行适当折中。
但是,时间片轮转的系统响应时间也不一定总是比FCFS的响应时间短。时间片轮转是一种大锅饭的做法,但是现实生活中却是走的“一部分人先富,先富带动后富”的路线。例如,如果有30个任务,其中一个任务只需要1秒时间执行,而其他29个任务需要30秒钟执行,如果因为某种原因,这个只要1秒钟的任务排在另外29个任务的后面轮转,则它需要等待29秒钟才能执行(假定时间片为1秒)。于是,这个任务的响应时间和交互体验就变得非常差。因此,短任务优先算法被提出。
2.3 短任务优先算法
短任务优先算法的核心是所有的任务并不都一样,而是有优先级的区分。具体来说,就是短任务的优先级比长任务的高,而我们总是安排优先级高的任务先运行。
短任务优先算法又分为两种类型:一种是非抢占式,一种是抢占式。 非抢占式当已经在CPU上运行的任务结束或阻塞时,从候选任务中选择执行时间最短的进程来执行。 而抢占式则是每增加一个新的进程就需要对所有进程(包括正在CPU上运行的进程)进行检查,谁的时间短就运行谁。
由于短任务优先总是运行需要执行时间最短的程序,因此其系统平均响应时间在以上几种算法中是最优的,这也是短任务优先算法的优点。但短任务优先算法也有缺点:一是可能造成长任务无法得到CPU时间从而导致“肌饿”。二是如何知道每个进程还需要运转多久? 于是为了解决第一个缺点,优先级调度算法被提出。而第二个缺点则可以采取一些启发式的方法来进行估算,目前很多的人工智能算法都可以做这个事。
2.4 优先级调度算法
优先级调度算法给每个进程赋予一个优先级,每次需要进程切换时,找一个优先级最高的进程进行调度。这样如果赋予长进程一个高优先级,则该进程就不会再“饥饿”。事实上,短任务优先算法本身就是一种优先级调度,只不过它给予短进程更高的优先级而已。
该算法的优点在于可以赋予重要的进程以高优先级以确保重要任务能够得到CPU时间,其缺点则有二:一是低优先级的进程可能会“饥饿”,二是响应时间无法保证。第一个缺点可以通过动态地调节任务的优先级解决,例如一个进程如果等待时间过长,其优先级将因持续提升而超越其他进程的优先级,从而得到CPU时间。第二个缺点可以通过将一个进程优先级设置为最高来解决,但即使将优先级设置为最高,但如果每个人都将自己的进程优先级设置为最高,其响应时间还是无法保证。
2.5 混合调度算法
之前的算法都存在一定缺点,那么可否有一个算法混合他们的优点,摒弃它们的缺点,这就是所谓的混合调度算法。混合调度算法将所有进程分为不同的大类,每个大类为一个优先级。如果两个进程处于不同的大类,则处于高优先级大类的进程优先执行;如果处于同一个大类,则采用时间片轮转算法来执行。 混合调度算法的示意图如下图所示:
2.5 进程调度的过程
三、调度异常之优先级倒挂
3.1 何为优先级倒挂
优先级倒挂指的是一个低优先级任务持有一个被高优先级任务所需要的共享资源。 这样高优先级任务因为资源缺乏而处于受阻状态,一直到低优先级任务释放资源为止。 这样实际上造成了这两个任务的优先级倒挂。
3.2 优先级倒挂的表现形式
(1)不持有资源的低优先级进程阻碍需要资源的高优先级进程的执行;
(2)持有资源的优先级进程阻碍需要资源的高优先级进程的执行;
3.3 优先级倒挂的预防办法
(1)针对第一种形式,可以使用中断禁止的方法,其核心是通过禁止中断来保护临界区。
(2)针对第二种形式,不能让低优先级进程持有高优先级进程所需要的资源,则可以通过优先级上限和优先级继承来实现。
优先级倒挂问题,后面会详细学习,到时再详细说说。当然,欲知后事如何,请看下回分解。
参考资料
邹恒明,《操作系统之哲学原理》,机械工业出版社
作者:周旭龙
出处:http://edisonchou.cnblogs.com
PS:在多进程并发的环境里,虽然从概念上看,有多个进程在同时执行,但在单个CPU下,在任何时刻只能有一个进程处于执行状态,而其他进程则处于非执行状态。那么问题来了,我们是如何确定在任意时刻到底由哪个进程执行,哪些不执行呢?这就涉及到进程管理的一个重要组成部分:进程调度,跟随本篇来一起复习下进程调度吧!
一、进程调度基础
1.1 进程调度定义
进程调度是操作系统进程管理的一个重要组成部分,其任务是选择下一个要运行的进程。
1.2 进程调度目标
首先,一般的程序任务分为三种:CPU计算密集型、IO密集型与平衡(计算与IO各半)型,对于不同类型的程序,调度需要达到的目的也有所不同。对于IO密集型,响应时间最重要;对于CPU密集型,则周转时间最重要;而对于平衡型,进行某种响应和周转之间的平衡就显得比较重要。
因此,进程调度的目标就是要达到极小化平均响应时间、极大化系统吞吐率、保持系统各个功能部件均处于繁忙状态和提供某种貌似公平的机制。
PS:为何说要保持系统各个功能部件均处于繁忙状态? 因为CPU非常昂贵,让其闲置是一种浪费,因此保持CPU繁忙十分重要。就像生命也非常珍贵,我们只有一直保持学习保持充实的状态,才算不浪费生命。
二、基本调度算法
2.1 先来先服务算法
先来先服务(FCFS)算法是一种最常见的算法,它是人的本性中的一种公平观念。其优点就是简单且实现容易,缺点则是短的工作有可能变得很慢,因为其前面有很长的工作在执行,这样就会造成用户的交互式体验也比较差。
例如排队办理业务时,你要办理的业务只需要几分钟就可以办好,但是你前面的一个人办理的事情很复杂需要1个小时,这时你需要在他后面等很久,于是你就想到:要是每个人轮流办理10分钟事务的话,那该多好!于是就出现了时间片轮转算法。
2.2 时间片轮转算法
时间片轮转是对FCFS算法的一种改进,其主要目的是改善短程序的响应时间,实现方式就是周期性地进行进程切换。时间片轮转的重点在于时间片的选择,需要考虑多方因素:如果运行的进程多时,时间片就需要短一些;进程数量少时,时间片就可以适当长一些。 因此,时间片的选择是一个综合的考虑,权衡各方利益,进行适当折中。
但是,时间片轮转的系统响应时间也不一定总是比FCFS的响应时间短。时间片轮转是一种大锅饭的做法,但是现实生活中却是走的“一部分人先富,先富带动后富”的路线。例如,如果有30个任务,其中一个任务只需要1秒时间执行,而其他29个任务需要30秒钟执行,如果因为某种原因,这个只要1秒钟的任务排在另外29个任务的后面轮转,则它需要等待29秒钟才能执行(假定时间片为1秒)。于是,这个任务的响应时间和交互体验就变得非常差。因此,短任务优先算法被提出。
2.3 短任务优先算法
短任务优先算法的核心是所有的任务并不都一样,而是有优先级的区分。具体来说,就是短任务的优先级比长任务的高,而我们总是安排优先级高的任务先运行。
短任务优先算法又分为两种类型:一种是非抢占式,一种是抢占式。 非抢占式当已经在CPU上运行的任务结束或阻塞时,从候选任务中选择执行时间最短的进程来执行。 而抢占式则是每增加一个新的进程就需要对所有进程(包括正在CPU上运行的进程)进行检查,谁的时间短就运行谁。
由于短任务优先总是运行需要执行时间最短的程序,因此其系统平均响应时间在以上几种算法中是最优的,这也是短任务优先算法的优点。但短任务优先算法也有缺点:一是可能造成长任务无法得到CPU时间从而导致“肌饿”。二是如何知道每个进程还需要运转多久? 于是为了解决第一个缺点,优先级调度算法被提出。而第二个缺点则可以采取一些启发式的方法来进行估算,目前很多的人工智能算法都可以做这个事。
2.4 优先级调度算法
优先级调度算法给每个进程赋予一个优先级,每次需要进程切换时,找一个优先级最高的进程进行调度。这样如果赋予长进程一个高优先级,则该进程就不会再“饥饿”。事实上,短任务优先算法本身就是一种优先级调度,只不过它给予短进程更高的优先级而已。
该算法的优点在于可以赋予重要的进程以高优先级以确保重要任务能够得到CPU时间,其缺点则有二:一是低优先级的进程可能会“饥饿”,二是响应时间无法保证。第一个缺点可以通过动态地调节任务的优先级解决,例如一个进程如果等待时间过长,其优先级将因持续提升而超越其他进程的优先级,从而得到CPU时间。第二个缺点可以通过将一个进程优先级设置为最高来解决,但即使将优先级设置为最高,但如果每个人都将自己的进程优先级设置为最高,其响应时间还是无法保证。
2.5 混合调度算法
之前的算法都存在一定缺点,那么可否有一个算法混合他们的优点,摒弃它们的缺点,这就是所谓的混合调度算法。混合调度算法将所有进程分为不同的大类,每个大类为一个优先级。如果两个进程处于不同的大类,则处于高优先级大类的进程优先执行;如果处于同一个大类,则采用时间片轮转算法来执行。 混合调度算法的示意图如下图所示:
2.5 进程调度的过程
三、调度异常之优先级倒挂
3.1 何为优先级倒挂
优先级倒挂指的是一个低优先级任务持有一个被高优先级任务所需要的共享资源。 这样高优先级任务因为资源缺乏而处于受阻状态,一直到低优先级任务释放资源为止。 这样实际上造成了这两个任务的优先级倒挂。
3.2 优先级倒挂的表现形式
(1)不持有资源的低优先级进程阻碍需要资源的高优先级进程的执行;
(2)持有资源的优先级进程阻碍需要资源的高优先级进程的执行;
3.3 优先级倒挂的预防办法
(1)针对第一种形式,可以使用中断禁止的方法,其核心是通过禁止中断来保护临界区。
(2)针对第二种形式,不能让低优先级进程持有高优先级进程所需要的资源,则可以通过优先级上限和优先级继承来实现。
优先级倒挂问题,后面会详细学习,到时再详细说说。当然,欲知后事如何,请看下回分解。
参考资料
邹恒明,《操作系统之哲学原理》,机械工业出版社
作者:周旭龙
出处:http://edisonchou.cnblogs.com
作者:周旭龙
出处:http://edisonchou.cnblogs.com