發(fā)現(xiàn)問(wèn)題的契機(jī):
學(xué)習(xí)異步通知的時(shí)候,自己實(shí)現(xiàn)一個(gè)功能:按鍵控制蜂鳴器,同時(shí)LED燈在閃爍
結(jié)果:LED好像也同時(shí)被按鍵控制了
最后調(diào)試結(jié)果發(fā)現(xiàn):
應(yīng)用層的sleep被驅(qū)動(dòng)層的kill_fasync打斷,所以sleep沒(méi)有執(zhí)行完就重新進(jìn)入下一次循環(huán)了
修改代碼后解決該問(wèn)題
解決邏輯就是:既然休眠函數(shù)會(huì)被打斷,則讓每次休眠的時(shí)間短一點(diǎn)
解釋:
sleep函數(shù)是可以被中斷的,也就是說(shuō)當(dāng)進(jìn)程在睡眠的過(guò)程中,如果被中斷,那么當(dāng)中斷結(jié)束回來(lái)再執(zhí)行該進(jìn)程的時(shí)候,該進(jìn)程會(huì)從sleep函數(shù)的下一條語(yǔ)句執(zhí)行。
說(shuō)明kill_fasync這個(gè)函數(shù)中有中斷發(fā)生。
PS:
為什么sleep被中斷后會(huì)直接執(zhí)行下一句呢?(感謝網(wǎng)友的提問(wèn) @dhy9974)
查看sleep的man手冊(cè)
man 3 sleep
發(fā)現(xiàn)sleep函數(shù)有返回值
SLEEP(3) Linux Programmer's Manual SLEEP(3)
NAME
sleep - sleep for a specified number of seconds
SYNOPSIS
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
DESCRIPTION
sleep() causes the calling thread to sleep either until the number of real-time seconds speci‐
fied in seconds have elapsed or until a signal arrives which is not ignored.
RETURN VALUE
Zero if the requested time has elapsed, or the number of seconds left to sleep, if
the call was interrupted by a signal handler.
返回值:
如果請(qǐng)求的時(shí)間已過(guò),則返回零,
如果調(diào)用被信號(hào)處理程序中斷,則返回剩余的睡眠秒數(shù)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-538610.html
如果想做到真正的睡眠,sleep被中斷了也沒(méi)問(wèn)題的話,可以這么寫(xiě)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-538610.html
int time;
while(time > 0){
time = sleep(time);
}
到了這里,關(guān)于【IMX6ULL驅(qū)動(dòng)開(kāi)發(fā)學(xué)習(xí)】15.IMX6ULL驅(qū)動(dòng)開(kāi)發(fā)問(wèn)題記錄(sleep被kill_fasync打斷)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!