免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: mq110

写了一个内核模块 实现简单的类似ps命令. [复制链接]

论坛徽章:
0
发表于 2005-11-23 13:48 |显示全部楼层
嗯, 原来memset()是为了那个作用, 没注意到.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2005-11-23 17:44 |显示全部楼层
多谢 platinum 斑竹加精.

论坛徽章:
0
发表于 2005-11-27 11:11 |显示全部楼层
贴一个我写的吧. 纯粹是为了讨论, 免得有人说我侵犯知识产权.

#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("xunil@bmy");
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\n","pid","ppid","uid","gid","comm");
       
        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\n",task->pid,task->parent->pid,task->uid,task->gid,task->comm);
        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("myps",0,NULL);
        if(entry)
                entry->proc_fops = &ps_file_ops;
        return 0;
}

static void __exit ps_exit(void){
        remove_proc_entry("myps",NULL);
}

module_init(ps_init);
module_exit(ps_exit);

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2005-11-27 12:54 |显示全部楼层
学习.

论坛徽章:
0
发表于 2005-12-17 15:03 |显示全部楼层
学习。。。

论坛徽章:
0
发表于 2005-12-17 16:41 |显示全部楼层
原帖由 mq110 于 2005-11-23 13:27 发表
>>memset(buf,0,sizeof(buf));
~~~~~~~~~~~~~~~~~~~~~~~~~~.bss数据段自动初始化为0, 没必要memset()的

这是静态空间啊..如果多执行几次的话.(不清0)肯定会超出buf的范围的会.溢出的。....

即使是如此,
还是建议应该用
strcpy( buf, "" ) 来代替。

从语义上来讲,memset 是操作内存段,而 strcpy 是操作字符串,因此后者更加使得程序易懂。
况且,也要快一些。

论坛徽章:
0
发表于 2005-12-17 16:57 |显示全部楼层
linux中task结构和内核栈的大小是8K,局部变量太多或者buffer太长容易stack overflow。向这种小模块就用kmalloc,如果大型模块,可以自己写个slab分配器。呵呵

论坛徽章:
0
发表于 2005-12-17 17:10 |显示全部楼层
还有,就是那个for_each_process(for_each_task)处不需要加读所,设计成for_each_process的目的就是在读的时候不需要读锁了。呵呵

论坛徽章:
0
发表于 2005-12-17 17:15 |显示全部楼层
原帖由 richardhesidu 于 2005-11-27 11:11 发表
贴一个我写的吧. 纯粹是为了讨论, 免得有人说我侵犯知识产权.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>        //proc_fs
#include <linux/seq_file. ...

这个比较professional,呵呵
laohe200304 该用户已被删除
发表于 2005-12-17 22:10 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP