本节介绍下kernel thread简单使用的例子实验.
我的系统:
joseph:/usr/src/linux-2.6.23/joseph# uname -a
Linux joseph 2.6.23 #1 SMP PREEMPT Fri May 6 18:02:45 CST 2011 i686 GNU/Linux
文件:
├── hello.c
└── Makefile
1. hello.c
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/sched.h>
- #include <linux/kthread.h>
- MODULE_LICENSE("Dual BSD/GPL");
-
- //static DECLARE_WAIT_QUEUE_HEAD(myevent_waitqueue);
- //rwlock_t myevent_lock;
- static int mykthread(void* unused)
- {
- daemonize("josephThread");
-
- // reuest delivery of SIGKILL
- allow_signal(SIGKILL);
- for(;;) {
- //relinquish the processor until the event occurs
- set_current_state(TASK_INTERRUPTIBLE);
- schedule();//allow other parts of the kernel to run
- //Die if I receive SIGKILL
- if (signal_pending(current)) break;
- }
-
- printk("josephThread out <------<<<<< /n");
- set_current_state(TASK_RUNNING);
- return 0;
- }
- static int hello_init(void)
- {
- printk("Hello, I am a test module/n");
- //create my kernel thread
-
- printk("Create mythread<---------<<<<<<<</n");
- kernel_thread(mykthread, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD);
- return 0;
- }
- static void hello_exit(void)
- {
- printk("Bye, my dear!/n Cruel world/n");
- }
2. Makefile
obj-m := hello.o
3. 编译
#make -C /usr/src/linux-2.6.23 M=`pwd` modules
// /usr/src/linux-2.6.23 是我的源码路径
4. 清除记录的信息
#echo "" > /var/log/messages
5. 插入模块
# insmod hello.ko
6. 查看信息
# cat /var/log/messages
May 7 20:24:10 joseph kernel: [ 4281.427586] Hello, I am a test module
May 7 20:24:10 joseph kernel: [ 4281.427758] Create mythread<---------<<<<<<<<
查看我们的内核线程
#ps -ef
...
root 4030 2 0 20:24 ? 00:00:00 [josephThread]
...
7. 结束内核线程
#kill -s 9 4030
查看信息
# cat /var/log/messages
May 7 20:24:10 joseph kernel: [ 4281.427586] Hello, I am a test module
May 7 20:24:10 joseph kernel: [ 4281.427758] Create mythread<---------<<<<<<<<
May 7 20:27:22 joseph kernel: [ 4472.245694] josephThread out <------<<<<<
8. 卸载模块
#rmmod hello
查看信息
#cat /var/log/messages
May 7 20:24:10 joseph kernel: [ 4281.427586] Hello, I am a test module
May 7 20:24:10 joseph kernel: [ 4281.427758] Create mythread<---------<<<<<<<<
May 7 20:27:22 joseph kernel: [ 4472.245694] josephThread out <------<<<<<
May 7 20:28:43 joseph kernel: [ 4553.589958] Bye, my dear!
May 7 20:28:43 joseph kernel: [ 4553.589960] Cruel world
另一个例子:
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/sched.h>
-
- MODULE_AUTHOR("T-bagwell_CU");
- MODULE_LICENSE("GPL");
-
- static DECLARE_WAIT_QUEUE_HEAD(myevent_waitqueue);
- extern unsigned int myevent_id;
-
-
- static int example_kernel_thread(void *unused)
- {
- DECLARE_WAITQUEUE(wait, current);
-
- daemonize("create_by_T-bag");
- allow_signal(SIGKILL);
- add_wait_queue(&myevent_waitqueue, &wait);
-
- while(1){
- set_current_state(TASK_INTERRUPTIBLE);
- schedule();
-
- if(signal_pending(current)){
- break;
- }
- }
-
- set_current_state(TASK_RUNNING);
- remove_wait_queue(&myevent_waitqueue, &wait);
- printk(KERN_WARNING "This is in example_kernel_thread\n");
-
- return 0;
- }
-
- static __init int init_hello_kernel_thread(void)
- {
- int ret;
-
- ret=kernel_thread(example_kernel_thread, NULL,
- CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD );
-
- if(unlikely(ret<0)){
- printk(KERN_WARNING "kernel_thread create failed \n");
- }
- else{
- printk(KERN_WARNING "kernel_thread create success \n");
- }
-
- return 0;
- }
-
- static __exit void cleanup_hello_kernel_thread(void)
- {
- printk(KERN_WARNING "kernel_thread exit \n");
- return ;
- }
-
- module_init(init_hello_kernel_thread);
- module_exit(cleanup_hello_kernel_thread);
-
-
写一个Makefile来编译这个module
- KERNELDIR = /usr/src/kernels/2.6.27.5-117.fc10.i686
- PWD := $(shell pwd)
- obj-m := kernel_thread.o
-
- modules:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
- clean:
- rm -rf *.o *.ko test_chrdev Module.* module* *.mod.c
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。