2

The linux scheduler calls for an scheduler algorithm that finds the next task in the task list.

What does the scheduling algorithm return if there are no tasks left?

Below is a piece of code

struct rt_info* sched_something(struct list_head *head, int flags) { /* some logic */ return some_task; // What task's value if there are no tasks left. }

1 Answers1

1

There is special "idle" task with PID=0, sometimes it is called swapper (Why do we need a swapper task in linux?). This task is scheduled to CPU core when there is no other task ready to run. There are several idle tasks - one for each CPU core

Source kernel/sched/core.c http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.16#L3160

3160 /**
3161  * idle_task - return the idle task for a given cpu.
3162  * @cpu: the processor in question.
3163  *
3164  * Return: The idle task for the cpu @cpu.
3165  */
3166 struct task_struct *idle_task(int cpu)
3167 {
3168         return cpu_rq(cpu)->idle;
3169 }
3170 

So, pointer to this task is stored in runqueue (struct rq) kernel/sched/sched.h:

502  * This is the main, per-CPU runqueue data structure. ...  */
508 struct rq {
557         struct task_struct *curr, *idle, *stop;

There is some init code in sched/core.c:

4517 /**
4518  * init_idle - set up an idle thread for a given CPU
4519  * @idle: task in question
4520  * @cpu: cpu the idle task belongs to
4524  */
4525 void init_idle(struct task_struct *idle, int cpu)

I think, idle task will run some kind of loop with special asm commands to inform CPU core that there is no useful job...

This post http://duartes.org/gustavo/blog/post/what-does-an-idle-cpu-do/ says idle task executes cpu_idle_loop kernel/sched/idle.c (there may be custom version of loop for arch and CPU - called with cpu_idle_poll(void) -> cpu_relax();):

 40 #define cpu_relax()     asm volatile("rep; nop")
 45 static inline int cpu_idle_poll(void)
 ..
 50         while (!tif_need_resched())
 51                 cpu_relax();

221                         if (cpu_idle_force_poll || tick_check_broadcast_expired())
222                                 cpu_idle_poll();
223                         else
224                                 cpuidle_idle_call();
Community
  • 1
  • 1
osgx
  • 90,338
  • 53
  • 357
  • 513
  • Earlier idle: http://www.tldp.org/HOWTO/Linux-i386-Boot-Code-HOWTO/init_main.html "`rest_init() { /* init process, pid = 1 */ kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); unlock_kernel(); current->need_resched = 1; /* idle process, pid = 0 */ cpu_idle(); /* never return */ }`" "`/* used in the idle loop; sti takes one instruction cycle to complete */ #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory") CPU will resume code execution with the instruction following "hlt" on the return from an interrupt handler.`" – osgx Mar 21 '15 at 08:13
  • also http://www.criticalblue.com/news/wp-content/uploads/2013/12/linux_scheduler_notes_final.pdf "The list is used to prioritise tasks of different types before others. ... the complete list looks like the following: stop_sched_class → rt_sched_class → fair_sched_class → idle_sched_class → NULL Stop and Idle are special scheduling classes. Stop is used to schedule the per-cpu stop task which pre-empts everything and can be pre-empted by nothing, and Idle is used to schedule the per-cpu idle task (also called swapper task) which is run if no other task is runnable." – osgx Mar 21 '15 at 08:19