返回命令列表

Linux command

nanosleep 命令

文本

Uses pipes, overwrite, or deletion. Check paths and options first.

命令示例

Sleep for 1 second (C code)

struct timespec ts = {1, 0}; nanosleep(&ts, NULL);

Sleep for 500 milliseconds

struct timespec ts = {0, 500000000L}; nanosleep(&ts, NULL);

Sleep with remaining time

nanosleep(&req, &rem);

说明

nanosleep suspends execution of the calling thread for the specified duration. Unlike sleep(), it provides nanosecond precision and handles interruption gracefully. The function returns immediately if interrupted by a signal, storing remaining time in rem.

参数

req
Requested sleep time (seconds and nanoseconds).
rem
Remaining time if interrupted (can be NULL).

FAQ

What is the nanosleep command used for?

nanosleep suspends execution of the calling thread for the specified duration. Unlike sleep(), it provides nanosecond precision and handles interruption gracefully. The function returns immediately if interrupted by a signal, storing remaining time in rem.

How do I run a basic nanosleep example?

Run `struct timespec ts = {1, 0}; nanosleep(&ts, NULL);` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does req do in nanosleep?

Requested sleep time (seconds and nanoseconds).