首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

32位linux内核2.6.38.2增添系统调用,并编写模块模拟ps命令显示进程信息

2012-07-02 
32位linux内核2.6.38.2添加系统调用,并编写模块模拟ps命令显示进程信息现在我用的是虚拟机下ubuntu10.10,

32位linux内核2.6.38.2添加系统调用,并编写模块模拟ps命令显示进程信息
现在我用的是虚拟机下ubuntu10.10,内核版本已经升级到2.6.38.2,要完成的任务有:

1,在内核的结构体task_struct中添加一个数据成员ctx,记录每个进程切换(被调度)的次数,并编写一个模块显示进程的信息,包括该数据成员的值;

2,在内核中增加一个系统调用,并编写用户态程序调用该系统调用;

首先,准备内核内核源码。

1,make mrproper;清除之前编译内核时的残存配置文件,和一些生成的映像,(据说可以不执行make mrproper和make clean来实现增量编译,但个人认为不可取,至于linux内核的增量编译目前个人并不懂,希望有人能讲解一下)。

2,为了任务1;在task_struct中添加一个数据成员,在include/linux/sched.h中找到task_struct所在位置,添加一个成员

#include <linux/init.h> #include <linux/module.h> #include <linux/proc_fs.h> //proc_fs #include <linux/seq_file.h> //seq_file #include <linux/fs.h> //struct file,struct inode #include <linux/sched.h>    //next_task() MODULE_AUTHOR("vampire"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("a test module utilise the seq_file mechanism"); static void *ps_seq_start(struct seq_file *s,loff_t *pos){ struct task_struct *task; seq_printf(s,"%s\t%s\t%s\t%s\t%s\t%s\n","pid","ppid","uid","gid","comm","ctx"); if(*pos>0) return NULL; else{ task=next_task(current); return task; } } static void *ps_seq_next(struct seq_file *s,void *v,loff_t *pos){ struct task_struct *task=(struct task_struct *)v; ++*pos; if(task->pid== current->pid){ return NULL; }else{ task=next_task(task); return task; } } static void ps_seq_stop(struct seq_file *s,void *v){} static int ps_seq_show(struct seq_file *s,void *v){ rwlock_t lock = RW_LOCK_UNLOCKED; struct task_struct *task=(struct task_struct *)v; read_lock(&lock); seq_printf(s,"%d\t%d\t%d\t%d\t%s\t%d\n",task->pid,task->parent->pid,task->uid,task->gid,task->comm,task->ctx); read_unlock(&lock); return 0; } static struct seq_operations ps_seq_ops = { .start = ps_seq_start, .next = ps_seq_next, .stop = ps_seq_stop, .show = ps_seq_show }; static int ps_open(struct inode *inode,struct file *file){ return seq_open(file,&ps_seq_ops); } static struct file_operations ps_file_ops = { .owner = THIS_MODULE, .open = ps_open, .read  =  seq_read, .llseek = seq_lseek, .release= seq_release }; static int __init ps_init(void){ struct proc_dir_entry *entry; entry = create_proc_entry("plist",0,NULL); if(entry) entry->proc_fops = &ps_file_ops; return 0; } static void __exit ps_exit(void){ remove_proc_entry("plist",NULL); } module_init(ps_init); module_exit(ps_exit);

热点排行