CPU scheduling (process scheduling) strategy

Due to the different requirements of the operating system in different usage scenarios, the CPU scheduling strategy itself is not a definite answer. For example, operating systems on missiles and satellites generally adopt real-time scheduling. Take the PC as an example here to briefly describe the CPU scheduling strategy of the operating system on the PC.

I. Introduction

Process state transition :

(1) Ready → execute

A process in the ready state, when the CPU schedules the execution of the process, the process changes from the ready state to the execution state.

(2) Execute → Ready

During its execution, the process in the execution state has to give up the CPU because a time slice allocated to it has been used up, so the process changes from the execution state to the ready state.

(3) Execute → block

When the executing process cannot continue to execute because it is waiting for some event to occur, it changes from the executing state to the blocking state.

(4) Blocking → Ready

A process in a blocked state, if the event it is waiting for has occurred, the process changes from the blocked state to the ready state.

2. Factors to consider for CPU scheduling

(1) Turnaround time

周转时间: The time from process submission to process completion . Including the time waiting to enter the memory, the time waiting in the ready queue, the time of IO operations, and the time of CPU running calculations.

How to shorten the turnaround time? ——SJF短作业优先策略

Insert picture description here

(2) Response time

响应时间: The time for the computer to respond to user actions

How to effectively control the response time of each process in the memory? ——时间片轮转调度(RR)

Scheduling CPU resources to each process according to the time slice, the size of the time slice needs to be compromised within a certain range: 10-100ms
if the time slice is too large, the response time will be too long, and if it is too small, the throughput will be too small.

(3) How to find a compromise between turnaround time and response time

The current stage and background tasks exist when (foreground task multi-IO, the emphasis response time; background task and more calculated, emphasizing the turnaround time), we not only need 时间片轮转调度to ensure that the response time, but also needs to be set on this basis 动态优先级策略, to consider 短作业优先, The 一定程度上foreground task is executed first.

Three.Schedule scheduling function example (exquisite code under linux0.11)

/* this is the scheduler proper: */

	while (1) {
    
    
		c = -1;
		next = 0;
		i = NR_TASKS; 
		p = &task[NR_TASKS];
		/*遍历就绪态进程,同时寻找counter值最大的进程。如果最大的counter值>0,
		那么跳出循环执行switch_to(next),该进程由就绪态转变为执行态*/
		while (--i) {
    
    
			if (!*--p)
				continue;
				/*counter在其中既有时间片的作用(counter=0即意味着CPU分配的时间片
				个数降为了0),还有执行优先级策略的作用(counter值越大优先级越高)*/
			if ((*p)->state == TASK_RUNNING && (*p)->counter > c)
				c = (*p)->counter, next = i;
		}
		if (c) break;
		/*当所有就绪态的时间片都用完的时候,遍历重置所有进程的时间片大小:
		就绪态时间片大小重置为初值;阻塞态时间片大小不断增加(最终收敛于
		初值大小的两倍)*/
		for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
			if (*p)
			{
    
    
				(*p)->counter = ((*p)->counter >> 1) + (*p)->priority;
			}
	}
	switch_to(next);
}

In the Schedule function, the same counter variable is used to achieve the functions of time slice rotation scheduling and dynamic priority calling at the same time . The longer the blocking thread is blocked, the more times the counter size will be reset, and the value of the counter will be larger and larger, so the priority will naturally be higher and higher.

Simple and clear, wonderful!

Regarding the role of the counter variable, there is another subtle point: the execution of the background process is completely scheduled according to the priority strategy controlled by the counter, and each time the time slice is exhausted, it corresponds to the reset of the counter, so that the continuous rotation, the final The effect is similar to SJF's short assignment priority strategy.

Guess you like

Origin blog.csdn.net/m0_46550452/article/details/108107575