タイマsetimerとgettimeofday現在時刻【Linuxマイクロ秒レベル】を取得
12130 ワード
文書ディレクトリ setimer遅延、タイミング パラメータ 戻り値 動作機構 例 SIGALRM信号 システム現在時刻 を取得する.パラメータ 戻り値 例 setitimer遅延、タイミング
#include int setitimer( int which, const struct itimerval * new_value, struct itimerval * old_value );
struct itimerval{ struct timeval it_interval;//サイクル実行時間 struct timeval it_value;//遅延実行時間};
struct timeval{ time_t tv_sec;//秒 suseconds_t tv_usec;//マイクロ秒};
パラメータ
which : ITIMER_REAL:システムの実際の時間で計算し、SIGALRM信号を送る. ITMER_VIRTUAL:当該プロセスがユーザ状態でかかる時間で計算し、SIGVTALRM信号を送出する. ITMER_PROF:当該プロセスがユーザ状態とカーネル状態でかかる時間で計算し、SIGPROF信号を送出する.
old_value:一般的にNULLに設定され、前回のsetimer呼び出しで設定したnew_を格納します.value.
戻り値
呼び出しは正常に0を返し、そうでなければ-1を返します.
動作メカニズム
it_valueカウントダウン、0の場合トリガ信号、it_valueをit_にリセットinterval、カウントダウンを続け、サイクル実行します.
サイクル実行時、it_valueは0ではなくit_を設定しますinterval; 実行遅延時にit_を設定value,it_intervalは0です.
例
SIGALRM信号
#include
POSIX対応プラットフォームでは、SIGALRMはタイマ終了時にプロセスに送信される信号である.SIGは、信号名の汎用プレフィックスである.ALRMはalarmの略、すなわちタイマである.
タイマーを使うには、まずSIGALRM信号をインストールします.インストールしない場合、プロセスが信号を受信すると、デフォルトの動作は現在のプロセスを終了します.
システムの現在時刻の取得
#include #include int gettimeofday( struct timeval * tv, struct timezone * tz );
struct timeval{ long tv_sec;//秒 long tv_usec;//マイクロ秒};
struct timezone{ int tz_minuteswest;//グリニッジとの時間差は何分ですか int tz_dsttime;//日光節約時間の状態};
パラメータ
tv:現在時刻tzを返す:ローカルタイムゾーン情報
戻り値
呼び出しは正常に0を返し、そうでなければ-1を返します.
例
参考資料:https://blog.csdn.net/lixianlin/article/details/25604779 https://blog.csdn.net/zaishaoyi/article/details/20239997 https://baike.baidu.com/item/SIGALRM/22777025 https://blog.csdn.net/xiewenhao12/article/details/78707101 http://c.biancheng.net/cpp/html/142.html
#include int setitimer( int which, const struct itimerval * new_value, struct itimerval * old_value );
struct itimerval{ struct timeval it_interval;//サイクル実行時間 struct timeval it_value;//遅延実行時間};
struct timeval{ time_t tv_sec;//秒 suseconds_t tv_usec;//マイクロ秒};
パラメータ
which : ITIMER_REAL:システムの実際の時間で計算し、SIGALRM信号を送る. ITMER_VIRTUAL:当該プロセスがユーザ状態でかかる時間で計算し、SIGVTALRM信号を送出する. ITMER_PROF:当該プロセスがユーザ状態とカーネル状態でかかる時間で計算し、SIGPROF信号を送出する.
old_value:一般的にNULLに設定され、前回のsetimer呼び出しで設定したnew_を格納します.value.
戻り値
呼び出しは正常に0を返し、そうでなければ-1を返します.
動作メカニズム
it_valueカウントダウン、0の場合トリガ信号、it_valueをit_にリセットinterval、カウントダウンを続け、サイクル実行します.
サイクル実行時、it_valueは0ではなくit_を設定しますinterval; 実行遅延時にit_を設定value,it_intervalは0です.
例
#include
#include
#include
void signalHandler(int signo){
switch (signo){
case SIGALRM:
printf("Caught the SIGALRM signal!
");
break;
}
}
// 1 SIGALRM , 200 SIGALRM 。
int main(int argc, char *argv[]){
// SIGALRM
signal(SIGALRM, signalHandler);
struct itimerval new_value, old_value;
new_value.it_value.tv_sec = 0;
new_value.it_value.tv_usec = 1;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_usec = 200000;
setitimer(ITIMER_REAL, &new_value, &old_value); f
or(;;);
return 0;
}
SIGALRM信号
#include
POSIX対応プラットフォームでは、SIGALRMはタイマ終了時にプロセスに送信される信号である.SIGは、信号名の汎用プレフィックスである.ALRMはalarmの略、すなわちタイマである.
タイマーを使うには、まずSIGALRM信号をインストールします.インストールしない場合、プロセスが信号を受信すると、デフォルトの動作は現在のプロセスを終了します.
システムの現在時刻の取得
#include #include int gettimeofday( struct timeval * tv, struct timezone * tz );
struct timeval{ long tv_sec;//秒 long tv_usec;//マイクロ秒};
struct timezone{ int tz_minuteswest;//グリニッジとの時間差は何分ですか int tz_dsttime;//日光節約時間の状態};
パラメータ
tv:現在時刻tzを返す:ローカルタイムゾーン情報
戻り値
呼び出しは正常に0を返し、そうでなければ-1を返します.
例
#include
#include
#include
#include
#include
int main(){
struct timeval tv;
gettimeofday(&tv,NULL);
printf("second:%ld
",tv.tv_sec);//
printf("millisecond:%ld
",tv.tv_sec*1000 + tv.tv_usec/1000);//
printf("microsecond:%ld
",tv.tv_sec*1000000 + tv.tv_usec); //
sleep(3);
std::cout << "3s later:" << std::endl;
gettimeofday(&tv,NULL);printf("second:%ld
",tv.tv_sec);//
printf("millisecond:%ld
",tv.tv_sec*1000 + tv.tv_usec/1000);//
printf("microsecond:%ld
",tv.tv_sec*1000000 + tv.tv_usec); //
return0;
}
// ,
int getCurrentTime(){
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}
参考資料:https://blog.csdn.net/lixianlin/article/details/25604779 https://blog.csdn.net/zaishaoyi/article/details/20239997 https://baike.baidu.com/item/SIGALRM/22777025 https://blog.csdn.net/xiewenhao12/article/details/78707101 http://c.biancheng.net/cpp/html/142.html