天天看點

Linux信号實踐(5) --時間與定時器

1.sleep

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.

2.usleep(以微秒為機關)

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(以納秒為機關)

req指定睡眠的時間, rem傳回剩餘的睡眠時間

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

參數

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

  第二個參數是請求的時間

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

which值

  ITIMER_REAL: 經過指定的時間後,核心将發送SIGALRM信号給本程序 (用的較多)

  ITIMER_VIRTUAL : 程式在使用者空間執行指定的時間後,核心将發送SIGVTALRM信号給本程序 

  ITIMER_PROF : 程序在核心空間中執行時,時間計數會減少,通常與ITIMER_VIRTUAL共用,代表程序在使用者空間與核心空間中運作指定時間後,核心将發送SIGPROF信号給本程序。

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

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

繼續閱讀