---
name: "clock_nanosleep"
section: "2"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/clock_nanosleep"
---

# clock\_nanosleep(2)

high-resolution sleep with specifiable clock

## NAME

clock\_nanosleep - high-resolution sleep with specifiable clock

## LIBRARY

Standard C library (*libc*, *-lc*), since glibc 2.17

Before glibc 2.17, Real-time library (*librt*, *-lrt*)

## SYNOPSIS

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

```

```

**int clock\_nanosleep(clockid\_t** *clockid***, int** *flags***,**                     **const struct timespec \****t***,**                     **struct timespec \*\_Nullable** *remain***);**

Feature Test Macro Requirements for glibc (see [**feature\_test\_macros**(7)](/feature_test_macros/7.md)):

**clock\_nanosleep**():

```
    _POSIX_C_SOURCE >= 200112L
```

## DESCRIPTION

Like [**nanosleep**(2)](/nanosleep/2.md), **clock\_nanosleep**() allows the calling thread to sleep for an interval specified with nanosecond precision. It differs in allowing the caller to select the clock against which the sleep interval is to be measured, and in allowing the sleep interval to be specified as either an absolute or a relative value.

The time values passed to and returned by this call are specified using [**timespec**(3)](/timespec/3.md) structures.

The *clockid* argument specifies the clock against which the sleep interval is to be measured. This argument can have one of the following values:

- **CLOCK\_REALTIME**

  A settable system-wide real-time clock.

- **CLOCK\_TAI** (since Linux 3.10)

  A system-wide clock derived from wall-clock time but counting leap seconds.

- **CLOCK\_MONOTONIC**

  A nonsettable, monotonically increasing clock that measures time since some unspecified point in the past that does not change after system startup.

- **CLOCK\_BOOTTIME** (since Linux 2.6.39)

  Identical to **CLOCK\_MONOTONIC**, except that it also includes any time that the system is suspended.

- **CLOCK\_PROCESS\_CPUTIME\_ID**

  A settable per-process clock that measures CPU time consumed by all threads in the process.

See [**clock\_getres**(2)](/clock_getres/2.md) for further details on these clocks. In addition, the CPU clock IDs returned by [**clock\_getcpuclockid**(3)](/clock_getcpuclockid/3.md) and [**pthread\_getcpuclockid**(3)](/pthread_getcpuclockid/3.md) can also be passed in *clockid*.

If *flags* is 0, then the value specified in *t* is interpreted as an interval relative to the current value of the clock specified by *clockid*.

If *flags* is **TIMER\_ABSTIME**, then *t* is interpreted as an absolute time as measured by the clock, *clockid*. If *t* is less than or equal to the current value of the clock, then **clock\_nanosleep**() returns immediately without suspending the calling thread.

**clock\_nanosleep**() suspends the execution of the calling thread until either at least the time specified by *t* has elapsed, or a signal is delivered that causes a signal handler to be called or that terminates the process.

If the call is interrupted by a signal handler, **clock\_nanosleep**() fails with the error **EINTR**. In addition, if *remain* is not NULL, and *flags* was not **TIMER\_ABSTIME**, it returns the remaining unslept time in *remain*. This value can then be used to call **clock\_nanosleep**() again and complete a (relative) sleep.

## RETURN VALUE

On successfully sleeping for the requested interval, **clock\_nanosleep**() returns 0. If the call is interrupted by a signal handler or encounters an error, then it returns one of the positive error number listed in ERRORS.

## ERRORS

- **EFAULT**

  *t* or *remain* specified an invalid address.

- **EINTR**

  The sleep was interrupted by a signal handler; see [**signal**(7)](/signal/7.md).

- **EINVAL**

  The value in the *tv\_nsec* field was not in the range \[0, 999999999\] or *tv\_sec* was negative.

- **EINVAL**

  *clockid* was invalid. (**CLOCK\_THREAD\_CPUTIME\_ID** is not a permitted value for *clockid*.)

- **ENOTSUP**

  The kernel does not support sleeping against this *clockid*.

## STANDARDS

POSIX.1-2024.

## HISTORY

POSIX.1-2001. Linux 2.6, glibc 2.1.

## NOTES

If the interval specified in *t* is not an exact multiple of the granularity underlying clock (see [**time**(7)](/time/7.md)), then the interval will be rounded up to the next multiple. Furthermore, after the sleep completes, there may still be a delay before the CPU becomes free to once again execute the calling thread.

Using an absolute timer is useful for preventing timer drift problems of the type described in [**nanosleep**(2)](/nanosleep/2.md). (Such problems are exacerbated in programs that try to restart a relative sleep that is repeatedly interrupted by signals.) To perform a relative sleep that avoids these problems, call [**clock\_gettime**(2)](/clock_gettime/2.md) for the desired clock, add the desired interval to the returned time value, and then call **clock\_nanosleep**() with the **TIMER\_ABSTIME** flag.

**clock\_nanosleep**() is never restarted after being interrupted by a signal handler, regardless of the use of the [**sigaction**(2)](/sigaction/2.md) **SA\_RESTART** flag.

The *remain* argument is unused, and unnecessary, when *flags* is **TIMER\_ABSTIME**. (An absolute sleep can be restarted using the same *t* argument.)

POSIX.1 specifies that **clock\_nanosleep**() has no effect on signals dispositions or the signal mask.

POSIX.1 specifies that after changing the value of the **CLOCK\_REALTIME** clock via [**clock\_settime**(2)](/clock_settime/2.md), the new clock value shall be used to determine the time at which a thread blocked on an absolute **clock\_nanosleep**() will wake up; if the new clock value falls past the end of the sleep interval, then the **clock\_nanosleep**() call will return immediately.

POSIX.1 specifies that changing the value of the **CLOCK\_REALTIME** clock via [**clock\_settime**(2)](/clock_settime/2.md) shall have no effect on a thread that is blocked on a relative **clock\_nanosleep**().

## SEE ALSO

[**clock\_getres**(2)](/clock_getres/2.md), [**nanosleep**(2)](/nanosleep/2.md), [**restart\_syscall**(2)](/restart_syscall/2.md), [**timer\_create**(2)](/timer_create/2.md), [**sleep**(3)](/sleep/3.md), [**timespec**(3)](/timespec/3.md), [**usleep**(3)](/usleep/3.md), [**time**(7)](/time/7.md)
