当前位置:文档之家› Linux课程设计报告_编写proc文件系统相关的内核模块

Linux课程设计报告_编写proc文件系统相关的内核模块

操作系统课程设计报告

学院:电子信息工程学院

专业班级:11级信工二班

姓名:杨佳炜

学号:20111309062

【实验目的】

1.通过课程设计对操作系统基本原理进行更深入的认识,以Linux为具体研究对象,分析理解操作系统底层实现,综合利用已学知识与技术,就Linux操作系统各功能方面进行模拟或实现

2.了解内核模块结构

3.熟练内核模块编程,练习使用makefile

【实验要求】

编写proc文件系统相关的内核模块:

设计一个模块,该模块功能是列出系统中所有内核线程的程序名、PID号和进程状态。再设计一个带参数的模块,参数为进程的PID号,功能是列出进程的家族信息,包括父进程、兄弟进程和子进程的程序名、PID号。【实验步骤】

1.编写list.c模块

2.编写Makefile

3.make编译模块

4.安装模块sudo insmod list.ko

5.查看系统信息dmesg

6.退出模块sudo rmmod list

7.编写list2.c模块

8.编写Makefile

9.make编译模块

10.安装模块sudo insmod list2.ko

11.查看系统信息dmesg

12.退出模块sudo rmmod list2

【代码实现】

1.list.c

#include

#include

#include

#include

#include

#define METHOD 1

static int list_init(void)

{

struct task_struct *p;

int count;

char *method;

count = 0; /*下面这些初始化完全是为了消除编译时的警告信息*/

p = NULL;

method = NULL;

method="for_each_process";

printk( "The method is %s\n", method );

printk(KERN_ALERT"进程号\t父进程\t状态\t名称\n");

#if METHOD == 1

for_each_process(p) {

count++;

printk( KERN_ALERT "%d\t%d\t%ld\t%s\n", p->pid,p->parent->pid,p->state, p->comm );

}

#endif

printk("系统当前共%d 个进程!!", count);

return 0;

}

static void list_exit(void)

{

printk( KERN_ALERT "GOOD BYE!!\n");

}

module_init( list_init );

module_exit( list_exit );

MODULE_AUTHOR( "Along" );

MODULE_LICENSE( "GPL" );

makefile

ifneq ($(KERNELRELEASE),)

obj-m := list.o

else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd)

default:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules elean:

rm -rf *.o *.mod.c *.ko *.syvmers

endif

2.list2.c

#include

#include

#include

#include

#include

#define METHOD 1

static int pidd = 1;

module_param(pidd,int,S_IRUGO|S_IWUSR);

EXPORT_SYMBOL(pidd);

static int __init list_init(void)

{

struct task_struct *task, *p,*ppid;

struct list_head *pos;

int count;

char *method;

count = 0; /*下面这些初始化完全是为了消除编译时的警告信息*/ p = NULL;

task = NULL;

pos = NULL;

method = NULL;

task = &init_task;

method="for_each_process";

printk( "The method is %s\n", method );

printk(KERN_ALERT"进程号\t状态\t名称\t父进程\t父进程名\n");

#if METHOD == 1

for_each_process(p) {

count++;

if (pidd == p->pid)

{

printk( KERN_ALERT "%d\t%ld\t%s\t%d\t%s\n",p->pid,p->state,p->comm,p->parent->pid,p->par ent->comm);

ppid=p->parent;

printk("兄弟进程名:\t进程号:\t 进程状态:\t\n");

list_for_each_entry(ppid, &p->real_parent->children, sibling)

{

printk(" %s \t%d\t%ld\t\n",ppid->comm,ppid->pid,p->state);

}

printk("子进程名:\t进程号:\t 进程状态:\t\n");

list_for_each_entry(ppid, &p->children, sibling)

{

printk(" %s \t%d\t%ld\t\n",ppid->comm,ppid->pid,p->state);

}

}

}

#endif

printk("系统当前共%d 个进程!!", count);

return 0;

}

static void __exit list_exit(void)

{

printk( KERN_ALERT "GOOD BYE!!\n");

}

module_init( list_init );

module_exit( list_exit );

MODULE_AUTHOR( "liyou" );

MODULE_LICENSE( "GPL" );

makefile ___

ifneq ($(KERNELRELEASE),)

obj-m := list2.o

else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd)

default:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules elean:

rm -rf *.o *.mod.c *.ko *.syvmers

endif

相关主题
文本预览
相关文档 最新文档