QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 860|回复: 14

请教内核模块编程的几个问题,谢谢您的帮助!!!

[复制链接]
发表于 2003-7-9 10:20:25 | 显示全部楼层 |阅读模式
现在有几个问题需要您的帮助:
1 我想用kmalloc在内核中申请内存,而编译时总提示第二个参数"GFP_xx undeclared",请问是怎么回事(我的程序头文件包含有:slab.h,malloc.h,mm.h)。
2 请问内核编程中spin_lock,rw_lock两个锁怎么使用.
在那里有参考资料.

非常感谢您的帮助!!!
发表于 2003-7-9 10:26:52 | 显示全部楼层
<GNU/Linux编程指南> 2nd
回复

使用道具 举报

发表于 2003-7-9 11:37:03 | 显示全部楼层
can u tell me, kernel version u use. the compile flag u use. the GFP_xx u used.

2) see LDD2.
回复

使用道具 举报

 楼主| 发表于 2003-7-9 18:27:30 | 显示全部楼层
Dragonfly,
thank you very much for you response.
kernel(i'm using) version is:2.4.18-3, comile flag:-Wall -DMODULE
-D_KERNEL_ -DLINUX,the last parameter of kmalloc is:GFP_ATOMIC

best wishes to u.
回复

使用道具 举报

发表于 2003-7-9 21:30:23 | 显示全部楼层
do u have -I/usr/src/linux-2.4/include  ?

回复

使用道具 举报

发表于 2003-7-9 22:21:12 | 显示全部楼层
你能不能把源代码给大家看看
那样大家也好帮你
回复

使用道具 举报

 楼主| 发表于 2003-7-10 09:27:37 | 显示全部楼层
really thank every here,the following is my source code,please check it.
makefile:
CC=gcc
MODCFLAGS := -Wall -DMODULE -D_KERNEL_ -DLINUX

test1.o: test1.c
        $(CC) $(MODCFLAGS) -c test1.c
        echo insmod test1.o to turn it on
        echo rmmod test1 to turn it off
        echo
test1.c
#include <linux/kernel.h>
#include <linux/module.h>
#include </linux/mm.h>
#include <linux/malloc.h>
int init_module()
{
    printk("Hello,It is a simple module\n");
    kmalloc(1024*10,GFP_ATOMIC);
    printk("The module in the  kernel\n");
    return 0;
}

void cleanup_module()
{
    printk("The module exits the kernel\n");
}

thank a lot for your help!!!
回复

使用道具 举报

发表于 2003-7-10 09:41:24 | 显示全部楼层
1)change to this
MODCFLAGS := -Wall -DMODULE -D_KERNEL_ -DLINUX -I/usr/src/linux-2.4/include

2) kmalloc. but not kfree?
回复

使用道具 举报

 楼主| 发表于 2003-7-10 10:01:42 | 显示全部楼层
Dragonfly, thank you.
1.  
I have changed my makefile as you said(add -I/usr/src/linux-2.4/include),but the problem is as same as before. I want to test memory leak, so i don't use kfree.
2.
I have found #define GFP_xx .... in the mm.h, and i included mm.h with full path(#include </usr/src/linux-2.4/include/linux/mm.h>),but the result still is:
GFP_xx undeclared, what's the reason.

thanks.
回复

使用道具 举报

发表于 2003-7-10 11:00:47 | 显示全部楼层
-D__KERNEL__ , not -D_KERNEL_

make sure u have /usr/src/linux-2.4 dir.

change u include to
[code:1]
#include &lt;linux/kernel.h&gt;
#include &lt;linux/module.h&gt;
#include &lt;linux/slab.h&gt;
[/code:1]
回复

使用道具 举报

 楼主| 发表于 2003-7-10 13:19:50 | 显示全部楼层
Dragonfly, thank you very much for you help,and I solved the problem. I made a mistake,I used -D_KERNEL_,  not -D__KERNEL__ ,you are great!
other things need your help:
1.
mm.h exists in not one directory,it exists in /usr/include and,/usr/include/linux and other directories, and their content are different,how can I know which directory should be used in my programs.
2.
where can i find the usage of spin_lock,rw_lock
best wishes to you!
回复

使用道具 举报

发表于 2003-7-10 21:12:15 | 显示全部楼层
write #include&lt;linux/mm.h&gt; in u code.

the -I will point to /usr/src/linux/incude/linux/mm.h correctly

LDD2 book has descriptin on it.

http://www.xml.com/ldd/chapter/book/ch09.html#t8
回复

使用道具 举报

 楼主| 发表于 2003-7-11 09:44:59 | 显示全部楼层
Dragonfly,
thanks,the web address you offered is very useful.
something i still don't understand:
1.
if i just write #include&lt;linux/mm.h&gt; in my prog,then use gcc to compile and link it(don't use -I option to give include path),i want to know which path is used actually,because mm.h exists in a several different directories(/usr/include and,/usr/include/linux and other directories).
2.
if i want to give a definite path  in my progs how can i know which path i shoud use.

by the way,
i have known the usage of spin_lock,rw_lock through reading a lot of docs from internet,thank you.now I want create process and thread in kernel module progs,but i can't find functions to create process and thread in kernel module,hope you can tell me.

really appreciate you help!!!
回复

使用道具 举报

发表于 2003-7-11 22:18:42 | 显示全部楼层
if u do not -I, it uses /usr/include/linux/mm.h

2 can not do in code. u need to do it in makefile

kernel_thread. i believe that link has this info. search other chapters.
回复

使用道具 举报

 楼主| 发表于 2003-7-12 12:40:40 | 显示全部楼层
Dragonfly,
非常感谢你的帮助,还有一个问题可能是我没有说清楚:
如果我使用一个函数,其使用手册中说明要引用一个头文件(比如mm.h),而mm.h文件存在好几个目录中,而且这几个不同地方的头文件内容可能有所不同,我怎么知道要引用那一个目录下的头文件.
best wishes to u.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-15 12:13 , Processed in 0.043865 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表