Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about init_task (pid 0 or pid 1?)

Tags:

linux

init

kernel

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.

like image 830
Paul Avatar asked Mar 15 '23 05:03

Paul


1 Answers

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.

like image 119
Paul Avatar answered Mar 26 '23 15:03

Paul