9.2 Sleep Functions

Team-FLY

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.

  SYNOPSIS  #include <unistd.h>    #unsigned sleep(unsigned seconds);  POSIX  

The sleep function returns 0 if the requested time has elapsed or the amount of unslept time if interrupted . The sleep function interacts with SIGALRM , so avoid using them concurrently in the same process.

Example 9.10 beeper .c

The following program beeps every n seconds, where n is passed as a command-line argument.

 #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) {    int sleeptime;    if (argc != 2) {       fprintf(stderr, ";Usage:%s n\n", argv[0]);       return 1;    }    sleeptime = atoi(argv[1]);    fprintf(stderr, "Sleep time is %d\n", sleeptime);    for ( ; ; ) {       sleep(sleeptime);       printf(" 
 #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { int sleeptime; if (argc != 2) { fprintf(stderr, ";Usage:%s n\n", argv[0]); return 1; } sleeptime = atoi(argv[1]); fprintf(stderr, "Sleep time is %d\n", sleeptime); for ( ; ; ) { sleep(sleeptime); printf("\007"); fflush ( stdout ); } } 
7"); fflush(stdout); } }

The nanosleep function causes the calling thread to suspend execution until the time interval specified by rqtp has elapsed or until the thread receives a signal. If nanosleep is interrupted by a signal and rmtp is not NULL , the location pointed to by rmtp contains the time remaining, allowing nanosleep to be restarted. The system clock CLOCK_REALTIME determines the resolution of rqtp .

  SYNOPSIS  #include <time.h>   int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);  POSIX:TMR  

If successful, nanosleep returns 0. If unsuccessful , nanosleep returns “1 and sets errno . The following table lists the mandatory errors for nanosleep .

errno

cause

EINTR

nanosleep interrupted by a signal

EINVAL

rqtp specifies a nanosecond value that is not in [0, 10 9 )

The data structures used by nanosleep allow for nanosecond resolution, but the resolution of CLOCK_REALTIME is typically much larger, on the order of 10 ms. The nanosleep function is meant to replace usleep , which is now considered obsolete. The main advantage of nanosleep over usleep is that nanosleep , unlike sleep or usleep , does not affect the use of any signals, including SIGALRM .

Program 9.6 tests the resolution of the nanosleep function. It executes 100 calls to nanosleep with a sleep time of 1000 nanoseconds. If nanosleep had a true resolution of 1 ns, this would complete in 100 m sec. The program takes about one second to complete on a system with a 10 ms resolution.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net