3、h>定義方法:靜態(tài)?DECLARE_QUEUE_HEAD(name)?????????????????????動態(tài)?wait_queue_head_t?my_queue;????????????????????????????????init_waitqueue_head(&my_queue);?struct__wait_queue_head{????spinlock_tlock;????structlist_headtask_list;};typedefstruct__wait_queue_headwait_queue_head_t;?簡單休眠linu
4、x最簡單的休眠方式是wait_event(queue,condition)及其變種,在實(shí)現(xiàn)休眠的同時(shí),它也檢查進(jìn)程等待的條件。四種wait_event形式如下:wait_event(queue,condition);/*不可中斷休眠,不推薦*/wait_event_interruptible(queue,condition);/*推薦,返回非零值意味著休眠被中斷,且驅(qū)動應(yīng)返回-ERESTARTSYS*/wait_event_timeout(queue,condition,timeout);wait_event_interruptible_timeout(
5、queue,conditon,timeout);/*有限的時(shí)間的休眠,若超時(shí),則不管條件為何值返回0*/?喚醒休眠進(jìn)程的函數(shù):wake_upvoid?wake_up(wait_queue_head_t?*queue);void?wake_up_interruptible(wait_queue_head?*queue);慣例:用wake_up喚醒wait_event,用wake_up_interruptible喚醒wait_event_interruptible?休眠與喚醒實(shí)例分析:本例實(shí)現(xiàn)效果為:任何從該設(shè)備上讀取的進(jìn)程均被置于休眠。只要某個(gè)進(jìn)程向給設(shè)備
6、寫入,所有休眠的進(jìn)程就會被喚醒。staticDECLARE_WAIT_QUEUE_HEAD(wq);staticintflag=0;ssize_tsleepy_read(structfile*filp,char__user*buf,size_tcount,loff_t*pos){pirntk(KERN_DEBUG"process%i(%s)goingtosleep",current->pid,current->comm);wait_event_interruptible(wq,flag!=0);flag=0;printk(KERN_DEBUG"awo
7、ken%i(%s)",current->pid,current->comm);return0;}?ssize_tsleepy_write(structfile*filp,constchar__user*buf,size_tcount,loff_t*pos){printk(KERN_DEBUG"process%i(%s)awakeningthereaders...",current->pid,current->comm);flag=1;wake_up_interruptible(&wq);returncount;?/*成功并避免重試*/}??阻塞與
8、非阻塞類操作小知識點(diǎn):操作系統(tǒng)中睡眠、阻塞、掛起的區(qū)別形象解釋操作系統(tǒng)中睡眠、阻