200 likes | 371 Views
Chapter 9 Times and Timers. Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003. Introduction. Operating systems use timers for such tasks as Process scheduling Timeouts for network protocols Periodic updates of system statistics
E N D
Chapter 9Times and Timers Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Introduction • Operating systems use timers for such tasks as • Process scheduling • Timeouts for network protocols • Periodic updates of system statistics • Applications access system time and timer functions to • Measure performance • Identify the time when events occur • Implement protocols • Control interaction with users
time_t Type and the time() Function • In POSIX, the Epoch is the start point for measuring time • It is defined as 00:00 (midnight) on January 1, 1970 • The POSIX base standard supports only a time resolution of seconds • It expresses time since the Epoch using a time_t type, which is usually a long integer • A program can access the system time (expressed in seconds since the Epoch) by calling the time() function#include <time.h>time_t time(time_t *timeLocation); • If timeLocation is not NULL, the function also stores the time in the location pointed to by timeLocation • If successful, time() returns the number of seconds; otherwise it returns –1 • If time_t is implemented as a 32-bit long integer, the number will overflow in 2038 • If time_t is implemented as an unsigned 32-bit long integer, the number will overflow in 2106, but this implementation would not allow the time() function to return –1 • If time_ t is implemented as a 64-bit long integer, the overflow will not occur for another 292 billion years
difftime() Function • The difftime() function computes the difference between two time values of type time_t#include <time.h>double difftime(time_t timeA, time_t timeB); • The timeB parameter is subtracted from the timeA parameter (i.e., A – B) • The time resolution for time_t is one second, which may not be accurate enough for some calculations • The program shown on the next two slides calculates the wall-clock (i.e., elapsed) time for a person to answer an addition problem
Example use of time() and difftime() #include <stdio.h> #include <time.h> #define MAX_NUMBER 100 // ******************************* int main(void) { time_t startTime; time_t stopTime; int valueX; int valueY; int guess; int answer; printf("\nAddition Drill Program \n\n"); srand(time(NULL)); valueX = rand() % MAX_NUMBER; valueY = rand() % MAX_NUMBER; answer = valueX + valueY; (More on next slide)
Example use of time() and difftime() (continued) printf(" %2d\n", valueX); printf(" + %2d\n", valueY); printf(" ----\n"); if (answer >= MAX_NUMBER) printf(" "); // Two spaces else printf(" "); // Three spaces startTime = time(NULL); scanf("%d", &guess); stopTime = time(NULL); if (guess == answer) printf("\nCorrect answer!\n"); else printf("\nWrong answer. The correct answer is %d\n", answer); printf("\nElapsed time: %.0f seconds\n", difftime(stopTime, startTime)); return 0; } // End main Sample Program Run uxb2% a.out Addition Drill Program 77 + 8 ---- 85 Correct answer! Elapsed time: 7 seconds uxb2%
Displaying the Date and Time • The localtime() function takes a parameter specifying the seconds since the Epoch and returns a pointer to a struct tm structure containing the components of time (such as day, month, and year) adjusted for local requirements • The asctime() function converts the structure returned by localtime() to a 26-character English-language string Example: Mon Aug 21 10:00:00 2006\n\0 • The ctime() function is equivalent to asctime(localtime(EpochTime))#include <time.h>struct tm *localtime(const time_t *EpochTime);char *asctime(const struct tm *timeRecordPtr);char *ctime(const time_t *EpochTime);
Example use of localtime(), asctime() and ctime() #include <stdio.h> #include <time.h> int main(void) { time_t currentTime; struct tm *localPtr; currentTime = time(NULL); localPtr = localtime(¤tTime); printf("Local time (using asctime): %s", asctime(localPtr)); printf("Local time (using ctime) : %s", ctime(¤tTime)); return 0; } // End main
struct tm Structure • The struct tm structure has the members shown belowint tm_sec; // Seconds after the minute [0, 60]int tm_min; // Minutes after the hour [0, 59]int tm_hour; // Hours since midnight [0, 23]int tm_mday; // Day of the month [1, 31]int tm_mon; // Months since January [0, 11]int tm_year; // Years since 1900int tm_wday; // Days since Sunday [0, 6]int tm_yday; // Days since January 1st [0, 365]int tm_isdst; // Flag indicating daylight savings time
Example use of struct tm Structure #include <stdio.h> #include <time.h> int main(void) { struct tm *localPtr; time_t currentTime; currentTime = time(NULL); localPtr = localtime(¤tTime); printf("Local time : %s\n", ctime(¤tTime)); printf("Seconds after minute: %d\n", localPtr->tm_sec); printf("Minutes after hour : %d\n", localPtr->tm_min); printf("Hours since midnight: %d\n", localPtr->tm_hour); printf("Day of the month : %d\n", localPtr->tm_mday); printf("Months since January: %d\n", localPtr->tm_mon); printf("Years since 1900 : %d\n", localPtr->tm_year); printf("Days since Sunday : %d\n", localPtr->tm_wday); printf("Days since Jan 1st : %d\n", localPtr->tm_yday); printf("DST Flag : %d\n", localPtr->tm_isdst); return 0; } // End main
Sample output for struct tm uxb2% a.out Local time : Wed Jul 26 19:36:37 2006 Seconds after minute: 37 Minutes after hour : 36 Hours since midnight: 19 Day of the month : 26 Months since January: 6 Years since 1900 : 106 Days since Sunday : 3 Days since Jan 1st : 206 DST Flag : 1 uxb2%
gettimeofday() Function • A time resolution of seconds is too coarse for timing programs or controlling program events • The getttimeofday() function retrieves the system time in seconds and microseconds since the Epoch#include <time.h>int gettimeofday(struct timeval *timePtr, void *); • The struct timeval structure pointed to by timePtr receives the retrieved time • The programmer should set the second parameter to NULL • The function returns zero if successful; otherwise, some implementations return –1 and set errno • The struct timeval structure contains two memberstime_t tv_sec; // Seconds since the Epochtime_t tv_usec; // Microseconds after the second
Example#1: use of gettimeofday() #include <stdio.h> #include <time.h> int main(void) { struct timeval timeRecord; int status; status = gettimeofday(&timeRecord, NULL); printf("Seconds since the Epoch : %d\n", timeRecord.tv_sec); printf("Microseconds after seconds: %d\n", timeRecord.tv_usec); return 0; } // End main Sample Program Run uxb2% a.out Seconds since the Epoch : 1153962282 Microseconds after seconds: 592133 uxb2%
Example#2: use of gettimeofday() #include <stdio.h> #include <time.h> #define MILLION 1000000L void functionToTime(void); int main(void) { struct timeval timeStart; struct timeval timeStop; long timeDifference; gettimeofday(&timeStart, NULL); functionToTime(); gettimeofday(&timeStop, NULL); timeDifference = MILLION * (timeStop.tv_sec - timeStart.tv_sec) + timeStop.tv_usec - timeStart.tv_usec; printf("The function took %ld microseconds\n", timeDifference); return 0; } // End main void functionToTime(void) { // Do some I/O or some calculations } // End functionToTime
sleep() Function • A process that voluntarily blocks for a specified time is said to sleep • The sleep() function causes the calling thread to be suspended either until the specified number of seconds has elapsed or until the calling thread catches a signal#include <unistd.h>unsigned int sleep(unsigned int seconds); • In some implementations, the function returns 0 if the requested time has elapsed; otherwise, it returns the amount of unslept time if interrupted • The function interacts with SIGALRM, so the programmer should avoid using them concurrently in the same process
Example use of sleep() #include <stdio.h> #include <unistd.h> int main(void) { int sleepTime; printf("\nPID: %d\n", getpid()); printf("\nHow long should I sleep (sec)? "); scanf("%d", &sleepTime); printf("\nGood night :) ...\n"); sleep(sleepTime); printf("\nThat was a good rest!\n"); return 0; } // End main Sample Program Run uxb2% a.out PID: 21998 How long should I sleep (sec)? 20 Good night :) ... That was a good rest! uxb2%