4种方法: 1> 忙等待: - void 1_delay(u32 sec)
- {
- unsigned long end_point = jiffies + sec*HZ;
-
- while(!time_before_eq(end_point, jiffies)){
- /*nothing*/;
- }
- }
- #define time_after(a,b) \
- (typecheck(unsigned long, a) && \
- typecheck(unsigned long, b) && \
- ((long)(b) - (long)(a) < 0)) //学会这招!!,复习有符号到无符号的转换
- #define time_before(a,b) time_after(b,a) //如此巧妙
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2> 让出CPU: - void 2_delay(u32 sec)
- {
- unsigned long end_point = jiffies + sec*HZ;
-
- while(!time_before_eq(end_point, jiffies)){
- schedule();
- }
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3> 等待队列实现: - void 3_delay(u32 s)
- {
- wait_queue_head_t head;
- init_waitqueue_head(&head);
-
- wait_event_interruptible_timeout(head, 0, s*HZ);
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4> 最理想的实现: - void 4_delay(u32 s)
- {
- set_current_state(TASK_INTERRUPTIBLE);
- schedule_timeout(s*HZ);
- }
阅读(3911) | 评论(0) | 转发(0) |