Time and ClocksClock & Timer conceptsTime.h (page R:Ch9 pp302-320)Timing a functionTime (P156)Time struct tmPOSIX XSIP307 Measure running time using gettimeofdayMeasure running time using gettimeofdayGettimeofday limitationsP308 A program to test the resolution of gettimeofdayA program to test the resolution of gettimeofdayRealtime clocksElapsed time versus processor timeSleep FunctionsNanosleepPOSIX:XSI Interval timersItimersPowerPoint PresentationSlide 20Slide 21P320 POSIX:XSI Interval Timer used to time functionXSI Interval TimersInterval Timers(Following slides are beyond CS241 requirements)POSIX:TMR Interval Timerstimer_createInterval POSIX:TMR examplesParametersCreating a timertimer_deleteTimer operationsP328 Display Message every 2 secondsSlide 34Slide 35Slide 36CS 241 Spring 2007System Programming 01/14/19 CS241 © 2007 LA, RHC and YZ, All Rights Reserved1Time and ClocksRR: Ch 9Lawrence Angrave2Clock & Timer conceptsAccuracyResolutionOverrunLatencyDrift3Time.h (page R:Ch9 pp302-320)#include <time.h>time_t time(time_t *calptr);Epoch: 00:00 (midnight), Jan 1, 1970 GMTDay is 86,400 secondstime_t is usually a longIf the long is 32 bits, time overflows in 2038ExtensionsPOSIX:XSI microsecondsPOSIX:TMR nanoseconds4Timing a function#include <stdio.h>#include <time.h>void function_to_time(void);int main(void) { time_t tstart; tstart = time(NULL); function_to_time(); printf(“function_to_time took %f seconds of elapsed time\n”, difftime(time(NULL), tstart)); return(0);}5Time (P156)struct tm *localtime(const time_t *timer);Takes time since epoch, returns datestruct tm *gmtime(const time_t *timer);Takes time since epoch, returns UTCchar *ctime(const time_t *clock); 26 byte date string in asciichar *asctime(const struct tm *timeptr); 26 byte date string in ascii6Time struct tmint tm_sec;int tm_min;int tm_hour;int tm_mday;int tm_mon;int tm_year;int tm_wday;int tm_yday;int tm_isdst;7POSIX XSIstruct timevaltime_t tv_sec; /* seconds since the Epoch*/time_t tv_usec /* and microsoeconds*/#include <sys/time.h>int gettimeofday(struct timeval *restrict tp, void *restrict tzp);tzp is null, historical8P307 Measure running time using gettimeofday#include <stdio.h>#include <sys/time.h>#define MILLION 1000000Lvoid function_to_time(void);int main(void) {long timedif;struct timeval tpend;struct timeval tpstart;if (gettimeofday(&tpstart, NULL)) { fprintf(stderr, “Failed to get start time\n”); return 1; }9Measure running time using gettimeofdayfunction_to_time(); /* timed code goes here */if (gettimeofday(&tpend, NULL)) { fprintf(stderr, “Failed to get end time\n”); return 1; }timedif = MILLION*(tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec – tpstart.tv_usec;printf(“The function_to_time took %ld microseconds\n”, timedif);return 0;}10Gettimeofday limitationsResolution small number of microsecsMany consecutive calls of get.. Will return same value11P308 A program to test the resolution of gettimeofday#include <stdio.h>#include <sys/time.h>#define MILLION 1000000L#define NUMDIF 20int main(void) { int i; int numcalls = 1; int numdone = 0; long sum = 0;long timedif[NUMDIF];struct timeval tlast;struct timeval tthis;if (gettimeofday(&tlast, NULL)) { fprintf(stderr, “Failed to get gettimeofday\n”); return 1; }12A program to test the resolution of gettimeofdaywhile (numdone < NUMDIF) { numcalls++; if (gettimeofday(&tthis, NULL)) { fprintf(stderr, “Failed to get a later gettimeofday.\n”); return 1; } timedif[numdone] = MILLION*(tthis.tv_sec – tlast.tv_sec) + tthis.tv_usec –tlast.tv_usec; if (timedif[numdone] != 0 { numdone++; tlast=this; } }………………….13Realtime clocksPOSIX:TMR Library –l rtclockid_t variables --- Can create virtual clocksSystem wide clock in all implementations is CLOCK_REALTIME#include <time.h>struct timespec time_t tv_sec; long tv_nsec; /*NANOSECS*/#include <time.h>int clock_getres(clockid_t clock_id, struct timespec *res);int clock_gettime(clockid_t clock_id, struct timespec *tp);int clock_settime(clockid_t clock_id, struct timespec *tp);Clock_getres displays nominal resolution in nanoseconds of timers and timer interrupts – usually larger and unrelated to clock_gettime for timing-14Elapsed time versus processor timeThe time function measures elapsed time or wall clock time.The virtual time for a process is the amount of time that the process spends in the running state#include <sys/times.h>clock_t times(struct tms *buffer);See sysconf on page P5315Sleep Functions#include <unistd.h>unsigned sleep(unsigned seconds);sleep interacts with SIGALRM16Nanosleep#include <time.h>Int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);Resolution of CLOCK_REALTIME is of order 10msDoes not affect SIGALRM17POSIX:XSI Interval timersGenerates a signal after a time interval repeatedly – periodicstruct timeval it_value; /* time until next expiration*/struct timeval it_interval; /* value to reload into the timer */Timeval structrue has fields for seconds and microseconds#include <sys/time.h>int getitimer(int which, struct itimerval *value);int setitimer(int which, const struct itimerval *restrict value, struct itimerval *restrict ovalue);18ItimersIt_interval = 0 then timer wont restart19#include <errno.h>#include <signal.h>#include <stdio.h>#include <unistd.h> #include <sys/time.h>/* ARGSUSED */static void myhandler(int s) { char aster = '*'; int errsave; errsave = errno; write(STDERR_FILENO, &aster, 1); errno = errsave;}Print asterix every 2 seconds20static int setupinterrupt(void) { /* set up myhandler for SIGPROF */ struct sigaction act; act.sa_handler = myhandler; act.sa_flags = 0; return (sigemptyset(&act.sa_mask) || sigaction(SIGPROF, &act, NULL));}/* Create signal handler */ static int setupitimer(void) /* set ITIMER_PROF for 2-second intervals*/ struct itimerval value; value.it_interval.tv_sec = 2; value.it_interval.tv_usec = 0; value.it_value = value.it_interval; return (setitimer(ITIMER_PROF, &value, NULL));}/* Create timer to generate signals */21int main(void) { if (setupinterrupt()) { perror("Failed to set up handler for SIGPROF"); return 1; } if (setupitimer() == -1) { perror("Failed to set up the ITIMER_PROF interval timer"); return 1; } for ( ; ; ); /* execute rest of main program here */}Set up signal and handler to create *s. Set up interval timer to create signals22P320 POSIX:XSI Interval Timer
View Full Document