三个内部定时器的问题

包含网卡/无线网的网络问题和ADSL/校园网/宽带拨号支持及代理/共享等网络使用问题
回复
louxiaoyan
帖子: 6
注册时间: 2011-04-07 15:47
送出感谢: 0
接收感谢: 0

三个内部定时器的问题

#1

帖子 louxiaoyan » 2011-06-10 18:48

今天照着书写了个定时器的程序,如下:
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>

/*函数声明*/
/*信号处理函数*/
static void SignHandler(int signo);

/*操作辅助函数*/
void TimerPassed(const struct itimerval *itimer, struct timeval *tv);
void TimeSubstract(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tvres);
int TimeCompare(const struct timeval *tv1, const struct timeval *tv2);
void PrintTimeval(const char *str, const struct timeval *tv);

int main()
{
struct itimerval myitimer;
long i,count;
struct itimerval tmp_itimer;

/*注册信号处理函数*/
if(signal(SIGUSR1,SignHandler)== SIG_ERR)
{
printf("Unable to creat handle for SIGUSR1\n");
exit(0);
}
if(signal(SIGALRM,SignHandler)== SIG_ERR)
{
printf("Unable to creat handle for SIGALRM\n");
exit(0);
}
//运行参数输入
printf("Loop times between timer info outputs (300 Recommanded):\n");
scanf("%ld", &count);
count *= 1000000;

/*初始化定时器参数*/
myitimer.it_interval.tv_sec = 10;
myitimer.it_interval.tv_usec = 0;
myitimer.it_value.tv_sec = 10;
myitimer.it_value.tv_usec = 0;

/*注册定时器*/
setitimer(ITIMER_REAL, &myitimer, NULL);
setitimer(ITIMER_VIRTUAL, &myitimer, NULL);
setitimer(ITIMER_PROF, &myitimer, NULL);

while(1)
{
for (i=0; i<count;i++);
raise(SIGUSR1);
}
}

static void SignHandler(int signo)
{
struct itimerval tmp_itimer;
struct timeval realtv, cputv, usertv, kerneltv;

//获得实时定时器时间
getitimer(ITIMER_REAL, &tmp_itimer);
TimerPassed(&tmp_itimer, &realtv);
//获得CPU定时器时间
getitimer(ITIMER_PROF, &tmp_itimer);
TimerPassed(&tmp_itimer, &cputv);
//获得用户定时器时间
getitimer(ITIMER_VIRTUAL, &tmp_itimer);
TimerPassed(&tmp_itimer, &usertv);
//计算Linux内核使用CPU时间
TimeSubstract(&cputv, &usertv, &kerneltv);

//按照信号进行处理
switch(signo)
{
case SIGUSR1:
PrintTimeval("Real Time ", &realtv);
PrintTimeval("CPU Time ", &cputv);
PrintTimeval("User Time ", &usertv);
PrintTimeval("Kernel Time ", &kerneltv);
printf("\n");
break;
case SIGALRM:
printf("Time up, the application will escape.\n");
PrintTimeval("CPU Time ", &cputv);
PrintTimeval("User Time ", &usertv);
PrintTimeval("Kernel Time ", &kerneltv);
exit (0);
break;
}
}
//计算时间的流逝
void TimerPassed(const struct itimerval *itimer, struct timeval *tv)
{
TimeSubstract(&(itimer->it_interval), &(itimer->it_value),tv);
}
//计算两个时间的差值
void TimeSubstract(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tvres)
{
const struct timeval *tmptv1, *tmptv2;
int cmpres;

cmpres = TimeCompare(tv1,tv2);
if (cmpres > 0)
{
tmptv1 = tv1;
tmptv2 = tv2;
}
else
{
tmptv1 = tv2;
tmptv2 = tv1;
}
if (tmptv1->tv_usec < tmptv2 ->tv_usec)
{
tvres->tv_sec = tmptv1->tv_sec - tmptv2->tv_sec - 1;
tvres->tv_usec = tmptv1->tv_usec +1000000 - tmptv2->tv_usec;
}
else
{
tvres->tv_sec = tmptv1->tv_sec - tmptv2->tv_sec;
tvres->tv_usec = tmptv1->tv_usec - tmptv2->tv_usec;
}
}
//时间值比较大小
int TimeCompare(const struct timeval *tv1, const struct timeval *tv2)
{
if(tv1->tv_sec > tv2->tv_sec)
return 1;
else if (tv1->tv_sec < tv2->tv_sec)
return -1;
else if (tv1->tv_usec > tv2->tv_usec)
return 1;
else if (tv1->tv_usec < tv2->tv_usec)
return -1;
else
return 0;
}
//打印时间
void PrintTimeval(const char *str, const struct timeval *tv)
{
printf("%s = %ld sec %ld usec\n", str, tv->tv_sec, tv->tv_usec);
}

运行结果中kernel time一直是0,难道这个程序内核没有进行调度?不是很理解。书上kernel time是有值的。。。。请各位理解定时器用法的老师们赐教。然后,这个程序定义了三种定时器,会不会相互影响呢。。。
回复

回到 “网卡问题以及网络和拨号”