linuxkernelprintk

printing the comm field of the `current` task_struct, Linux kernel


I'm trying to print the comm field of the current task_struct to print the name of a process. Then, using current->parent, I want to do the same thing for all the current process's ancestors.

Here's what I've got so far:

while (there is still an ancestor of current to print) {
...
   printk("Name: %s", current_task->comm);
... 
}

And here's the result (pid shown to the left of the process names): enter image description here

As you can see, the first and last processes aren't printing correctly. I understand why the first one is being truncated -- current->comm is an array of 16 chars, and so there is simply no room for the final "r" in process_ancestor (the name of my program). However, I don't understand why swapper has the null terminator appended to it. Is there any chance that this is actually the name of the process? Or is something else going wrong?


Solution

  • What you see is exactly the process name, the swapper process runs each instance per CPU on a SMP system, and they are distinguished by appending the process number to it, so on the 1st CPU, the process is swapper/0, on the 2nd it would be swapper/1, and so on. You can find out in the kernel source:

    http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.5#L5136

    #if defined(CONFIG_SMP)
         sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
    #endif
    

    Here INIT_TASK_COMMON is defined as "swapper" in header file, and cpu is current cpu number.