55

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?

Suresh Subedi
  • 660
  • 2
  • 10
  • 25
jscoot
  • 2,019
  • 3
  • 24
  • 27
  • 3
    I haven't seen it called "swapper task" anywhere so far. there's kswapd taking care of swapping memory from/to the disk, but I'd say that's a pretty different matter (and that one do show up in the PID list) – PypeBros Apr 26 '12 at 13:23
  • 1
    @PypeBros Linux's own perf-tools are calling it the swapper task (in `perf top`) – pistache Nov 12 '19 at 05:16

4 Answers4

47

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.

bl4ckb0l7
  • 3,839
  • 4
  • 28
  • 33
  • 3
    HLT instruction pre-dates the linux scheduler, doesn't it? – PypeBros Sep 05 '11 at 13:56
  • @sylvainulg I think it does, but I wouldn't be surprised if the linux scheduling algorithms (or at least some of them) predate the HLT instruction. – Samuel Edwin Ward Apr 03 '12 at 19:55
  • @SamuelEdwinWard, that could be. My documentation mentions HLT was already available on 8088 and 8086. 10 years after the earlies Unix variant, then ... but is the algorithm really that old ? – PypeBros Apr 26 '12 at 13:20
  • 1
    @sylvainulg, it's an interesting question, and I'm not sure where to find the answer. Where did Linux get their scheduling algorithms? Did the PDP-11 have a halt instruction? Did the original UNIX group get their scheduling algorithms from someone even earlier? – Samuel Edwin Ward Apr 26 '12 at 14:58
  • 1
    @SamuelEdwinWard: apparently, it did: http://www.divms.uiowa.edu/~jones/pdp8/man/micro.html#hlt -- on the PDP-11, it looks like WAIT is the instruction one would have used, and HALT is more like "CLI; HLT" in 80x86 lingo. – PypeBros Apr 30 '12 at 12:42
  • Do all architectures Linux supports have a HLT instruction (or equivalent) though? – Thayne Sep 10 '20 at 15:53
  • 1
    Per Wikipedia, "All x86 processors from the 8086 onwards had the HLT instruction, but it ... was not specifically designed to reduce power consumption until the release of the Intel DX4 processor in 1994". – pericynthion Feb 19 '21 at 09:46
18

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.

Spence
  • 28,526
  • 15
  • 68
  • 103
10

There is actually one idle task per cpu, but it's not held in the main task list, instead it's in the cpu's "struct rq" runqueue struct, as a struct task_struct * .

This gets activated by the scheduler whenever there is nothing better to do (on that CPU) and executes some architecture-specific code to idle the cpu in a low power state.

MarkR
  • 62,604
  • 14
  • 116
  • 151
2

You can use ps -ef and it will list the no of process which are running. Then in the first link, it will list the first pid - 0 which is the swapper task.

user1082080
  • 41
  • 1
  • 3