sleep, sleepd -- Suspend Execution for a Period of Time

SYNOPSIS

 #include <lclib.h>

 unsigned sleep(unsigned sec);
 double sleepd(double sec);
 
The synopsis for the POSIX implementation is
 #include <sys/types.h>
 #include <unistd.h>

 unsigned sleep(unsigned sec);
 double sleepd(double sec);
 

DESCRIPTION

sleep and sleepd are called to suspend execution of the program for a specified number of seconds or until a signal occurs. If the value of the sec argument is 0, the sleep or sleepd function immediately returns to its caller. The behavior of the sleep and sleepd functions depends on whether SIGALRM is defined as a signal managed by SAS/C or an OpenEdition signal. If SIGALRM is managed by OpenEdition, the sleep function is implemented by OpenEdition, and the sleepd function is not implemented. In this case, note that the occurrence of a signal managed by SAS/C does not cause sleep to terminate. If SIGALRM is managed by SAS/C, a value of sec that is more than a day (86,400 seconds) is treated as a day.

You may use sleep or sleepd in association with the alarm or alarmd function. If either of these functions is called before completion of a time interval set with an alarm function, execution resumes when the alarm interval ends or the sleep interval ends, whichever occurs first.

The SAS/C implementation of sleep and sleepd always unblocks the SIGALRM signal to enable them to detect the completion of the time interval. However, no other signals are unblocked, and the signal mask is completely restored before these functions return. If a signal is raised and then blocked, program execution does not resume. If an unblocked signal occurs, the handler for the signal is executed before these functions return to the program that called it.

sleepd performs the same actions as sleep but permits the amount of time to be specified with greater accuracy. The accuracy of timing depends on the operating system and CPU model.

RETURN VALUE

If the sleep period ends because the specified time has elapsed, the sleep and sleepd functions return 0. If a signal occurs that ends the sleep period, the functions return the amount of time left in the sleep interval; sleep rounds up to an integer of seconds.

CAUTION

Under a non-XA, non-ESA version of CMS, you must use the CP command SET TIMER REAL for proper functioning of sleep and sleepd. If SET TIMER REAL is not in effect, a diagnostic message is produced and these functions return immediately.

PORTABILITY

sleepd is not portable.

IMPLEMENTATION

sleep and sleepd are implemented using idle waiting; that is, no CPU time is consumed (other than set-up time) during the sleep interval.

EXAMPLE

This example calls the routine acquire to get exclusive control of a file. It tries to obtain control four times a second until it is successful.
  #include <lclib.h>

     /* Return 1 if successful,and 0 if unsuccessful. */
  int acquire();

  while (!acquire())
     sleepd(0.25);
  .
  .
  .
 

RELATED FUNCTIONS

alarm, alarmd, select, sigsuspend

SEE ALSO


Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.