【发布时间】:2016-05-05 23:39:16
【问题描述】:
我想用 pid、ppid、comm 和 size 列出所有正在运行的进程。除了 ppid 和 size 之外,我有以下代码工作,所以如何做到这一点。
--代码--
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include "sched.h"
#include "sched1.h"
/* This function is called when the module t is loaded. */
int process_init(void)
{
printk(KERN_INFO "lOADING MODULE \n");
printk(KERN_INFO "PID \t PPID \t PNAME \t SIZE \n");
struct task_struct *task;
for_each_process(task)
{
printk(KERN_INFO "%d \t %d\t %s \t %d \n", task->pid,task->ppid,task->comm,task->sz);
}
return 0;
}
【问题讨论】:
-
execlp("ps","ps","-ef",(char *)NULL);是否适合您的目的 -
而不是哪一行?
-
@ishyfishy 他对
printk的使用意味着这是在内核中运行,而不是在用户进程中。他不能从内核调用execlp。 -
你能给我们你的内核版本吗?我在struct task_struct中找不到ppid和sz成员@
-
这里也一样,所以问了如何获取父id和进程大小。
标签: c process linux-kernel