Process scheduling function schedule () analysis

Source: Internet
Author: User

1, Function Brief: The main function is Select one of the highest priority processes from the ready process to run in place of the current process。 2. Code Analysisschedule ();
struct Task_struct *tsk = current; //current is the current processsched_submit_work (TSK); //Avoid deadlocks__schedule (); //This is the main function of the dispatch. static void __sched __schedule (void) {struct task_struct *prev, *next; unsigned long *switch_count; struct RQ *rq; int CPU ; Need_resched:preempt_disable (); //Off kernel preemption, see note 1 for kernel preemption CPU = smp_processor_id ();RQ = CPU_RQ (CPU); //Runqueue information related to the current process is stored in RQRcu_note_context_switch (CPU); Prev = rq->curr;  //Current process into PrevSchedule_debug (prev);  if (Sched_feat (Hrtick)) hrtick_clear (RQ);  RAW_SPIN_LOCK_IRQ (&rq->lock); Switch_count = &prev->nivcsw;  / /If the kernel state is not preempted and the kernel preemption is validif (prev->state &&!) ( Preempt_count () & preempt_active)) {                  //If the current process has a non-blocking wait signal, and its status is Task_interruptibleif (Unlikely (Signal_pending_state (Prev->state, prev))) {prev->state = task_running; //Set the status of the current process to: task_running} else {Deactivate_task (RQ, Prev, dequeue_sleep); //To move the current process from Runqueue (run queue) Delete PREV->ON_RQ = 0; //identifies that the current process is not in Runqueue                          //This involves the knowledge of the work queue, which we have in later chapters, which are skipped hereif (Prev->flags & pf_wq_worker) {struct task_struct *to_wakeup; To_wakeup = wq_worker_sleeping (prev, CPU); if (to_wakeup)try_to_wake_up_local (to_wakeup);  }} Switch_count = &prev->nvcsw; } pre_schedule (RQ, prev);if (unlikely (!rq->nr_running)) //If There are no running processes in the Runqueue idle_balance (CPU, RQ); //Will pull the process from other CPUsPut_prev_task (RQ, prev); //Notifies the scheduler that the current process will be replaced by another process, ready next = Pick_next_task (RQ); //From Select the most appropriate process in the Runqueue clear_tsk_need_resched (prev); //Clears the rescheduling identity of the current processrq->skip_clock_update = 0;        //Whether the current process is the same process as the selected process and does not belong to the same process to switchif (likely (prev! = next)) {rq->nr_switches++; Rq->curr = next; //Selected process in place of the current process++*switch_count;Context_switch (RQ, Prev, next); //responsible for the underlying context switchCPU = smp_processor_id (); RQ = CPU_RQ (CPU); } elseRaw_spin_unlock_irq (&rq->lock); //If you do not need to switch processes, you only need to unlockPost_schedule (RQ); Sched_preempt_enable_no_resched (); if (need_resched ()) goto need_resched;} Note 1:  Kernel preemption Basics1. Kernel preemption ConceptWhen a process is in kernel space and there is a higher priority task, if the kernel supports preemption, you can suspend the current task and perform higher priority tasks! 2, the concept of user preemptionuser preemption Occurs when the kernel is about to return to user space, and if the need resched flag is set, it causes schedule () to be called. The kernel checks the need resched flag whether it is returned from an interrupt handler or after a system call. If it is set, then the kernel chooses a different (more appropriate) process to run.  3. Kernel preemption BenefitsFirst, this is what the real-time system requires. Imagine, if the hardware interrupt to open a real-time process, if the kernel does not support preemption, the real-time process is opened to wait until the current process execution to be dispatched, which brings a delay, bad for real-time! If the kernel supports preemption, it is possible to suspend the current process to execute real-time processes, which is beneficial for real-time performance!  4, under what circumstances can not seize the kernel(1) The kernel is processing interrupt(2) The kernel is in the process of interrupting the context of the bottom half (the bottom half of the interrupt)(3) The kernel code snippet is holding spinlock spin lock, Writelock/readlock read-write lock and other locks, in the protection state of these locks. (4) The kernel is executing the scheduler scheduler, this situation is corresponding to our schedule function analysis!!! (5) The kernel is operating a "private" data structure for each CPUto ensure that the Linux kernel is not preempted in the above scenario, the preemption kernel uses a variable preempt_count called a kernel preemption lock. This variable is set in the PCB structure task_struct of the process. Each time the kernel enters these states, the variable Preempt_ count is incremented by 1, indicating that the kernel does not allow preemption. Whenever the kernel exits from the above several states, the variable Preempt_ count is reduced by 1, while the preemptive judgment and dispatch are performed.

Process scheduling function schedule () analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.