Sunteți pe pagina 1din 16

Lecture 8

Chapter 9 of Robbins Book

Times and Timers

BIL 244 – System Programming


POSIX Timers

• POSIX specifies that systems should keep time in terms


of seconds since the Epoch ( Epoch is defined as 00:00
January 1 1970 UTC)
• Most operatings need to be measured with timers greater
than one-second resolution.
• Two POSIX extensions,
– POSIX:XSI extension and
– POSIX:TMR extension
define time resolutions of microseconds and
nanoseconds, respectively

BIL 244 – System Programming


Expressing Time in seconds
(since the Epoch)

• POSIX base standard supports only a time


resolution of seconds and expresses time since
Epoch using time_t type (usually long).
• A program can access the system time by calling
the time function
#include <time.h>

time_t time(time_t *tloc);

• The time function stores the time in *tloc. If


succesfull time returns the number of seconds
from Epoch. If unsuccesfull time returns -1
BIL 244 – System Programming
The difftime function

• The difftime function computes the difference


between two calender times of type time_t,
making it convenient for calculations
• The difftime function has two time_t parameters
and returns a doubşe containing the first parameter
minus the second

#include <time.h>

double difftime(time_t time1, time_t time2);

• No errors are defined in difftime

BIL 244 – System Programming


Displaying date and time

• The time_t type is not convinient for displaying the dates (need to
adjuct for factors like time-zone, daylight-saving etc.)
• The localtime function takes a parameter specifying the seconds
since the Epoch and returns a structure with the components of the
time adjusted for local requirements.
• The asctime function converts the structure returned by
localtime to a string
• The ctime function is equivalent to asctime(localtime(clock))
• The gtime function take a parameter representing seconds since the
Epoch and returns a structure with the components of the time
expressed as UTC.

BIL 244 – System Programming


Displaying date and time

#include <time.h>

char *asctime(const struct tm *timeptr);


char *ctime(const time_t *clock);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);

• No errors are defined for these functions.


• Unfortunetly the above functions are not thread safe. The POSIX:TSF
specifies thread safe alternatives of thesse functions as
#include <time.h>
char *asctime_r(const struct tm *restrict timeptr,
char *restrict buf );
char *ctime_r(const time_t *clock, char *buf);
struct tm *gmtime_r(const time_t *restrict timer,
struct tm *restrict result);
struct tm *localtime(const time_t *restrict timer,
struct tm *restrict result);
BIL 244 – System Programming
Using struct timeval to express time

• A time scale of seconds is too coarse for timing


programs or controlling program events
• The POSIX:XSI Extension uses the struct timeval
structure to express time on a finer scale (microseconds)
• Certain POSIX functions that support a timeout option
specify the timeout values by using variables of type
struct timeval

• The gettimeofday function retrieves the system time in


seconds and microseconds since the Epoch.
#include <sys/time.h>

int gettimeofday(struct timeval *restrict tp,


void *restrict tzp);
BIL 244 – System Programming
A program that measıres the running time of a function by using gettimeofday

#include <stdio.h>
#include <sys/time.h>
#define MILLION 1000000L

void 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;
}

function_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;
}
Using realtime clocks

• A clock is a counter which increments at fixed


intervals called clock resolution.
• The POSIX:TMR Timers Extension contains clocks
that are represented by variables of type clockid_t.
• POSIX clocks may be systemwide or only visibale
within a process All implimentations must support a
systemwide clock with a clockid_t value of
CLOCK_REALTIME corresponding to the system
realtime clock.
• Only privileged users may set this clock, but any user
can read it.
BIL 244 – System Programming
Using realtime clocks

• The struct timespec structure specifies time for both


POSIX:TMX clocks and timers, as well as the timeout
values for the POSIX thread functions that support
timeouts.
• POSIX provides functions to set the clock time
(clock_ssettime), to retrieve the clock time
(clock_gettime), and determine the clock resolution
(clock_getres).
• Each of these functions takes two parameters: a
clockid_t used to identify the particular clock and a
pointer to a struct timespec structure

BIL 244 – System Programming


Using realtime clocks

#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,
const struct timespec *tp);

• If successful these functions return 0. If unsuccessful


these functions return -1.
• All three functions set errno to EINVAL if
clockid_t does not specify a known clock.

BIL 244 – System Programming


A program to test the resolution of the CLOCK_REALTIME

#include <stdio.h>
#include <time.h>
#define BILLION 1000000000L
#define NUMDIF 20

int main(void) {
int i;
int numcalls = 1;
int numdone = 0;
long sum = 0;
long timedif[NUMDIF];
struct timespec tlast;
struct timespec tthis;

if (clock_getres(CLOCK_REALTIME, &tlast))
perror("Failed to get clock resolution");
else if (tlast.tv_sec != 0)
printf("Clock resolution no better than one second\n");
else
printf("Clock resolution: %ld nanoseconds\n", (long)tlast.tv_nsec);
if (clock_gettime(CLOCK_REALTIME, &tlast)) {
perror("Failed to get first time");
return 1;
}
while (numdone < NUMDIF) {
numcalls++;
if (clock_gettime(CLOCK_REALTIME, &tthis)) {
perror("Failed to get a later time");
return 1;
}
timedif[numdone] = BILLION*(tthis.tv_sec - tlast.tv_sec) +
tthis.tv_nsec - tlast.tv_nsec;
if (timedif[numdone] != 0) {
numdone++;
tlast = tthis;
}
}
printf("Found %d differences in CLOCK_REALTIME:\n", NUMDIF);
printf("%d calls to CLOCK_REALTIME were required\n", numcalls);
for (i = 0; i < NUMDIF; i++) {
printf("%2d: %10ld nanoseconds\n", i, timedif[i]);
sum += timedif[i];
}
printf("The average nonzero difference is %f\n", sum/(double)NUMDIF);
return 0;
}
Sleep Functions

• A process that voluntarialy 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
elapsed or until the calling thread catches a signal
#include <unisd.h>

unsigned sleep (unsigned seconds);

• The sleep function returns 0 if the requested time has


elapsed or the amount of unslept time if interrupted

BIL 244 – System Programming


Sleep Functions

• The nanosleep function causes the calling thread


to suspend execution until the time interval
specified by rqtp has elapsed or until the thread
recieves a signal.
• If nanosleep is interrupted by a signal rmtp is
not NULL, the location pointed by rmtp contains
the time remaining, allowing the function to be
restarted
#include <time.h>

int nanosleep (const struct timespec *rqtp,


struct timespect *rmtp);

BIL 244 – System Programming


Realtime Signals

• This portion of the chapter is left to the students

BIL 244 – System Programming


Project I

• DERSDE ANLATILACAK

BIL 244 – System Programming

S-ar putea să vă placă și