From 768bd2a1c41bda1ee31364b3d83f1d064c5b42b4 Mon Sep 17 00:00:00 2001 From: gatieme Date: Sat, 2 Apr 2016 19:54:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=8D=E5=8E=86=E7=B3=BB=E7=BB=9F=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=BF=9B=E7=A8=8Blist=5Fprocess...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- study/process/Makefile | 14 +++++ study/process/list_process.c | 107 +++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 study/process/Makefile create mode 100644 study/process/list_process.c diff --git a/study/process/Makefile b/study/process/Makefile new file mode 100644 index 0000000..f1b0236 --- /dev/null +++ b/study/process/Makefile @@ -0,0 +1,14 @@ + + +obj-m := list_process.o + +KERNELDIR ?= /lib/modules/$(shell uname -r)/build + +PWD := $(shell pwd) + +all: + make -C $(KERNELDIR) M=$(PWD) modules + +clean: + make -C $(KERNELDIR) M=$(PWD) clean + diff --git a/study/process/list_process.c b/study/process/list_process.c new file mode 100644 index 0000000..d6bfeb7 --- /dev/null +++ b/study/process/list_process.c @@ -0,0 +1,107 @@ +/************************************************************************* + > File Name: process.c + > Author: GatieMe + > Mail: gatieme@163.com + > Created Time: 2016年04月01日 星期五 21时09分29秒 + ************************************************************************/ + + + +#include + +#include +#include + +#include + + + +//#define METHOD 2 +static unsigned int METHOD = 1; +moudle_param(METHOD, uint,0400); + +static int list_process_init(void) +{ + struct task_struct *task, *p; + struct list_head *pos; + int count; + char *method; + count = 0; /*下面这些初始化完全是为了消除编译时的警告信息*/ + p = 0; + task = 0; + pos = 0; + method = 0; + task = &init_task; + + printk(KERN_ALERT"PID\tCOMM\n"); + + switch(METHOD) + { + + case 1: + + method = "list_for_each"; + + break; + + case 2: + + method = "for_each_process"; + + break; + + case 3: + + method = "list_for_each_entry"; + + break; + + } + + printk( "The method is %s\n", method ); + +#if METHOD == 1 + + list_for_each( pos, &task->tasks ) + { + p = list_entry( pos, struct task_struct, tasks ); + count++; + printk( KERN_ALERT "%d\t%s\n", p->pid, p->comm ); + + } + +#elif METHOD == 2 + + for_each_process(task) + { + count++; + printk( KERN_ALERT "%d\t%s\n", task->pid, task->comm ); + } + +#elif METHOD == 3 + + list_for_each_entry( p, &task->tasks, tasks ) + { + count++; + printk( KERN_ALERT "%d\t%s\n", p->pid, p->comm ); + } + +#endif + + printk("there are %d process in your system now...", count); + + return 0; +} + +static void list_process_exit(void) +{ + printk( KERN_ALERT "GOOD BYE!!\n"); +} + + + +module_init(list_process_init); +module_exit(list_process_exit); + +MODULE_AUTHOR("gatieme"); +MODULE_LICENSE("GPL");