Linux内核编程入门的第一个程序

书本再好也难免出错,调试通才是硬道理,运行通才是真正的硬道理。
 
hellomod.c
 
#include <linux/netdevice.h>
#include <linux/kernel.h>
#include <linux/init.h>
 
static int __init lkp_init(void)
{
         printk("<1>Hello,World! from the kernel space...\n");
         return 0;
}
 
static void __exit lkp_cleanup(void)
{
         printk("<1>Goodbye,World! leaving kernel space...\n");
}
module_init(lkp_init);
module_exit(lkp_cleanup);
MODULE_LICENSE("GPL");
Makefile
obj-m += hellomod.o
 
doit
make -C /usr/src/kernels/2.6.18-274.7.1.el5-i686 SUBDIRS=$PWD modules
 
[root@localhost testNic]# ./doit
make: Entering directory `/usr/src/kernels/2.6.18-274.7.1.el5-i686'
   CC [M]   /home/testNic/hellomod.o
   Building modules, stage 2.
   MODPOST
   CC           /home/testNic/hellomod.mod.o
   LD [M]   /home/testNic/hellomod.ko
make: Leaving directory `/usr/src/kernels/2.6.18-274.7.1.el5-i686'
[root@localhost testNic]# insmod hellomod.ko
[root@localhost testNic]# lsmod
Module                                   Size   Used by
hellomod                               5632   0
[root@localhost testNic]# tail /var/log/messages
Nov   1 01:53:12 localhost kernel: Hello,World! from the kernel space...
[root@localhost testNic]# rmmod hellomod
[root@localhost testNic]# tail /var/log/messages
Nov   1 01:53:12 localhost kernel: Hello,World! from the kernel space...
Nov   1 01:57:14 localhost kernel: Goodbye,World! leaving kernel space...
 
太美妙了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值