使用一些功能来做到这一点如下。
walk_tasks_backwards_from(current);
功能遵循...
static void walk_tasks_backwards_from(struct task_struct *pos);
static void process_tasks(struct task_struct *parent, struct task_struct *child);
static void process_tasks(struct task_struct *parent, struct task_struct *child) {
pr_alert("parent process: %s, PID: %d\n", parent->comm, parent->pid);
pr_alert("child process: %s, PID: %d\n", child->comm , child->pid);
}
static void walk_tasks_backwards_from(struct task_struct *pos) {
struct task_struct *parent; ;
do {
parent = pos->parent;
process_tasks(parent, pos);
pos = parent;
} while(parent->pid != 0);
}
您可以使用您想从即
walk_tasks_backwards_from(current);
走电流或任何任务时,这会产生我的机器,其中以下输出调用函数我有一个玩具内核模块,可以使用cat /dev/toy
[161769.300609] parent process: bash, PID: 918
[161769.301299] child process: cat, PID: 2803
[161769.301973] parent process: login, PID: 626
[161769.302632] child process: bash, PID: 918
[161769.303216] parent process: systemd, PID: 1
[161769.303797] child process: login, PID: 626
[161769.304355] parent process: swapper/0, PID: 0
[161769.304978] child process: systemd, PID: 1
另外,您可以使用以下任何...
使用do {} while
task = current;// Get current process
printk(KERN_INFO "process: %s, PID: %d", task->comm, task->pid);
do {
task = task->parent;
printk(KERN_INFO "process: %s, PID: %d", task->comm, task->pid);
} while (task->pid != 0);
//task here has pid == 0;
或使用while循环。
task = current;// Get current process
while (task->pid != 0) {
printk(KERN_INFO "process: %s, PID: %d", task->comm, task->pid);
task = task->parent;
}
//task here has pid == 0;
printk(KERN_INFO "process: %s, PID: %d", task->comm, task->pid);
遍历一切
struct task_struct task;
for_each_process(task) {
printk(KERN_INFO "process: %s, PID: %d", task->comm, task->pid);
}