From cf066e022d0091727f565ad15b42cbf52305d766 Mon Sep 17 00:00:00 2001 From: gatieme Date: Sun, 8 May 2016 22:44:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E6=89=80=E6=9C=89=E8=BF=9B=E7=A8=8B=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- study/process/find/find_task.c | 17 +++++++++-------- study/process/list/list_process.c | 8 ++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/study/process/find/find_task.c b/study/process/find/find_task.c index 0a3a0db..437e8a1 100644 --- a/study/process/find/find_task.c +++ b/study/process/find/find_task.c @@ -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); } } } diff --git a/study/process/list/list_process.c b/study/process/list/list_process.c index 9f56703..3fade23 100644 --- a/study/process/list/list_process.c +++ b/study/process/list/list_process.c @@ -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, ¤t->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";