← 返回命令列表

Linux command

nanosleep 命令

文本

涉及管道、覆盖或删除,执行前请先确认路径和参数。

常用示例

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).