免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
41 [报告]
发表于 2005-12-23 21:52 |只看该作者
今天拿楼主的代码在2.4下写了个makefile编译,发现生成warning :kernel.h不能再客户态,还报了一大堆的系统头文件内的错误。
连for_each_process(p),sprintf,printfk等函数显示IMPLICT DEFINED 错误
本人刚接触内核,请问这是什么错误呢!是2.4下不支持吗?

论坛徽章:
0
42 [报告]
发表于 2005-12-23 22:37 |只看该作者
原帖由 conan7205 于 2005-12-23 21:52 发表
今天拿楼主的代码在2.4下写了个makefile编译,发现生成warning :kernel.h不能再客户态,还报了一大堆的系统头文件内的错误。
连for_each_process(p),sprintf,printfk等函数显示IMPLICT DEFINED 错误
本人刚接触 ...

#ifdef FOR_EACH_PROCESS
       for_each_process(p)
#else
       for_each_task(p)
#endif

论坛徽章:
0
43 [报告]
发表于 2005-12-24 11:02 |只看该作者
原帖由 guotie 于 2005-12-21 16:29 发表


提个问题,proc中读函数中的内存如何释放,还是一直都不会被释放?

类似于这个程序中的buffer,好像只能使用堆栈的空间吧,如果使用kmalloc,如何释放其空间?


#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("mq110");

static char *buf;

static int ps_read(char *page, char **start, off_t offset,int count, int *eof, void *data)  
{
           char tmp[128];
            struct task_struct *p;
            rwlock_t tasklist=RW_LOCK_UNLOCKED;

            if(offset >0)
                return 0;
            memset(buf,0,sizeof(buf));
            read_lock(&tasklist);
            for_each_process(p)
            {
                sprintf(tmp,"%d\t\t%d\t\t\t%s\n",p->pid,p->parent->pid,p->comm);
                strcat(buf,tmp);
                        memset(tmp,0,sizeof(tmp));
            }
            read_unlock(&tasklist);
            *start=buf;
            return strlen(buf);
}

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

        buf = (char *)kmalloc(1024*8,GFP_KERNEL);
        if(buf == NULL)
                goto error1;

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

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

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

module_init(ps_init);
module_exit(ps_cleanup);

论坛徽章:
0
44 [报告]
发表于 2005-12-26 09:38 |只看该作者
不错。牛啊

论坛徽章:
0
45 [报告]
发表于 2005-12-26 09:52 |只看该作者
richardhesidu 用 seqfile 比较好,避免了可能的内存问题。

论坛徽章:
0
46 [报告]
发表于 2005-12-26 12:01 |只看该作者
你这样解决也好。
收下了。
对了,seqfile 好像比较复杂啊!

论坛徽章:
0
47 [报告]
发表于 2005-12-26 12:28 |只看该作者

回复 1楼 mq110 的帖子

your ps_read(...) isn't reentrance. you may get strange result on SMP machine.

I think use seqfile is a good solution. (Actually the usage of seqfile is very simple)

[ 本帖最后由 daemeon 于 2005-12-26 12:41 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
48 [报告]
发表于 2005-12-26 12:46 |只看该作者
原帖由 daemeon 于 2005-12-26 12:28 发表
your ps_read(...) isn't reentrance. you may get strange result on SMP machine.

I think use seqfile is a good solution. (Actually the usage of seqfile is very simple)


恩.接受意见.多谢.

论坛徽章:
0
49 [报告]
发表于 2005-12-26 12:52 |只看该作者
原帖由 daemeon 于 2005-12-26 12:28 发表
your ps_read(...) isn't reentrance. you may get strange result on SMP machine.

I think use seqfile is a good solution. (Actually the usage of seqfile is very simple)


对确实是seqfile好一些. 我是给guotie兄演示一下怎么用kmalloc(), 没有考虑smp的情况, 见笑了.

论坛徽章:
0
50 [报告]
发表于 2005-12-26 13:45 |只看该作者
原帖由 daemeon 于 2005-12-26 12:28 发表
your ps_read(...) isn't reentrance. you may get strange result on SMP machine.

I think use seqfile is a good solution. (Actually the usage of seqfile is very simple)


BTW: 请教一下daemeon兄我的ps_read()在smp上问题出在哪儿吗? 我不太懂smp.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP