Linux schedule introduction

来源:互联网 发布:网络基站建设方案 编辑:程序博客网 时间:2024/04/30 04:14
In Linux kernel, the scheduler is invoked by periodic timer interrupt. This iscalled periodic scheduling which is essential for preempting tasks that have consumed more CPU cycles in order tooffer other tasks on run-queue a fair chance to utilize the CPU. 

The scheduler is alsoinvoked by kernel functions that block the current task, and this allows the scheduler to decide on which task on the run-queue should run and context switch to that task. 

Exit-paths of most critical sections within the kernel may invoke the scheduler especially if the critical section had scheduling disabled for more than a jiffy. This is just a good practice within kernel-space development in order to have a more responsive system.

For example, preempt_enable() function may invoke schedule() if thepreempt count becomes zero and need_resched() returns true.


Schedule() function at its simplest is a function which tells the process scheduler to run the next task in its runqueue. This runqueue is maintained per CPU. 
Scheduler (Per CPU) has its own way to determine which is next task shall be made based on the priority associated with the task.

As to how it is called is: Any kernel code can by itself declare to give up its turn of running on CPU by calling schedule(). That's like i saying i got an change to run on CPU and i found based on some condition i do not have any meaning full work, so its better i go to sleep and wait for my next turn. 

The calling is as simple as writing schedule(); after you condition of no work in your code. 

So, schedule() the way in kernel when you want a process to go voluntarily to go sleep.

Ever thought what is the opposite to schedule () .. When we want our process to wake up voluntarily ? Its wake_up(). 

0 0