Лабораторная работа 21 / lab2 / Отчет2
.docСанкт-Петербургский Государственный
Электротехнический Университет
Кафедра МОЭВМ
Дисциплина: Системы реального времени
Отчет по лабораторной работе № 2
«Часы и таймеры»
Выполнил: Судаков И.А., 3351
Преподаватель: Селеджи Г.Ц.
Санкт-Петербург
2007
1. Формулировка задания
Написать программу, в которой:
-
Установить текущее время 22ч 43м 30сек 20 января 20012 года
-
Напечатать дату и текущее время
-
Определить (в мксек) и сравнить время вычисления функций sin(x) и tg(x)
-
Определить (в мксек) время вычисления функции sin(x) с использованием таймера.
-
Напечатать ещё раз дату и текущее время
-
Представить отчет, включающий текст программ и протокол работы.
2. Выполнение задания
2.1. Текст программы
#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;
}
2.2 Протокол работы в системе
root@1[knoppix]# gcc -lrt -lm lab2.c
root@1[knoppix]# ./a.out
Resolution of CLOCK_REALTIME:sec=0 nsec=999848
Current time: Sat Oct 19 05:05:10 1985
sintime=0.068007 microseconds
tantime=0.096597 microseconds
sintime=32.010338 nsec
wait timer: 5 seconds
Done
Current time: Sat Oct 19 05:05:16 1985
root@1[knoppix]#