当前位置: 技术问答>linux和unix
内核模块编程输出进程树
来源: 互联网 发布时间:2016-10-16
本文导语: //帮忙看看程序哪里有问题,加载模块后不停止的输出 #include //task_struct #include //unix std #include //list_entry ,list_head #include //needed by macros #include //needed by all modules #include //needed by all kernel programming MODULE_LICENSE...
//帮忙看看程序哪里有问题,加载模块后不停止的输出
#include //task_struct
#include //unix std
#include //list_entry ,list_head
#include //needed by macros
#include //needed by all modules
#include //needed by all kernel programming
MODULE_LICENSE("GPL");
static int num=-1; //decide means of print tree
module_param(num,int,S_IRUGO); //input num via "insmod"
struct proc_info
{
struct list_head *task; //point to struct task_struct->tasks
char flag; //visited or not
int rec; //number of fathers
};
void pstree(struct proc_info *proc, int total, int ppid, int rec) //print ptree
{
int i,k;
for (i=0; iparent->pid==ppid)
{
proc[i].rec=rec+1;
proc[i].flag=1;
for(k=0; kpid>0)
{
printk(KERN_ALERT "├──%s(%d)n",list_entry(proc[i].task,struct task_struct,tasks)->comm,list_entry(proc[i].task,struct task_struct,tasks)->pid);
}
pstree(proc,total,list_entry(proc[i].task,struct task_struct,tasks)->pid,proc[i].rec);
}
}
}
static int printree_init(void) //initialize the module
{
struct task_struct* p;
struct proc_info proc[512];
int total=0;
int rec=0;
proc[total++].task=&p->tasks;
for ( p=&init_task; (p=list_entry((p)->tasks.next,struct task_struct,tasks))!=&init_task; total++)
{
proc[total].task=&p->tasks;
}
memset(&proc->flag,0,total);//将proc.flag的所有字节初始化为0
memset(&proc->rec,0,total);
if (numtasks; 这时候p还没初始化吧?
memset(&proc->flag,0,total); proc是个数组,proc->flag是什么意思?能编译过?