So I know how to create a kernel and to iterate over the processes linearly by Simply including linux/sched.h
and using the following code:
struct task_struct *task;
for_each_process(task)
{
printk("Name: %s PID: [%d]\n", task->comm, task->pid);
}
How can I print these tasks using a depth first search? I want my output to be similar to the one of ps -eLf
.
The following patch of code has been given for reference:
struct task_struct *task;
struct list_head *list;
list_for_each(list, &init_task->children) {
task = list_entry(list, struct task_struct, sibling);
/* task points to the next child in the list */
}
and I know that task->comm
returns the name and task->pid
returns the PID for that task.
What commands are used to return the state and parent pid?
you can get the state with
task->state /* -1 unrunnable, 0 runnable, >0 stopped */
get the parent pid with
task->parent->pid
This is a bit old, but I came across it as it seems to be one of the programming projects found in chapter 3 of Operating System Concepts 9th Edition, so others may yet come looking.
The code you started with is straight from the book, but it is a good starting point. You just need to implement the DFS. Here is the code that will accomplish it, it should be pretty self explanatory:
For uid, tgid you can refer cred structure in task_struct. For priority you can refer rt_priority(for real time priority) and prio field. For other fields you may refer proc directory (Because ps will take input from proc).