Lecture8 - CSCE 313: Introduction to Computer Systems...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSCE 313: Introduction to Computer Systems Spring 2011 Dr. Ronnie Ward Times and Timers Chapters 9 in textbook Slides adopted and modified from Dr. Bettati. Time and Timers The time Epoch The Current Time Sleeping and Waiting Timers Reading: R&R, Ch 9 Current Time UNIX Time Zero : 00.00 (midnight), January 1, 1970 . time() provides second level time resolution This sets up a UNIX version of Y2K: If time_t is long , then overflow happens in Year 2038. IF time_t is unsigned long , then overflow happens in Year 2106. If time_t is long long , then overflow happens in Year 292*10 9 . #include <time.h> time_t time (time_t * tloc); /* returns time (in seconds) since epoch */ Day is 86,400 seconds Other functions for date & time POSIX:XSI (Extended Systems Interface) Timescale of time() seconds is too coarse POSIX:XSI extension provides microsecond time resolution Consecutive calls may return the same value #include <sys/time.h> int gettimeofday (struct timeval * tp, NULL); /* stores time since epoch in tp */ struct timeval { time_t tv_sec; /* second since epoch */ time_t tv_usec; /* microseconds */ } Timing an Activity #include <stdio.h> #include <sys/time.h> void activity_to_time(void); int main(void) { long timedif; struct timeval tpstart, tpend; gettimeofday (&tpstart, NULL); activity_to_time(void); gettimeofday (&tpend, NULL); timedif = 1000000L * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec); printf(The function took %ld musec\n, timedif); } POSIX:TMR (Timers Extension) Realtime clocks Clock: counter that increments at fixed intervals. POSIX:TMR requires a system-wide clock with a clockid_t of value CLOCK_REALTIME At compile time, link with l rt #include <time.h> int clock_getres (clockid_t id, struct timespec * res); /* determines clock resolution */ int clock_gettime (clockid_t id, struct timespec * res); /* retrieve the clock time */ int clock_settime (clockid_t id, struct timespec * res); /* set the clock time */ struct timespec { time_t tv_sec; /* second since epoch */ long tv_nsec; /* nanoseconds */ } Measure the running time using clock_gettime Sleeping and Waiting #include <time.h> unsigned sleep (unsigned seconds); /* sleeps for seconds, returns unslept time if interrupted */ #include <time.h> int nanosleep (const struct timespec * rqtp, struct timespec * rmtp); /* sleeps for amount of time specified in rqtp, stores unslept time in rmtp if interrupted */ #include <unistd.h> int usleep (useconds_t microseconds); /* returns -1 if error or interrupted */ How long does usleep actually sleep?...
View Full Document

This note was uploaded on 02/14/2012 for the course CSCE 313 taught by Professor Gu during the Spring '11 term at Texas A&M.

Page1 / 33

Lecture8 - CSCE 313: Introduction to Computer Systems...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online