Lecture8 - CSCE313: Spring2011 Dr.RonnieWard TimesandTimers...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCE 313: Introduction to Computer Systems Spring 2011 Dr. Ronnie Ward
Image of page 1

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

View Full Document Right Arrow Icon
Times and Timers Chapters 9 in textbook Slides adopted and modified from Dr. Bettati.
Image of page 2
Time and Timers The time Epoch The Current Time Sleeping and Waiting Timers Reading: R&R, Ch 9
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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 */ }
Image of page 5

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

View Full Document Right Arrow Icon
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); }
Image of page 6
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 */ }
Image of page 7

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

View Full Document Right Arrow Icon
Measure the running time using clock_gettime
Image of page 8
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 */
Image of page 9

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

View Full Document Right Arrow Icon
How long does  usleep  actually sleep? #include … #define COUNT 100 #define DTIME 1000 int main(void) { struct timeval tpstart; struct timeval tpend; printf(">> %d iterations of usleep(%d)...", COUNT, DTIME); gettimeofday (&tpstart, NULL); for(int i = 0; i < COUNT; i++) { usleep (DTIME); } gettimeofday (&tpend, NULL); printf("done"); long timedif = MILLION * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec; printf(": Time: %ld musec\n", timedif); }
Image of page 10
Sleep Resolution: Mac OS X  [email protected] mac [~/MyDocuments/Classes/313/mini case studies] % a.out
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern