菩提本无树,明镜变非台 本来无一物,何处惹尘埃
分类: LINUX
2012-04-10 15:02:34
#includelong wait_event_timeout(wait_queue_head_t, condition, long timeout); /* 休眠时不可中断 */ long wait_event_interruptible_timeout(wait_queue_head_t, condition, long timeout);/*可中断*/ #include signed long schedule_timeout(signed long timeout); /* 这个函数用于要使用wake_up这种情况 */
#include如果要使用schedule_timeout的话,"wait_queue_proc"内容应为#include #include /* jiffies */ #include /* waitqueue */ #include #include /* create_proc_read_entry() */ MODULE_LICENSE("Dual BSD/GPL"); int wait_queue_proc(char *buf, char **start, off_t offset, int len, int *eof, void *data) { unsigned long j0, j1; wait_queue_head_t wait; /* 定义一个等待队列 */ init_waitqueue_head(&wait); /* 初始化等待队列 */ j0 = jiffies; wait_event_interruptible_timeout(wait, 0, HZ); /* 设置延迟1秒 */ j1 = jiffies; len = sprintf(buf,"%9ld %9ld\n",j0,j1); *start = buf; return len; } static __init int main_init(void) { create_proc_read_entry("waitqueue", 0, NULL, wait_queue_proc, NULL); return 0; } static __exit void main_exit(void) { remove_proc_entry("wait_queue",NULL); } module_init(main_init); module_exit(main_exit);
unsigned long j0, j1; j0 = jiffies; set_current_state(TASK_INTERRUPTIBLE); /* 设置当前进程状态 */ schedule_timeout(HZ); /* 延迟1秒 */ j1 = jiffies; len = sprintf(buf,"%9ld %9ld\n",j0,j1); *start = buf; return len;这里用到"proc"文件机制;加载该模块后,使用"$dd bs=20 count=5 < /proc/waitqueue",输出结果如下
#includevoid ndelay(unsigned long nsecs); /* 纳秒级 */ void udelay(unsigned long usecs); /* 微秒级 */ void mdelay(unsigned long msecs); /* 毫秒级 */
void msleep(unsigned int millisecs); /* 毫秒级,不可中断 */ unsigned long msleep_interruptible(unsigned int millisecs); /* 毫秒级,可中断 */ void ssleep(unsigned int seconds); /* 秒级,不可中断 */
int wait_queue_proc(char *buf, char **start, off_t offset, int len, int *eof, void *data) { unsigned long j0,j1; j0 = jiffies; mdelay(1000); /* 1秒 */ /* 或者是 ssleep(1); */ j1 = jiffies; len = sprintf(buf,"%9ld %9ld\n",j0,j1); *start = buf; return len; }如果能容忍比所请求更长的延迟,则应使用"schedule_timeout"、"ssleep"。("ldd3"中写道)