Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2151556
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-11-25 11:46:49

1. 功能
init中起两个线程,
  线程1等侍
  线程2每隔1秒唤醒线程1
2. 代码
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/workqueue.h>
  4. #include <linux/device.h>
  5. #include <linux/errno.h>
  6. #include <linux/workqueue.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irqreturn.h>
  11. #include <linux/types.h>
  12. #include <linux/delay.h>
  13. #include <linux/clk.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/sched.h>
  18. #include <linux/kthread.h>
  19. #include <linux/wait.h>

  20. static wait_queue_head_t mythread_wq;
  21. struct task_struct *wait_thread_task;
  22. struct task_struct *waken_thread_task;
  23. static int wake_up_condition = 0;

  24. static int my_wait_thread(void* data)
  25. {
  26.     while(1)
  27.     {
  28.         wait_event_interruptible(mythread_wq, wake_up_condition);
  29.         wake_up_condition = 0;
  30.         printk("cong: %s:%d, wakeup \n",__FUNCTION__, __LINE__);
  31.     }
  32. }

  33. static int my_waken_thread(void* data)
  34. {
  35.     while(1)
  36.     {
  37.         wake_up_condition = 1;
  38.         wake_up_interruptible(&mythread_wq);
  39.         msleep(1000);
  40.         printk("cong: %s:%d, waken\n",__FUNCTION__, __LINE__);
  41.     }
  42. }

  43. static int __init hello_init(void)
  44. {
  45.     printk(KERN_ALERT "my first driver\n");
  46.     init_waitqueue_head(&mythread_wq);
  47.     wait_thread_task = kthread_create(my_wait_thread, NULL, "my_wait_thread");
  48.     if (IS_ERR(wait_thread_task)) {
  49.         return PTR_ERR(wait_thread_task);
  50.     }
  51.     wake_up_process(wait_thread_task);

  52.     waken_thread_task = kthread_create(my_waken_thread, NULL, "my_waken_thread");
  53.     if (IS_ERR(waken_thread_task)) {
  54.         return PTR_ERR(waken_thread_task);
  55.     }
  56.     wake_up_process(waken_thread_task);
  57.     return 0;
  58. }

  59. static void __exit hello_exit(void)
  60. {
  61.     printk(KERN_ALERT "goodbye my first dirver\n");
  62.     return ;
  63. }

  64. module_init(hello_init);
  65. module_exit(hello_exit);
  66. MODULE_LICENSE("GPL");
3. 说明
a. 关于kthread
  kthread_create创建的线程不会马上运行,需要调用wake_up_process()后线程开始执行
  kthread_run 会创建并启动线程的函数 
b. 关于wait_event
wait_event_interruptible()
wake_up_interruptible(&mythread_wq);
要想唤醒wait_event_interruptible()的进程,需要滿足两个条件:
  a.condition为真的前提下        //刚开始以为只要这个condition为真就可以了,后来发现不行
  b.还需要调用用wake_up

4.运行结果
  1. [ 40.075829].(0)[1467:insmod]my first driver
  2. [ 40.094922].(0)[1468:my_wait_thread]cong: my_wait_thread:32, wakeup
  3. [ 41.101196].(0)[1469:my_waken_thread]cong: my_waken_thread:43, waken
  4. [ 41.102065].(1)[1468:my_wait_thread]cong: my_wait_thread:32, wakeup
  5. [ 42.111162].(0)[1469:my_waken_thread]cong: my_waken_thread:43, waken
  6. [ 42.112043].(1)[1468:my_wait_thread]cong: my_wait_thread:32, wakeup
  7. [ 43.121312].(0)[1469:my_waken_thread]cong: my_waken_thread:43, waken
  8. [ 43.122197].(1)[1468:my_wait_thread]cong: my_wait_thread:32, wakeup
  9. [ 44.131133].(0)[1469:my_waken_thread]cong: my_waken_thread:43, waken
  10. [ 44.132010].(1)[1468:my_wait_thread]cong: my_wait_thread:32, wakeup
  11. [ 45.141168].(1)[1469:my_waken_thread]cong: my_waken_thread:43, waken
  12. [ 45.142013].(0)[1468:my_wait_thread]cong: my_wait_thread:32, wakeup
  13. [ 46.151078].(0)[1469:my_waken_thread]cong: my_waken_thread:43, waken


阅读(1595) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~