Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 21 / lab2 / lab2
.c#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
void PrintResTime()
{
struct timespec t;
printf("Resolution of CLOCK_REALTIME:");
if(clock_getres(CLOCK_REALTIME,&t)==-1) printf("clock_getres error\n");
//warn();
printf("sec=%d nsec=%d\n",t.tv_sec,t.tv_nsec);
}
void PrintCurrentTime()
{
time_t t=time(0);
struct tm* locT=localtime(&t);
char* st=asctime(locT);
printf("Current time: %s\n",st);
}
void SetTime()
{//19 October 1985 5:05:10
struct tm locT;
locT.tm_sec=10;
locT.tm_min=5;
locT.tm_hour=5;
locT.tm_mday=19;
locT.tm_mon=9;
locT.tm_year=85;
time_t t=mktime(&locT);
if(t==-1) printf("mktime error\n");
struct timespec tsp;
tsp.tv_sec=t;
tsp.tv_nsec=1;
if(clock_settime(CLOCK_REALTIME,&tsp)==-1) printf("clock_settime error\n");
//warn();//describe errors
}
void SinAndTanTime()
{
int nmax=10000000;
int i=0;
float res=0;
struct timeval tv1,tv2;
//sin
if(gettimeofday(&tv1,NULL)==-1) printf("gettimeofday error\n");
for(i=0;i<nmax;i++)
res=sin(i);
if(gettimeofday(&tv2,NULL)==-1) printf("gettimeofday error\n");
tv2.tv_usec += 1000000*(tv2.tv_sec-tv1.tv_sec);
res = (float)(tv2.tv_usec - tv1.tv_usec)/nmax;
printf("sintime=%f microseconds\n",res);
//tan
if(gettimeofday(&tv1,NULL)==-1) printf("gettimeofday error\n");
for(i=0;i<nmax;i++)
res=tan(i);
if(gettimeofday(&tv2,NULL)==-1) printf("gettimeofday error\n");
tv2.tv_usec += 1000000*(tv2.tv_sec-tv1.tv_sec);
res = (float)(tv2.tv_usec - tv1.tv_usec)/nmax;
printf("tantime=%f microseconds\n",res);
}
void Timer()
{
int i=0;
float res=0;
int nmax=10000000;
struct sigevent sigx;
struct itimerspec val1;
struct itimerspec val2;
timer_t t_id;
sigx.sigev_notify=SIGEV_SIGNAL;
sigx.sigev_signo=SIGUSR1;
val1.it_value.tv_sec=5;
timer_create(CLOCK_REALTIME,&sigx,&t_id);
timer_settime(t_id,0,&val1,0);
timer_gettime(t_id,&val1);
for( i=0;i<nmax;i++) res=sin(i);
timer_gettime(t_id,&val2);
res = (float)(val2.it_value.tv_nsec - val1.it_value.tv_nsec)/nmax;
printf("sintime=%f nsec\n",res);
printf("wait timer: 5 seconds\n");
}
void SigUsr1Handler()
{
printf("Done\n");
PrintCurrentTime();
}
int main()
{
PrintResTime();
SetTime();
PrintCurrentTime();
SinAndTanTime();
signal(SIGUSR1,SigUsr1Handler);
Timer();
pause();
return 0;
}