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) //如此巧妙
2> 让出CPU:
点击(此处)折叠或打开
- void 2_delay(u32 sec)
- {
- unsigned long end_point = jiffies + sec*HZ;
-
- while(!time_before_eq(end_point, jiffies)){
- schedule();
- }
- }
3> 等待队列实现:
点击(此处)折叠或打开
- void 3_delay(u32 s)
- {
- wait_queue_head_t head;
- init_waitqueue_head(&head);
-
- wait_event_interruptible_timeout(head, 0, s*HZ);
- }
4> 最理想的实现:
点击(此处)折叠或打开
- void 4_delay(u32 s)
- {
- set_current_state(TASK_INTERRUPTIBLE);
- schedule_timeout(s*HZ);
- }