Linux sleep()原理重現

#include<stdio.h>
#include<unistd.h>
#include<signal.h>
void handler(int signo)
{
}

// sigsuspend()函數的功能就是-“解除信號屏蔽”-“掛起進程等待信號”-“執行信號處理函數”- “出錯返回”。
// 所以sigsuspend()函數函數同pause()函數一樣只有出錯返回值。在對程序時序要求比較嚴格的程序中一般使用sigsuspend()函數

int mysleep(int time)
{
// 當前進程 屏蔽SIGALRM信號
sigset_t set, oset;
sigemptyset(&set);
sigaddset(&set, SIGALRM);
sigprocmask(SIG_BLOCK, &set, &oset); // 設置set爲當前進程的信號集,保存進程以前的信號集合oset

// 設置SIGALRM信號的處理方式
struct sigaction act;
struct sigaction oact;
act.sa_handler = handler;
act.sa_mask = set;
act.sa_flags = 0; 
sigaction(SIGALRM, &act, &oact);  // oact,保存SIGALRM信號原來的處理方式


// 鬧鐘函數,在進程中設置一個定時器,當定時器指定的時間到時,它向進程發送SIGALRM信號
alarm(time);


sigset_t susmask = oset;
sigdelset(&susmask, SIGALRM);

// “解除信號屏蔽” - “掛起進程等待信號” - “執行信號處理函數” - “出錯返回”
sigsuspend(&susmask);

int _time = alarm(0);

// 重置進程對SIGALRM信號原來的處理方式
sigaction(SIGALRM, &oact, NULL);

// 重置進程原來的信號集
sigprocmask(SIG_BLOCK, &oset, NULL);


return _time;

}

int main()
{
while (1)
{
printf(“hello yingying\n”);
mysleep(2);
}

return 0;

}

參考:
https://www.jb51.net/article/112394.htm

數據庫:事務特性:acid,如何實現acid,
https://blog.csdn.net/hxpjava1/article/details/79409395
http://blog.jobbole.com/108569/
https://blog.csdn.net/javaMare/article/details/85163824

https://www.cnblogs.com/AndyAo/p/8228099.html

https://www.w3cschool.cn/architectroad/architectroad-two-phase-commit.html
http://oceanbase.org.cn/?p=195
https://weibo.com/ttarticle/p/show?id=2309403965965003062676

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章