Skip to content
Advertisement

How to Obtain Youngest Child’s PID from task_struct

I’m working on a project that involves writing a new system call for Linux 3.18.20. This system call is supposed to store various information about the currently running process in a newly defined struct.

One of the fields of the struct is the PID of the process’s youngest child, and I’ve been searching for information about this in the struct task_struct, as defined here: http://lxr.free-electrons.com/source/include/linux/sched.h. I’ve been testing my new system call, and I can’t seem to find the appropriate PID value from the struct list_head children.

My test function involves forking a child, storing the return value of fork, and comparing it to the value I get from doing various things in the parent’s task_struct. I got the parent’s task_struct, and I tried all of the following macros for struct list_head to get the correct PID. None of them have been correct.

    printk("list_next_entry pid = %dn", list_next_entry(task, children)->pid);
printk("list_prev_entry pid = %dn", list_prev_entry(task, children)->pid);
printk("list_entry pid = %dn", list_entry(&task->children, struct task_struct, children)->pid);
printk("list_first_entry pid = %dn", list_first_entry(&task->children, struct task_struct, children)->pid);
printk("list_last_entry pid = %dn", list_last_entry(&task->children, struct task_struct, children)->pid);

Is this even close to the correct way to try to find the youngest child of the parent process? How can I find a process’s youngest child’s PID from that process’s task_struct?

Advertisement

Answer

As comments to fields in task_struct note, children is

list of my children

and siblings is

linkage in my parent’s children list

So pointer to the first children task can be requested using

list_first_entry(&task->children, struct task_struct, siblings)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement