日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux信号实践(5) --时间与定时器

發布時間:2025/3/17 linux 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux信号实践(5) --时间与定时器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

三種不同精度的睡眠

1.sleep

#include <unistd.h> unsigned int sleep(unsigned int seconds);

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.

//示例 int sleepTime = 5; do {sleepTime = sleep(sleepTime); } while (sleepTime > 0);

2.usleep(以微秒為單位)

int usleep(useconds_t usec);

The??type?useconds_t?is?an?unsigned?integer?type?capable?of?holding?integers?in?the?range?[0,1000000].?

Programs?will?be?more?portable?if?they?never?mention?this?type??explicitly.

3.nanosleep(以納秒為單位)

#include <time.h> int nanosleep(const struct timespec *req, struct timespec *rem);

req指定睡眠的時間,?rem返回剩余的睡眠時間

struct timespec {time_t tv_sec; /* seconds: 秒 */long tv_nsec; /* nanoseconds: 納秒 */ };

三種時間結構

time_t struct timeval {long tv_sec; /* seconds */long tv_usec; /* microseconds 微秒*/ }; struct timespec {time_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */ };

setitimer

#include <sys/time.h> int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));

setitimer()比alarm功能強大,支持3種類型的定時器

參數

? 第一個參數which指定定時器類型

? 第二個參數是請求的時間

? 第三個參數是定時器原來所關聯的值

struct itimerval {struct timeval it_interval; /* next value : 產生信號的間隔時間*/struct timeval it_value; /* current value : 第一次產生信號的時間*/ }; struct timeval {time_t tv_sec; /* seconds: 秒 */suseconds_t tv_usec; /* microseconds: 微秒 */ };

which值

? ITIMER_REAL: 經過指定的時間后,內核將發送SIGALRM信號給本進程?(用的較多)

? ITIMER_VIRTUAL?: 程序在用戶空間執行指定的時間后,內核將發送SIGVTALRM信號給本進程?

? ITIMER_PROF?: 進程在內核空間中執行時,時間計數會減少,通常與ITIMER_VIRTUAL共用,代表進程在用戶空間與內核空間中運行指定時間后,內核將發送SIGPROF信號給本進程。

/**示例1: 1.在啟動進程的5秒之后產生信號 2.然后每隔1秒產生一次信號 **/ int main() {if (signal(SIGALRM, signalAction) == SIG_ERR)err_exit("signal error");struct itimerval it;struct timeval it_interval = {1, 0};struct timeval it_value = {5, 0};it.it_interval = it_interval;it.it_value = it_value;if (setitimer(ITIMER_REAL, &it, NULL) == -1)err_exit("setitimer error");while (true)pause(); } /**示例2: 獲取itimerval 結構體 **/ int main() {struct itimerval it;struct timeval it_interval = {1, 0};struct timeval it_value = {5, 0};it.it_interval = it_interval;it.it_value = it_value;if (setitimer(ITIMER_REAL, &it, NULL) == -1)err_exit("setitimer error");for (int i = 0; i < 100000; ++i);struct itimerval oldIt;// if (setitimer(ITIMER_REAL, &it, &oldIt) == -1) // err_exit("setitimer error");// 在不重新設置時鐘的情況下獲取剩余時間if (getitimer(ITIMER_REAL, &oldIt) == -1)err_exit("getitimer error");cout << oldIt.it_interval.tv_sec << ' ' << oldIt.it_interval.tv_usec<< ' ' << oldIt.it_value.tv_sec << ' ' << oldIt.it_value.tv_usec << endl; }

附:秒-微秒-納秒的轉換

? S(秒)、ms(毫秒)、μs(微秒)、ns(納秒),其中:1s=1000ms,1?ms=1000μs,1μs=1000ns

總結

以上是生活随笔為你收集整理的Linux信号实践(5) --时间与定时器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。