各位大侠:为什么在对内核进行操作时用printk函数而不是printf呢,而且此程序不能运行.
我的一段程序为:
#include <linux/kernel.h> /* Were doing kernel work */
#include <linux/module.h> /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Initialize the module */
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* cant be loaded */
return 0;
}
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
printf is in the user mode
printk is in the kernel mode
And you ask why ?
I dont know I only know how to use it.
your code no problem.
gcc -c hello.c -I/usr/src/linux-2.4/include -DMODULE -D__KERNEL__
如果你的机器中内核源码的链接文件为linux-2.4,则试试
gcc -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux-2.4/include -c hello.c
如果为linux,则试试
gcc -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux/include -c hello.c
这个程序不是直接运行的,是模块
加载用insmod hello.o
卸载用rmmod hello
如果加载不上,就在init函数里面加上一句MODULE_LICENSE("GPL");