Introduction to the nanosleep function of a scheduling thread suspend

Source: Internet
Author: User

The function code is as follows:

/*

* Execution of the current suspend thread

*/

Int threadsleep (time_t sec, long nsec)

{

Struct timespec req, REM;

// Set the length of time for suspend

Req. TV _sec = (time_t) sec;

Req. TV _nsec = (long) nsec;

// Note: If the thread is suspend, The nanosleep function will not return, that is, the thread enters the suspend state.

// If 0 is returned, it indicates that the time specified by req has passed. Otherwise,-1 is returned.

While (nanosleep (& req, & rem )! = 0)

{

// Return but not 0 at this time, indicating an error or signal interruption

// If the signal is interrupted, errno is set to eintr, and the output parameter REM stores the remaining time. In this case, the remaining time is set to req to call nanosleep cyclically.

If (errno = eintr)

{

Req = REM;

}

// If other errors occur,-1 is returned.

Else

{

Return-1;

}

} // End of while

Return 0;

}

The usage of the nanosleep function is described as follows:

Name
Nanosleep-high resolution sleep (Realtime)
Synopsis
# Include <time. h>
Int nanosleep (const struct timespec * rqtp, struct timespec * rmtp );
Description
The nanosleep () function shall cause the current thread to be suincluded from execution
Until either the time interval
Specified by the rqtp argument has elapsed
OrA signal is delivered to the calling thread, And its action is to invoke
Signal-catching function or to terminate the process. The suspension time may be longer than requested because the argu-
Ment value is rounded up to an integer multiple of the sleep resolution or because of the scheduling of other activity
The system. But, should t for the case of being interrupted by a signal, the suspension time shall not be less than
Time specified by rqtp, as measured by the system clock clock_realtime.

The use of the nanosleep () function has no effect on the action or blockage of any signal.

Return Value
If the nanosleep () function returns because the requested time has elapsed, its return value shall be zero.

If the nanosleep () function returns because it has been interrupted by a signal, it shall return a value of-1 and set
Errno to indicate the interruption.
If the rmtp argument is non-null, The timespec structure referenced by it is updated
To contain the amount of time remaining in the interval (the requested time minus the time actually slept). If the rmtp
Argument is null, the remaining time is not returned.

If nanosleep () fails, it shall return a value of-1 and set errno to indicate the error.

Otherwise, no result is returned, indicating that the thread enters the suspend state.

Errors
The nanosleep () function shall fail if:
Eintr the nanosleep () function was interrupted by a signal.
Einval the rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million.
The following sections are informative.

Rationale
It is common to suspend execution of a process for an interval in order to poll the status of a non-Interrupting func-
Tion. A large number of actual needs can be met with a simple extension to sleep () that provides finer resolution.

In the POSIX.1-1990 standard and svr4, it is possible to implement such a routine, but the frequency of wakeup is limited
By the resolution of the alarm () and sleep () functions. In 4.3 BSD, it is possible to write such a routine using no
Static storage and reserving no system facilities. Although it is possible to write a function with similar functionality
To sleep () using the remainder of the timer _ * () functions, such a function requires the use of signals and the reserva-
Tion of some signal number. This volume of IEEE Std 1003.1-2001 requires that nanosleep () Be non-intrusive of the signals
Function.

The nanosleep () function shall return a value of 0 on success and-1 on failure or if interrupted. This latter case is
Different from sleep (). This was done because the remaining time is returned via an argument structure pointer, rmtp,
Instead of as the return value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.