In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to schedule and switch processes in Linux process management. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
What is scheduling? According to some scheduling algorithm, select the process from the ready queue of the process to CPU.
Why dispatch? In order to maximize the use of CPU.
Scheduling related structure
Task_struct
Let's first bring out the scheduling-related structures in task_struct:
Struct task_struct {. / * * scheduling class. Abstract the scheduler with sched_class * Stop Scheduler: stop_sched_class * Deadline Scheduler: dl_sched_class * RT Scheduler: rt_sched_class * CFS Scheduler: cfs_sched_class * IDLE-Task Scheduler: idle_sched_class * / const struct sched_class * sched_class; / / CFS scheduling entity struct sched_entity se; / / RT scheduling entity struct sched_rt_entity rt;. # ifdef CONFIG_CGROUP_SCHED / / Task Group (a CFS scheduling entity, CFS running queue is maintained on each CPU; RT scheduling entity, RT running queue) struct task_group * sched_task_group; # endif / / DL scheduling entity struct sched_dl_entity dl;. / * * there are 6 scheduling strategies for processes. * deadline process scheduling policy: SCHED_DEADLINE. DL scheduler * real-time process scheduling policy: SCHED_FIFO,SCHED_RR. RT scheduler * ordinary process scheduling strategy: SCHED_NORMAL,SCHED_BATCH,SCHED_IDLE. CFS scheduler * / unsigned int policy;. }
Struct sched_class abstracts the scheduler into five categories:
Stop Scheduler: the highest priority scheduling class that can preempt all other processes and cannot be preempted by other processes
Deadline scheduler: use a red-black tree to sort processes according to absolute deadlines, and select the smallest process to schedule and run
RT Scheduler: maintains a queue for each priority
CFS scheduler: using fully fair scheduling algorithm and introducing the concept of virtual run time
IDLE-Task Scheduler: each CPU has an idle thread that is scheduled to run when there are no other processes to schedule
There are 6 scheduling policies for unsigned int policy processes. Users can call different scheduling policies in the scheduler:
SCHED_DEADLINE: make task select the Deadline scheduler to schedule the operation
SCHED_RR: time slice rotation. When a process runs out of time slices, it adds priority corresponding to the tail of the running queue, giving CPU to other processes with the same priority.
SCHED_FIFO: if there is no time slice for FIFO scheduling and no higher priority, you can only wait for the active release of CPU.
SCHED_NORMAL: make task select the CFS scheduler to schedule the operation
SCHED_BATCH: batch processing, so that task chooses CFS scheduler to schedule operation
SCHED_IDLE: make task choose the CFS scheduler with the lowest priority to schedule operation
Scheduling entity of ordinary non-real-time process dispatched by CFS algorithm in struct sched_entity se;
Real-time scheduling entity scheduled by Roound-Robin or FIFO algorithm in struct sched_rt_entity rt;
Real-time scheduling entity scheduled by EDF algorithm in struct sched_dl_entity dl;
Task assigned to CPU to join the running queue as a scheduling entity
Runqueue run queue struct rq {. / / three scheduling queues: CFS scheduling, RT scheduling, DL scheduling struct cfs_rq cfs; struct rt_rq rt; struct dl_rq dl;. / / idle points to idle kernel threads, and stop points to migrating kernel threads struct task_struct * curr, * idle, * stop;. }
Three scheduling queues:
Struct cfs_rq cfs; CFS scheduling queue
Struct rt_rq rt; RT scheduling queue
Struct dl_rq dl; DL scheduling queue
Each CPU has a running queue, and there are three scheduling queues in each running queue. Task is added to the respective scheduling queue as a scheduling entity.
Scheduling process
The essence of scheduling is to choose the next process to run. The scheduling process is divided into two steps:
1. Set scheduling flag
Set the TIF_NEED_RESCHED for the flags member in the thread_info structure of the running process on CPU.
So, when do you set up TIF_NEED_RESCHED?
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Scheduler_tick clock interrupt
When wake_up_process wakes up the process
When do_fork creates a new process
When smp_send_reschedule load balancing
When set_user_nice modifies the nice value of the process
In all of the above cases, the flags member in the process thread_info structure is set to TIF_NEED_RESCHED through resched_curr. Take scheduler_tick and wake_up_process as examples:
The basis for setting up TIF_NEED_RESCHED involves a specific scheduling algorithm, which will be discussed in more detail when we talk about the specific scheduler.
two。 Execution scheduling
Kernel determines whether the current process tag is TIF_NEED_RESCHED. If so, call the schedule function to switch the context. Kernel space can be preempted, and user space cannot be preempted. Preemption can be divided into kernel preemption and user preemption.
User mode preemption
Ret_to_user is a function that will be called after system call, exception trigger, and interrupt handling.
Kernel state preemption
Process switching context context_switch
From the above we know that the scheduling occurs in the _ schedule function.
The focus is on two of these functions, one is to select the pick_next_task that needs to switch tasks, and the other is to complete the process context switch context_switch.
The strategy of selecting task involves different scheduling classes. We will expand it when we talk about the specific scheduler. Here, we focus on the function context_switch of context switching. Process context switching mainly involves two main processes: process address space switching and processor state switching:
Address space switching of a process
Convert the pgd virtual address of the next process into a physical address and store it in ttbr0_el1 (this is the user space page table base address register). When accessing the user space address, mmu will use this register to traverse the page table to get the physical address. When this step is completed, the address space switching of the process is completed, that is, the virtual address space switching of the process.
Register state switching
X19-x28 is the register specified by the arm64 architecture to be called. You can see that when the processor state is switched, the x19murx28th fpjiggery SPREPC of the previous process (prev) is saved to the cpu_contex of the process descriptor, and then the x19murx28mfpje SPL PC of the cpu_contex of the process (next) descriptor to be executed is restored to the corresponding register. And the process descriptor task _ struct address of the next process is stored in sp_el0, which is used to find the current process through current, so that the state switch of the processor is completed.
Linux process management on how to schedule and process switching to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.