Why Do We Need a Swapper Task in Linux

Swapper task on multiple CPU cores

The idle tasks job is, as you say, to run when there is nothing to else to run, so the CPU doesn't run out of instructions.

So that means that on a system with a single core the idle process makes sure that the CPU always has something to do, so it doesn't stop.

On a multi CPU/core system the same thing is true, however some CPU's allow for the system to put some of the cores to into idle mode to save power. In this case you only need to keep a single core alive, with the idle process, because then when the kernel is switched into that core, it can wake up more cores on demand.

Please note that the above is a simplified version of the whole truth. Just trust the kernel to do the right thing, it usually knows what it's doing, and only want what's best for you :-)

What does the linux scheduler return when there are no tasks left in the queue

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();

In Linux, when a process is about to be swapped or terminated, what state should its threads be in?

Yeah - you need to read up on paged virtual memory, as suggested by @CL. Processes are not swapped out in their entirety and swapping!=termination.

If the OS needs to terminate a process, either because of a specific API request or because of its OOM algorithm, the OS stops all its threads first. Blocked threads are easy to 'stop' because they are not running anyway - it's only necessary to change their state to ensure that they are never run again. Thread/s that are actually running on cores have to be stopped by means of an inter-core comms driver that can hardware-interrupt the cores running the threads. Once all threads are not running, the resources, including all user-space memory, allocated to the process can be freed and OS thread/process management structs released. The process then no longer exists.



Related Topics



Leave a reply



Submit