From 6c34b0b3593881140c786c32816aa3cf9edfca95 Mon Sep 17 00:00:00 2001 From: gatieme Date: Mon, 9 May 2016 06:58:56 +0800 Subject: [PATCH] modify list_process --- study/process/find/find_task.c | 50 +++++++------- study/process/find/find_task.c.1 | 104 ++++++++++++++++++++++++++++++ study/process/list/list_process.c | 28 ++++++-- 3 files changed, 151 insertions(+), 31 deletions(-) create mode 100644 study/process/find/find_task.c.1 diff --git a/study/process/find/find_task.c b/study/process/find/find_task.c index 6de53bc..20de012 100644 --- a/study/process/find/find_task.c +++ b/study/process/find/find_task.c @@ -14,24 +14,7 @@ #include -/* -* find_task_by_pid maybe not supported -* O(n) is fine :) -*/ -struct task_struct * findTaskByPid(pid_t pid) -{ - struct task_struct *task = NULL; - return NULL; -} - -/* - * show the task info - */ -void getTaskinfo(struct task_struct *task) -{ - -} //#define METHOD 2 static unsigned int METHOD = 1; @@ -39,7 +22,21 @@ module_param(METHOD, uint,0400); static unsigned int PID = 1; module_param(PID, uint,0400); -static int find_task_init(void) + + + + + +void getTaskinfo(struct task_struct *task) +{ +} + +/* + * + * + */ + +void findTaskByPid(int pid) { struct task_struct *task = NULL, *pTask = NULL; struct list_head *pos = NULL; @@ -57,7 +54,7 @@ static int find_task_init(void) list_for_each(pos, &task->tasks) { pTask = list_entry(pos, struct task_struct, tasks ); - if(pTask->pid == (pid_t)PID) + if(pTask->pid == (pid_t)pid) { printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask); break; @@ -69,7 +66,7 @@ static int find_task_init(void) method = "for_each_process"; for_each_process(task) { - if(task->pid == PID) + if(task->pid == pid) { printk(KERN_ALERT "%d\t%s\t%p\n", task->pid, task->comm, task); break; @@ -80,7 +77,7 @@ static int find_task_init(void) method = "list_for_each_entry"; list_for_each_entry(pTask, &task->tasks, tasks) { - if(pTask->pid == PID) + if(pTask->pid == (pid_t)pid) { printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask); } @@ -90,15 +87,20 @@ static int find_task_init(void) return 0; } -static void find_task_exit(void) +static int init_find_task(void) +{ + +} + +static void exit_find_task(void) { printk(KERN_ALERT "GOOD BYE:find_task!!\n"); } -module_init(find_task_init); -module_exit(find_task_exit); +module_init(init_find_taskt); +module_exit(exit_find_task); MODULE_AUTHOR("gatieme"); MODULE_LICENSE("GPL"); diff --git a/study/process/find/find_task.c.1 b/study/process/find/find_task.c.1 new file mode 100644 index 0000000..6de53bc --- /dev/null +++ b/study/process/find/find_task.c.1 @@ -0,0 +1,104 @@ +/************************************************************************* + > File Name: process.c + > Author: GatieMe + > Mail: gatieme@163.com + > Created Time: 2016年04月01日 星期五 21时09分29秒 + ************************************************************************/ + + + +#include +#include +#include + +#include + + +/* +* find_task_by_pid maybe not supported +* O(n) is fine :) +*/ +struct task_struct * findTaskByPid(pid_t pid) +{ + struct task_struct *task = NULL; + + return NULL; +} + +/* + * show the task info + */ +void getTaskinfo(struct task_struct *task) +{ + +} + +//#define METHOD 2 +static unsigned int METHOD = 1; +module_param(METHOD, uint,0400); + +static unsigned int PID = 1; +module_param(PID, uint,0400); +static int find_task_init(void) +{ + struct task_struct *task = NULL, *pTask = NULL; + struct list_head *pos = NULL; + char *method = NULL; + + task = &init_task; + + printk(KERN_ALERT"PID\tCOMM\tADDR\n"); + + + switch(METHOD) + { + case 1 : + method = "list_for_each"; + list_for_each(pos, &task->tasks) + { + pTask = list_entry(pos, struct task_struct, tasks ); + if(pTask->pid == (pid_t)PID) + { + printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask); + break; + } + } + + break; + case 2 : + method = "for_each_process"; + for_each_process(task) + { + if(task->pid == PID) + { + printk(KERN_ALERT "%d\t%s\t%p\n", task->pid, task->comm, task); + break; + } + } + break; + case 3 : + method = "list_for_each_entry"; + list_for_each_entry(pTask, &task->tasks, tasks) + { + if(pTask->pid == PID) + { + printk(KERN_ALERT "%d\t%s\t%p\n", pTask->pid, pTask->comm, pTask); + } + } + } + printk("the method is %s", method); + return 0; +} + +static void find_task_exit(void) +{ + printk(KERN_ALERT "GOOD BYE:find_task!!\n"); +} + + + +module_init(find_task_init); +module_exit(find_task_exit); + +MODULE_AUTHOR("gatieme"); +MODULE_LICENSE("GPL"); diff --git a/study/process/list/list_process.c b/study/process/list/list_process.c index 7842d28..90faf12 100644 --- a/study/process/list/list_process.c +++ b/study/process/list/list_process.c @@ -12,15 +12,20 @@ #include #include -//#include -//#define METHOD 2 +/* + * you can use METHOD to select the function you want to list the process + * METHOD = 1 "list_for_each" + * METHOD = 2 "for_each_process" + * METHOD = 3 "list_for_each_entry" + */ static unsigned int METHOD = 1; -module_param(METHOD, uint,0400); +module_param(METHOD, uint, 0400); -static int list_process_init(void) + +void do_list_process(void) { struct task_struct *task = NULL, *pTask = NULL; struct list_head *pos = 0; @@ -80,15 +85,24 @@ static int list_process_init(void) return 0; } -static void list_process_exit(void) + + +static int init_list_process(void) +{ + // list all the process of you system + do_list_process( ); +} + +static void exit_list_process(void) { printk(KERN_ALERT "GOOD BYE--list process!!\n"); } -module_init(list_process_init); -module_exit(list_process_exit); +module_init(init_list_process); +module_exit(exit_list_process); + MODULE_AUTHOR("gatieme"); MODULE_LICENSE("GPL");