打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
kernel thread简单使用
    

本节介绍下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

  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/sched.h>  
  4. #include <linux/kthread.h>  
  5. MODULE_LICENSE("Dual BSD/GPL");  
  6.   
  7. //static DECLARE_WAIT_QUEUE_HEAD(myevent_waitqueue);  
  8. //rwlock_t myevent_lock;  
  9. static int mykthread(void* unused)  
  10. {  
  11.         daemonize("josephThread");  
  12.       
  13.         // reuest delivery of SIGKILL  
  14.         allow_signal(SIGKILL);  
  15.         for(;;) {  
  16.                 //relinquish the processor until the event occurs  
  17.                 set_current_state(TASK_INTERRUPTIBLE);  
  18.                 schedule();//allow other parts of the kernel to run  
  19.                 //Die if I receive SIGKILL  
  20.                 if (signal_pending(current)) break;  
  21.         }  
  22.       
  23.         printk("josephThread out <------<<<<< /n");  
  24.         set_current_state(TASK_RUNNING);  
  25.         return 0;  
  26. }  
  27. static int hello_init(void)  
  28. {  
  29.         printk("Hello, I am a test module/n");  
  30.         //create my kernel thread  
  31.           
  32.         printk("Create mythread<---------<<<<<<<</n");  
  33.         kernel_thread(mykthread, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD);  
  34.         return 0;  
  35. }  
  36. static void hello_exit(void)  
  37. {  
  38.         printk("Bye, my dear!/n Cruel world/n");  
  39. }  


 

2. Makefile

 

obj-m := hello.o

3. 编译 
#make -C /usr/src/linux-2.6.23 M=`pwd` modules
// /usr/src/linux-2.6.23 是我的源码路径
会生成 hello.ko
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

 

另一个例子:

 

 

  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/init.h>  
  4. #include <linux/sched.h>  
  5.   
  6. MODULE_AUTHOR("T-bagwell_CU");  
  7. MODULE_LICENSE("GPL");  
  8.   
  9. static DECLARE_WAIT_QUEUE_HEAD(myevent_waitqueue);  
  10. extern unsigned int myevent_id;  
  11.   
  12.   
  13. static int example_kernel_thread(void *unused)  
  14. {  
  15.         DECLARE_WAITQUEUE(wait, current);  
  16.   
  17.         daemonize("create_by_T-bag");  
  18.         allow_signal(SIGKILL);  
  19.         add_wait_queue(&myevent_waitqueue, &wait);  
  20.   
  21.         while(1){  
  22.                 set_current_state(TASK_INTERRUPTIBLE);  
  23.                 schedule();  
  24.   
  25.                 if(signal_pending(current)){  
  26.                         break;  
  27.                 }  
  28.         }  
  29.   
  30.         set_current_state(TASK_RUNNING);  
  31.         remove_wait_queue(&myevent_waitqueue, &wait);  
  32.         printk(KERN_WARNING "This is in example_kernel_thread\n");  
  33.   
  34.         return 0;  
  35. }  
  36.   
  37. static __init int init_hello_kernel_thread(void)  
  38. {  
  39.         int ret;  
  40.   
  41.         ret=kernel_thread(example_kernel_thread, NULL,  
  42.                                   CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD );  
  43.   
  44.         if(unlikely(ret<0)){  
  45.                 printk(KERN_WARNING "kernel_thread create failed \n");  
  46.         }  
  47.         else{  
  48.                 printk(KERN_WARNING "kernel_thread create success \n");  
  49.         }  
  50.   
  51.         return 0;  
  52. }  
  53.   
  54. static __exit void cleanup_hello_kernel_thread(void)  
  55. {  
  56.         printk(KERN_WARNING "kernel_thread exit \n");  
  57.         return ;  
  58. }  
  59.   
  60. module_init(init_hello_kernel_thread);  
  61. module_exit(cleanup_hello_kernel_thread);  
  62.   
  63.    


写一个Makefile来编译这个module

  1. KERNELDIR = /usr/src/kernels/2.6.27.5-117.fc10.i686  
  2. PWD := $(shell pwd)  
  3. obj-m := kernel_thread.o  
  4.   
  5. modules:  
  6.             $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
  7. clean:  
  8.             rm -rf *.o *.ko test_chrdev Module.* module* *.mod.c  


 

 

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
内核线程
内核并发控制---信号量
嵌入式Linux之Kernel(裁减移植)启动技术
EXPORT_SYMBOL例子
linux2.4与2.6下的模块编程对比
netlink简单介绍 (linux
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服