#include
#include
#include
#include
#include
#include
//這是對信號的處理函數(shù)
static void signalDeal(int signo)
{
struct timeval tp;
struct tm *tm;
gettimeofday(&tp,NULL); //獲得系統(tǒng)當(dāng)前時間(秒和微秒)
tm=localtime(&tp.tv_sec); //獲得當(dāng)?shù)啬壳皶r間和日期
printf(" sec = %ld \t",tp.tv_sec); //打印從UNIX紀(jì)元開始到現(xiàn)在的秒數(shù)
printf(" usec = %ld \n",tp.tv_usec); //打印微秒 printf("%d-%d-%d%d:%d:%d\n",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec); /*打印當(dāng)?shù)啬壳皶r間和日期*/
}
//時間初始化函數(shù)
static void InitTime(int tv_sec,int tv_usec)
{
struct itimerval value; //定義時間參數(shù)結(jié)構(gòu)體value
signal(SIGALRM, signalDeal); //注冊信號SIGALRM 和信號處理函數(shù)
value.it_value.tv_sec = tv_sec; //秒
value.it_value.tv_usec = tv_usec; //微秒
value.it_interval.tv_sec = tv_sec;
value.it_interval.tv_usec = tv_usec;
setitimer(ITIMER_REAL, &value, NULL);
//setitimer 發(fā)送信號,定時類型為ITIMER_REAL
}
//主函數(shù)
int main(int argc,char *argv[])
{
InitTime(1,0); //每隔1秒打印一次
while(1)
{
}
exit(0);
}
本文標(biāo)題:[Linux信號]使用setitimer函數(shù)進行精確定時
網(wǎng)站URL:
http://weahome.cn/article/jsejje.html