如何在C使用了nanosleep()?什么是`tim.tv_sec`和`tim.tv_nsec`? [英] How to use nanosleep() in C? What are `tim.tv_sec` and `tim.tv_nsec`?

查看:539
本文介绍了如何在C使用了nanosleep()?什么是`tim.tv_sec`和`tim.tv_nsec`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用 tim.tv_sec tim.tv_nsec 在下面?

我怎么可以睡执行为 500000 微秒?

 的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;诠释的main()
{
   结构的timespec恬,TIM2;
   tim.tv_sec = 1;
   tim.tv_nsec = 500;   如果(了nanosleep(安培;恬,&安培; TIM2)℃下)
   {
      的printf(纳米睡眠系统调用失败\\ n);
      返回-1;
   }   的printf(纳米睡眠全成\\ n);   返回0;
}


解决方案

半秒为5亿纳秒,所以你的code应改为:

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

就目前情况来看,您code为睡了1.0000005s(1S + 500ns的)。

What is the use of tim.tv_sec and tim.tv_nsec in the following?

How can I sleep execution for 500000 microseconds?

#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;
}

解决方案

Half a second is 500,000,000 nanoseconds, so your code should read:

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

As things stand, you code is sleeping for 1.0000005s (1s + 500ns).

这篇关于如何在C使用了nanosleep()?什么是`tim.tv_sec`和`tim.tv_nsec`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆