免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: mq110
打印 上一主题 下一主题

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

论坛徽章:
0
11 [报告]
发表于 2005-12-26 17:25 |显示全部楼层
原帖由 思一克 于 2005-12-26 09:52 发表
richardhesidu 用 seqfile 比较好,避免了可能的内存问题。


除了seqfile, 还是觉得直接写char *page比较合理. 这样就没有smp的问题.

论坛徽章:
0
12 [报告]
发表于 2005-12-26 18:17 |显示全部楼层
原帖由 guotie 于 2005-12-26 18:02 发表


这个程序在我这里测试是有问题的。

具体问题在于有时候会出现乱码,有时候正常,而且有时候出现以下错误:
** glibc detected *** double free or corruption (out): 0x0804d8c8 ***

我的系统是LFS 2. ...


把memset(buf,0,sizeof(buf));
改为memset(buf,0,1024*8 );
我在改楼主的程序的时候没有考虑buf的变化。

[ 本帖最后由 richardhesidu 于 2005-12-26 18:18 编辑 ]

论坛徽章:
0
13 [报告]
发表于 2005-12-26 19:52 |显示全部楼层
原帖由 guotie 于 2005-12-26 18:02 发表


这个程序在我这里测试是有问题的。

具体问题在于有时候会出现乱码,有时候正常,而且有时候出现以下错误:
** glibc detected *** double free or corruption (out): 0x0804d8c8 ***

我的系统是LFS 2. ...


这样会好一些:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/sched.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("xunil@bmy");

static int ps_read(char *page, char **start, off_t off,int count, int *eof, void *data)  
{
            int len = 0;
            struct task_struct *p;
            rwlock_t tasklist=RW_LOCK_UNLOCKED;

            read_lock(&tasklist);
            for_each_process(p)
                     len += sprintf(page+len,"%d\t\t%d\t\t\t%s\n",p->pid,p->parent->pid,p->comm);
            read_unlock(&tasklist);

            if (len <= off+count) *eof = 1;
            *start = page + off;
            len -= off;
            if (len>count) len = count;
            if (len<0) len = 0;
            return len;
}


static __init int ps_init(void)
{
            struct proc_dir_entry *entry;

            entry = create_proc_entry("_ps", 0444, &proc_root);
            if(entry == 0)
                    goto error;

           entry->mode = S_IFREG | 0444;
           entry->size = 0;
           entry->read_proc = ps_read;
           return 0;

error:
           printk(KERN_ERR "create_proc_entry() failed !\n");
           return -1;
}
static __exit void ps_cleanup(void)
{
            remove_proc_entry("_ps", &proc_root);
}

module_init(ps_init);
module_exit(ps_cleanup);

[ 本帖最后由 richardhesidu 于 2005-12-27 15:08 编辑 ]

论坛徽章:
0
14 [报告]
发表于 2005-12-27 12:10 |显示全部楼层
原帖由 guotie 于 2005-12-27 11:53 发表



这不是问题的关键。

问题依旧。


会不会是你系统的问题. 我这边测试都是没有问题的. 我的内核是2.6.9的.

论坛徽章:
0
15 [报告]
发表于 2006-01-06 20:34 |显示全部楼层
原帖由 jeffshia 于 2006-1-5 23:47 发表
static char buf[1024*8]={0};

这个不会出错么?
32位机?
改为kmalloc吧


在这里用kmalloc也是不合理的.

论坛徽章:
0
16 [报告]
发表于 2006-01-06 21:20 |显示全部楼层
原帖由 jeffshia 于 2006-1-6 20:39 发表
那么你觉得怎么用比较合适呢?


linux有现成的机制去做这些. 比如seq_file或者是直接写char *page
如果自己创建缓冲区的话, 效率会比较低. 而且很麻烦.

可能我水平有限, 如果大哥有什么好的方法用kmalloc, 并且能解决效率和不一致的问题, 请赐教!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP