I'm playing with the Linux kernel, and one thing that I don't understand is the pid of the init_task
task.
As far as I know, there are two special pids: pid 0 for the idle/swapper task, and pid 1 for the init task.
Every online resource (e.g. one, two) I could find say that the init_task
task represents the swapper task, i.e. it should have pid 0.
But when I print all the pids using the for_each_process
macro, which starts from init_task
, I get pid 1 as the first process. I don't get pid 0 at all. Which means that init_task
has pid 1, and that it's the init task (?!).
Please help me resolve this confusion.
P.S. the kernel version is 2.4.
The reason for my confusion was the tricky definition of the for_each_task
macro:
#define for_each_task(p) \
for (p = &init_task ; (p = p->next_task) != &init_task ; )
Even though it seems that p
starts from init_task
, it actually starts from init_task.next_task
because of the assignment in the condition.
So for_each_task(p) { /* ... */ }
could be rewritten as:
p = init_task.next_task;
while(p != &init_task)
{
/* ... */
p = p->next_task;
}
As it can be seen, the swapper process is not part of the iteration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With