(go) Process scheduling function schedule () interpretation

Source: Internet
Author: User
Tags prev

Original

In Linux, a single processor is also a multi-threaded processing signal, events and so on. This requires a core algorithm for process scheduling. The algorithm is CFS (Completely Fair Scheduler). Summarize the CFS process scheduling in a single sentence in the LInux Kernel Development Book:

Run the process represented by the leftmost leaf node in the Rbtree tree.

In a tree node with a self-balancing binary search tree red-black tree Rbtree, the next data that should run the process is stored. Here we see the perfect use of the two-fork search tree. See Introduction to algorithms Page 174~182 for details.

The main entry function for process scheduling is schedule (). It is defined in the file KERNEL/SCHED.C.

Let's look at an example of process scheduling in the wait queue:

Define_wait(Wait);Affirm waiting queueAdd_wait_queue(Q,&Wait);Add the Q queue we use to the wait queueWhile(!Condition){When the wait event does not come to a temporaryPrepare_to_wait(&Q, &wait,task_interruptible); //the Q from task_running or other status to task_interruptible non-functioning sleep state. //simultaneously accepts signals && events to wake it if (signal_pending< Span class= "P" > (current//if there is a signal from the processor {processingsignal (); //processing signal schedule (); //call the next process in the red and black tree } finish_wait ( &q,&wait); //set the process to task_running and move out of the wait queue.            

In fact, we can understand this piece of code. Now there is a task to wait for the event to come to run, how to implement it? is to block plus query. But this will allow the code to monopolize the entire operating system. In order to solve this problem, the queue and process dispatch schedule () are added to the blocking query, so that the execution of other threads is not delayed.

Let's take a look at the structure of the schedule () function:

Schedule () function structure
Asmlinkagevoid__schedSchedule(void)Defining pass-through stack values{structTask_struct*Prev,*Next;UnsignedLong*Switch_count;structRq*Rq;IntCpu;/*at the end of this function, it would check if need_resched () returnTrue if that indeed happen and then Goto here.*/Need_resched:/*current process won ' t be preempted after call preemept_disable () */Preempt_disable();Do not let priority occupy the current processCpu=smp_processor_id();Rq=Cpu_rq(Cpu);/* Rcu_sched_qs? */Rcu_sched_qs(Cpu);/* Prev Point-to-current task_struct */Prev=Rq-Curr;/* Get current task_struct ' s context switch count */Switch_count=&Prev-Nivcsw;/* Kernel_flag is "The Big kernel lock".* This spinlock are taken and released recursively by Lock_kernel ()* and Unlock_kernel (). It is transparently dropped and reacquired* Over Schedule (). It's used to protect legacy code that hasn ' t* Been migrated to a proper locking design yet.* In Task_struct, there are a member lock_depth, which is inited-1,* Indicates the current task has no kernel lock.* When lock_depth >=0 indicate that it own kernel lock.* During context Switching, it isn't permitted that task* Switched away remain own kernel lock, so in Scedule (), it* Call Release_kernel_lock (), release kernel lock.*/Release_kernel_lock(Prev);Need_resched_nonpreemptible:Schedule_debug(Prev);If(Sched_feat(Hrtick))Hrtick_clear(Rq);/* Occupy current RQ ' s lock */Raw_spin_lock_irq(&Rq-Lock);Possession of RQ spin lock/* Update RQ ' s clock,this function would call SCHED_CLOCK_CPU () */Update_rq_clock(Rq);/* Clear bit in task_struct ' s thread_struct ' flag tif_need_resched.* In case that it'll be rescheduled, because it prepare to give* Up CPU.*/Clear_tsk_need_resched(Prev);If(Prev-State&&!(Preempt_count()&Preempt_active)){If(Unlikely(Signal_pending_state(Prev-State,Prev)))Prev-State=Task_running;ElseDeactivate_task(Rq,Prev,1);Switch_count=&Prev-Nvcsw;}/* For NONE-SMP, Pre_schedule is NULL */Pre_schedule(Rq,Prev);If(Unlikely(!Rq-Nr_running))Idle_balance(Cpu,Rq);Put_prev_task(Rq,Prev);Next=Pick_next_task(Rq);If(Likely(Prev!=Next)){Sched_info_switch(Prev,Next);Perf_event_task_sched_out(Prev,Next);Rq-Nr_switches++;Rq-Curr=Next;++*Switch_count;Context_switch(Rq,Prev,Next);/* Unlocks the RQ * */** The context switch might has flipped the stack from under* us, hence refresh the local variables.*/Cpu=smp_processor_id();Rq=Cpu_rq(Cpu);}ElseRaw_spin_unlock_irq(&Rq-Lock);Current task still occupy CPUPost_schedule(Rq);If(Unlikely(Reacquire_kernel_lock(Current)<0{prev = rq-> Currswitch_count = &prev->< span class= "n" >nivcswgoto need_resched_nonpreemptible} preempt_enable_no_resched (); if  (need_resched ()) goto need_resched} export_symbol (schedule   

The purpose of the schedule () function is to replace the currently running process with another process. Therefore, the main result of this function is to set a variable named next so that it points to the descriptor of the selected process in place of the current. If there are no running processes in the system with a priority greater than the current priority, the result is that next is consistent with current and no process switchover occurs.

References

[1]. Understanding the LINUX KERNEL. Page 276

[2]. Linux Kernel Development. Page 52

[3].http://hi.baidu.com/zengzhaonong/item/20d9e8207b04cb8f6e2cc323

(go) Process scheduling function schedule () interpretation

Related Article

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.