gpt4 book ai didi

c - 如何在 C 中使用 nanosleep()?什么是 `tim.tv_sec` 和 `tim.tv_nsec` ?

转载 作者:太空狗 更新时间:2023-10-29 16:15:57 27 4
gpt4 key购买 nike

下面的tim.tv_sectim.tv_nsec有什么用?

如何让执行休眠 500000 微秒?

#include <stdio.h>
#include <time.h>

int main()
{
struct timespec tim, tim2;
tim.tv_sec = 1;
tim.tv_nsec = 500;

if(nanosleep(&tim , &tim2) < 0 )
{
printf("Nano sleep system call failed \n");
return -1;
}

printf("Nano sleep successfull \n");

return 0;
}

最佳答案

半秒是 500,000,000 纳秒,因此您的代码应为:

tim.tv_sec  = 0;
tim.tv_nsec = 500000000L;

就目前而言,您的代码休眠了 1.0000005 秒(1 秒 + 500 纳秒)。

关于c - 如何在 C 中使用 nanosleep()?什么是 `tim.tv_sec` 和 `tim.tv_nsec` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7684359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com