1. spa
#define cond_resched() ({ \ ___might_sleep(__FILE__, __LINE__, 0); \ _cond_resched(); \ }) int __sched _cond_resched(void) { if (should_resched(0)) { preempt_schedule_common(); return 1; } return 0; } static __always_inline bool should_resched(int preempt_offset) { return unlikely(preempt_count() == preempt_offset && tif_need_resched()); } #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED) 一个A进程, 在用户态时, 时间滴答若是选出新的高优先级B进程的话, 会当即进程切换, 若是A进程处于 内核态时,滴答不能当即切换B进程, 只是在A进程的thread_info.flag标志 TIF_NEED_RESCHED , 而后A进程 从内核态返回用户态时, 系统判断若是有这个TIF_NEED_RESCHED标志, 就立马切换到B进程,因此内核态的程序 (驱动)若是while(1); 除了中断会获得执行, 其余代码都没机会运行, 所以一个驱动处理完事情尽快返回用户态, 或者主动调用sleep(), 先休眠, 若是既想贪心执行, 又脸皮薄怕其余进程等过久, 能够经过cond_resched() 判断 一下自身有没有TIF_NEED_RESCHED, 有的话说明有更急的进程须要执行, 那就切换, 不然就问心无愧的跑(不是我不让, 是大家没叫我让)
2.线程
cd->health_thread = kthread_run(genwqe_health_thread, cd, GENWQE_DEVNAME "%d_health", cd->card_idx); kthread_stop(cd->health_thread); static int genwqe_health_thread(void *data) { while (!kthread_should_stop()) { rc = wait_event_interruptible_timeout(cd->health_waitq, (genwqe_health_check_cond(cd, &gfir) || (should_stop = kthread_should_stop())), genwqe_health_check_interval * HZ); } } 若是一个线程能够一直作某事就直接while(1) 不然就增长一个判断条件
3.code
#include <stdio.h> void test(int a) { switch(a) { case 1: printf("1\n"); break; default: printf("default\n"); break; case 0: printf("0\n"); break; } } void main() { test(5); printf("==============\n"); test(0); } default ============== 0