The trigger and execution time of Linux kernel process scheduling function schedule () __oracle

Source: Internet
Author: User

The kernel's dispatch operation is divided into the trigger and the execution two parts, the trigger only then sets the current process the tif_need_resched flag, the execution time is through the schedule () function to complete the process choice and the switch. The tif_need_resched bit in the thread_info->flags of the current process requires that the schedule () function be invoked to dispatch. The kernel sets the flag in two cases, one that is periodically checked for the clock interruption, and the other is when the awakened process has a higher priority than the running process.

Periodically updates the status of the current task:

Schedule_tick () is invoked in the timed interrupt handler function to handle periodic checks and processing of the schedule, and the call path is the Tick_periodic ()->update_process_times () associated with the clock processing-> Scheduler_tick () or Tick_sched_handle ()->update_process_times ()->scheduler_tick (), mainly used to update the clock for the ready queue, CPU load and current task run time statistics, and so on, as follows:

//linux-3.13/kernel/sched/core.c void Scheduler_tick (void) {int cpu = SMP_PROCESSOR_ID ();                  Gets the current CPU number struct RQ *RQ = CPU_RQ (CPU);          Get the RQ (ready queue) of the corresponding CPU struct task_struct *curr = rq->curr;

    Gets the currently running task Sched_clock_tick ();
    Raw_spin_lock (&rq->lock);                          Update_rq_clock (RQ);    Update Queue clock Curr->sched_class->task_tick (RQ, Curr, 0);                   Invokes the function of the scheduling class for the current task update_cpu_load_active (RQ);

    Update the Load Raw_spin_unlock (&rq->lock) of the processor;

Perf_event_task_tick ();
    #ifdef CONFIG_SMP rq->idle_balance = idle_cpu (CPU);                Trigger_load_balance (RQ, CPU);
Load balance #endif rq_last_tick_reset (RQ) when necessary; }

where Curr->sched_class->task_tick (RQ, Curr, 0); This line of code calls the Task_tick () function of the schedule class for the current task, which determines whether the current task needs to be set on a specific basis tif_need_ The resched flag and, if necessary, the final call to Set_tsk_need_resched () to set the flag. It should be noted that this is just a flag-setting without executing the schedule () function, which will be based on this flag to determine whether or not to perform the schedule () function at the end of various system calls and broken return codes.

When the task of sleep is awakened:

When a sleep task waits for an event to arrive, the kernel (for example, the driver's interrupt handler function) will invoke WAKE_UP () to wake the associated task and eventually invoke Try_to_wake_up (). It completes three things: Add the task back to the ready queue, set the run flag to task_running, and set the current task's tif_need_resched flag if the awakened task can preempt the currently running task.

After the tif_need_resched flag is set, there are only two ways to actually invoke the execution of the schedule () function, the first one being a system call or an interrupt return, depending on the tif_need_resched flag to determine whether to invoke schedule () function (in terms of efficiency, while the process is still in the kernel to deal with the matter completed); The second situation is that the current task because of the need for sleep, the process of sleep immediately after the call schedule () function, in the kernel of this situation is also more, such as disk, NIC and other device drivers.

References: "Linux technology Insider"
PS: When starting to learn the Linux kernel is easy to be a variety of structural concepts flooded the mind, mess. At this point, they need to be responsible for their respective functions and the coordination between each other clearly, recommend the book. After reading the "Linux kernel design and implementation" can be compared to each other, the effect is good.

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.