Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a swapper task in linux?

The idle task (a.k.a. swapper task) is chosen to run when no more runnable tasks in the run queue at the point of task scheduling. But what is the usage for this so special task? Another question is why i can't find this thread/process in the "ps aux" output (PID=0) from the userland?

like image 407
jscoot Avatar asked Jan 21 '09 08:01

jscoot


People also ask

What is the use of Swapper process in Linux?

The swapper is a kernel daemon. Swapper moves whole processes between main memory and secondary storage (swapping out and swapping in) as part of the operating system's virtual memory system. SA RELEVANCE: The swapper is the first process to start after the kernel is loaded.

What is the process ID of Swapper process in Linux?

There are two tasks with specially distinguished process IDs: swapper or sched has process ID 0 and is responsible for paging, and is actually part of the kernel rather than a normal user-mode process. Process ID 1 is usually the init process primarily responsible for starting and shutting down the system.


2 Answers

The reason is historical and programatic. The idle task is the task running, if no other task is runnable, like you said it. It has the lowest possible priority, so that's why it's running of no other task is runnable.

Programatic reason: This simplifies process scheduling a lot, because you don't have to care about the special case: "What happens if no task is runnable?", because there always is at least one task runnable, the idle task. Also you can count the amount of cpu time used per task. Without the idle task, which task gets the cpu-time accounted no one needs?

Historical reason: Before we had cpus which are able to step-down or go into power saving modes, it HAD to run on full speed at any time. It ran a series of NOP-instructions, if no tasks were runnable. Today the scheduling of the idle task usually steps down the cpu by using HLT-instructions (halt), so power is saved. So there is a functionality somehow in the idle task in our days.

In Windows you can see the idle task in the process list, it's the idle process.

like image 80
bl4ckb0l7 Avatar answered Sep 30 '22 05:09

bl4ckb0l7


The linux kernel maintains a waitlist of processes which are "blocked" on IO/mutexes etc. If there is no runnable process, the idle process is placed onto the run queue until it is preempted by a task coming out of the wait queue.

The reason it has a task is so that you can measure (approximately) how much time the kernel is wasting due to blocks on IO / locks etc. Additionally it makes the code that much easier for the kernel as the idle task is the same as every task it needs to context switch, instead of a "special case" idle task which could make changing kernel behaviour more difficult.

like image 25
Spence Avatar answered Sep 30 '22 06:09

Spence