---
name: "nanosleep"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/clock_nanosleep"
---

# nanosleep(2)

high resolution sleep

Part of [clock\_nanosleep(2)](/clock_nanosleep/2.md).

## NAME

**nanosleep** – high resolution sleep

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<time.h\>**

*int*\

**clock\_nanosleep**(*clockid\_t clock\_id*, *int flags*, *const struct timespec \*rqtp*, *struct timespec \*rmtp*);

*int*\

**nanosleep**(*const struct timespec \*rqtp*, *struct timespec \*rmtp*);

## DESCRIPTION

If the TIMER\_ABSTIME flag is not set in the *flags* argument, then **clock\_nanosleep**() suspends execution of the calling thread until either the time interval specified by the *rqtp* argument has elapsed, or a signal is delivered to the calling process and its action is to invoke a signal-catching function or to terminate the process. The clock used to measure the time is specified by the *clock\_id* argument.

If the TIMER\_ABSTIME flag is set in the *flags* argument, then **clock\_nanosleep**() suspends execution of the calling thread until either the value of the clock specified by the *clock\_id* argument reaches the absolute time specified by the *rqtp* argument, or a signal is delivered to the calling process and its action is to invoke a signal-catching function or to terminate the process. If, at the time of the call, the time value specified by *rqtp* is less than or equal to the time value of the specified clock, then **clock\_nanosleep**() returns immediately and the calling thread is not suspended. An unmasked signal will terminate the sleep early, regardless of the SA\_RESTART value on the interrupting signal. The *rqtp* and *rmtp* arguments can point to the same object.

The following *clock\_id* values are supported:

-

  CLOCK\_MONOTONIC

-

  CLOCK\_MONOTONIC\_FAST

-

  CLOCK\_MONOTONIC\_PRECISE

-

  CLOCK\_REALTIME

-

  CLOCK\_REALTIME\_FAST

-

  CLOCK\_REALTIME\_PRECISE

-

  CLOCK\_SECOND

-

  CLOCK\_TAI

-

  CLOCK\_UPTIME

-

  CLOCK\_UPTIME\_FAST

-

  CLOCK\_UPTIME\_PRECISE

The suspension time may be longer than requested due to the scheduling of other activity by the system. The clocks with the \_FAST suffix and the CLOCK\_SECOND are subject to the allowed time interval deviation specified by the *kern.timecounter.alloweddeviation* [sysctl(8)](/sysctl/8.md) variable. The clocks with the \_PRECISE suffix are always as precise as possible. The CLOCK\_MONOTONIC, CLOCK\_REALTIME and CLOCK\_UPTIME are precise by default. Setting the *kern.timecounter.nanosleep\_precise* [sysctl(8)](/sysctl/8.md) to a false value would make those clocks to behave like the \_FAST clocks.

The **nanosleep**() function behaves like **clock\_nanosleep**() with a *clock\_id* argument of CLOCK\_REALTIME and without the TIMER\_ABSTIME flag in the *flags* argument.

## RETURN VALUES

These functions return zero when the requested time has elapsed.

If these functions return for any other reason, then **clock\_nanosleep**() will directly return the error number, and **nanosleep**() will return -1 with the global variable *errno* set to indicate the error. If a relative sleep is interrupted by a signal and *rmtp* is non-NULL, the timespec structure it references is updated to contain the unslept amount (the request time minus the time actually slept).

## ERRORS

These functions can fail with the following errors.

- \[EFAULT\]

  Either *rqtp* or *rmtp* points to memory that is not a valid part of the process address space.

- \[EINTR\]

  The function was interrupted by the delivery of a signal.

- \[EINVAL\]

  The *rqtp* argument specified a nanosecond value less than zero or greater than or equal to 1000 million.

- \[EINVAL\]

  The *flags* argument contained an invalid flag.

- \[EINVAL\]

  The *clock\_id* argument was CLOCK\_THREAD\_CPUTIME\_ID or an unrecognized value.

- \[ENOTSUP\]

  The *clock\_id* argument was valid but not supported by this implementation of **clock\_nanosleep**().

## SEE ALSO

[clock\_gettime(2)](/clock_gettime/2.md), [sigaction(2)](/sigaction/2.md), [sleep(3)](/sleep/3.md)

## STANDARDS

These functions conform to IEEE Std 1003.1-2008 (“POSIX.1”).

## HISTORY

The predecessor of this system call, **sleep**(), appeared in Version 3 AT&T UNIX, but was removed when [alarm(3)](/alarm/3.md) was introduced into Version 7 AT&T UNIX. The **nanosleep**() system call has been available since NetBSD 1.3 and was ported to OpenBSD 2.1 and FreeBSD 3.0. The **clock\_nanosleep**() system call has been available since FreeBSD 11.1.

In FreeBSD 15.0 the default behavior of **clock\_nanosleep**() with CLOCK\_MONOTONIC, CLOCK\_REALTIME, CLOCK\_UPTIME clocks and **nanosleep**() has been switched to use precise clock.
