完善了遍历所有进程的代码...

This commit is contained in:
gatieme
2016-05-08 22:44:25 +08:00
parent 3c785f939e
commit cf066e022d
2 changed files with 17 additions and 8 deletions
+9 -8
View File
@@ -40,7 +40,7 @@ static unsigned int PID = 1;
module_param(PID, uint,0400);
static int find_task_init(void)
{
struct task_struct *task = NULL;
struct task_struct *task = NULL, *pTask = NULL;
struct list_head *pos = NULL;
char *method = NULL;
@@ -54,15 +54,16 @@ static int find_task_init(void)
{
case 1 :
method = "list_for_each";
list_for_each(pos, &task->tasks )
list_for_each(pos, &task->tasks)
{
task = list_entry(pos, struct task_struct, tasks );
if(task->pid == (pid_t)PID)
pTask = list_entry(pos, struct task_struct, tasks );
if(pTask->pid == (pid_t)PID)
{
printk(KERN_ALERT "%d\t%s\t%p\n", task->pid, task->comm, task);
printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask);
break;
}
}
break;
case 2 :
method = "for_each_process";
@@ -77,11 +78,11 @@ static int find_task_init(void)
break;
case 3 :
method = "list_for_each_entry";
list_for_each_entry(task, &task->tasks, tasks )
list_for_each_entry(pTask, &task->tasks, tasks)
{
if(task->pid == PID)
if(pTask->pid == PID)
{
printk(KERN_ALERT "%d\t%s\t%p\n", task->pid, task->comm, task);
printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask);
}
}
}
+8
View File
@@ -42,6 +42,14 @@ static int list_process_init(void)
count++;
printk(KERN_ALERT "%d\t%s\n", pTask->pid, pTask->comm);
}
// because the list of process is an double-linked circular list
// we can also list all the process from the current process
list_for_each(pos, &current->children)
{
pTask = list_entry(pos, struct task_struct, sibling);
printk(KERN_ALERT "%d\t%s\n", pTask->pid, pTask->comm);
}
break;
case 2 :
method = "for_each_process";