Welcome 微信登录

首页 / 操作系统 / Linux / Linux内核下等待队列的使用

1、wait_queue的使用需要的头文件#include<linux/wait.h>typedef struct __wait_queue wait_queue_t;struct __wait_queue {unsigned int flags;#define WQ_FLAG_EXCLUSIVE 0x01void *private;wait_queue_func_t func;struct list_head task_list;};struct __wait_queue_head {spinlock_t lock;struct list_head task_list;};typedef struct __wait_queue_head wait_queue_head_t;Static wait_queue_head_t waitq;用到的函数:extern void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *);#define init_waitqueue_head(q) do { static struct lock_class_key __key; \__init_waitqueue_head((q), &__key); } while (0)用于初始化一个wait_queue_head_t变量#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)唤醒waitq里的processes,该操作唤醒所有的processes用于block的函数:void interruptible_sleep_on(wait_queue_head_t *q);void sleep_on(wait_queue_head_t *q);上面两组中wait_up与wait_up_interruptible()的区别是:如果你是用 interruptible_sleep_on() 来将 process 放到 wait_queue 时,如果有人送一个 signal 给这个 process,那它就会自动从 wait_queue 中醒来。但是如果你是用 sleep_on() 把 process 放到 wq 中的话,那不管你送任何的 signal 给它,它还是不会理你的。除非你是使用 wake_up() 将它叫醒。sleep 有两组。wake_up 也有两组。wake_up_interruptible() 会将 wq 中使用 interruptible_sleep_on() 的 process 叫醒。至于 wake_up() 则是会将 wq 中所有的 process 叫醒。包括使用 interruptible_sleep_on() block的 process。使用sleep的步骤Static  wait_queue_head_t waitq;
init_waitqueue_head(&waitq);wake_up_interruptible(&waitq)interruptible_sleep_on(&waitq) 注意:以上函数只能在内核层运行,不能在用户层运行。Linux autotools的使用Linux下安装配置MemCached(以及libevent)相关资讯      Linux内核 
  • IT人员必须了解的六项Linux内核变  (今 12:05)
  • Linux 内核更新:3.10.98、3.14.62  (02月26日)
  • Linux:让手机运行主线内核  (11/26/2015 22:16:17)
  • Linux内核自防护项目  (05月24日)
  • Linux 内核架构的理解  (12/09/2015 09:01:01)
  • Linux内核被指缺乏安全性  (11/07/2015 08:28:47)
本文评论 查看全部评论 (0)
表情: 姓名: 字数