{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 7 - Timers and Signals - Time Timers and Signals...

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

View Full Document Right Arrow Icon
1 Time, Timers and Signals Intro to function pointers
Image of page 1

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

View Full Document Right Arrow Icon
2 Applications of Times and Timers Process scheduling Timeouts network protocols User input Periodic updates and system statistics Event management
Image of page 2
3 POSIX Time POSIX specification Time in terms of seconds since Epoch 1 day = 86,400 secs Epoch 00:00 (midnight), January 1, 1970 Coordinated Universal Time (UTC or Greenwich Mean Time GMT)
Image of page 3

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

View Full Document Right Arrow Icon
4 POSIX Extensions POSIX:XSI Extension microseconds resolution POSIX:TMR nanoseconds resolution
Image of page 4
5 Time Since Epoch NAME time - get time in seconds RETURN time in seconds since epoch SYNOPSIS #include <time.h> time_t time(time_t *t); PARAMETER time is also stored in *t if t is not NULL
Image of page 5

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

View Full Document Right Arrow Icon
6 Notes on time time_t time resolution in seconds usually as big as long data type How long does it take for time_t to overflow? 32-bit => Signed = 2 billion seconds from 1970 ( 2038 ) Unsigned = 4 billion seconds from 1970 ( 2100 ) 64-bit => much much longer
Image of page 6
7 Time Difference NAME difftime - calculate time difference RETURN time difference in seconds SYNOPSIS #include <time.h> double difftime(time_t time1, time_t time0);
Image of page 7

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

View Full Document Right Arrow Icon
8 difftime() sample #include <stdio.h> #include <time.h> int main ( void ) { time_t t0; int i = 7 ; int y; t0 = time(NULL); while (i > 0 ) { i -- ; y = 0xFFFFFFF ; while (y > 0 ) y -- ; } printf( "Executed in %lf seconds.\n" , difftime(time(NULL), t0)); return 0 ; }
Image of page 8
9 Date and Time NAME ctime, gmtime, localtime, mktime, asctime transform date and time to broken-down time or ASCII SYNOPSIS #include <time.h> char *ctime(const time_t *timep); struct tm *gmtime(const time_t *timep); struct tm *localtime(const time_t *timep); char *asctime(const struct tm *tm); time_t mktime(struct tm *tm);
Image of page 9

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

View Full Document Right Arrow Icon
10 struct tm struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ };
Image of page 10
11 Date and Time gmtime() converts the calendar time timep to broken-down time representation, expressed in Coordinated Universal Time (UTC) localtime() converts the calendar time timep to broken-time representation expressed relative to the user's specified time zone.
Image of page 11

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

View Full Document Right Arrow Icon
12 localtime() #include <stdio.h> #include <time.h> int main ( void ) { struct tm * current; time_t t = time(NULL); current = localtime( & t); printf ( "%d days before Xmas\n" , 359 - current -> tm_yday); return 0 ; }
Image of page 12
13 Date and Time asctime() Converts broken-down time into string It converts the calendar time_t into a string of the form "Wed Jun 30 21:49:08 1993\n" mktime() Converts broken-down time into time_t
Image of page 13

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

View Full Document Right Arrow Icon
14 asctime() #include <stdio.h> #include <time.h> int main ( void ) { time_t t0; t0 = time (NULL); printf ( "%s" , asctime(localtime( & t0))); /* Mon Aug 4 13:20:25 2008 */ return 0 ; }
Image of page 14
15 Date and Time ctime() Functions all take an argument of data type time_t Equivalent to asctime(localtime(t)) It converts the calendar time_t into a string of the form "Wed Jun 30 21:49:08 1993\n" Uses a static variable to store converted time in string format
Image of page 15

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

View Full Document Right Arrow Icon
16 ctime() #include <stdio.h> #include <time.h> int main ( void ) { time_t t0; t0 = time (NULL); printf ( "%s" ,ctime( & t0)); /* Mon Aug 4 13:20:25 2008 */ return 0 ; }
Image of page 16
Image of page 17
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