usleep()--Suspend Processing for Interval of Time


  Syntax
 #include <unistd.h>

 int usleep( useconds_t useconds );  

  Service Program Name: QP0SSRV1

  Default Public Authority: *USE

  Threadsafe: Yes

The usleep() function suspends a thread for the number of microseconds specified by the of useconds parameter. (Because of processor delays, the thread can be suspended slightly longer than this specified time.) The thread does not resume until either the specified interval of time passes or a signal is delivered whose action is to call a signal-catching function, to end the request, or to terminate the process.

The usleep() function uses the process's real-time interval timer to indicate when the thread should be resumed.

There is one real-time interval timer for each process. The usleep() function will not interfere with a previous setting of this timer.

If an incoming unblocked signal has an action of end the request or terminate the process, usleep() never returns to the caller. If an incoming signal is handled by a signal-catching function, usleep() returns after the signal-catching function returns.


Authorities and Locks

None.


Parameters

useconds
(Input) The number of microseconds for which the thread is to be suspended.

Return Value

0 The thread slept for the full time specified.
-1 usleep() was not successful. The errno variable is set to indicate the error.


Error Conditions

If usleep() is not successful, errno usually indicates the following error. Under some conditions, errno could indicate an error other than that listed here.

[EINVAL]
The value specified for the argument is not correct.

A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.

An argument value is not valid, out of range, or NULL.


[EINTR]

Interrupted function call.

A signal was received and handled by a signal-catching function that returned.


Usage Notes

The usleep() function is included for its historical usage. The setitimer() function is preferred over this function.


Related Information


Example

The following example uses the usleep() function to suspend processing for a specified time.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <unistd.h>
#include <stdio.h>
#include <time.h>

void timestamp( char *str ) {

    time_t t;

    time( &t );
    printf( "%s the time is %s\nquot;, str, ctime(&t) );
}

int main( int argc, char *argv[] ) {

    int result = 0;

    timestamp( quot;before usleep()quot; );
    result = usleep( 999999 );
    timestamp( quot;after usleep()quot; );

    printf( quot;usleep() returned %d\nquot;, result );

    return( result );
}

Output:

    before usleep() the time is Sun Jun 15 17:25:17 1995
    after usleep() the time is Sun Jun 15 17:25:18 1995
    usleep() returned 0


API introduced: V4R2

[ Back to top | UNIX-Type APIs | APIs by category ]