当前位置: 技术问答>linux和unix
sleep nanosleep select延时问题
来源: 互联网 发布时间:2016-12-24
本文导语: int my_uint_sleep (double sleep_time) { struct timespec tv; tv.tv_sec = (time_t) sleep_time; tv.tv_nsec = (long) ((sleep_time - tv.tv_sec) * 1e+9); while (1) { printf("tv.tv_sec = %d n",tv.tv_sec); printf("tv.tv_nsec = %d n...
int my_uint_sleep (double sleep_time)
{
struct timespec tv;
tv.tv_sec = (time_t) sleep_time;
tv.tv_nsec = (long) ((sleep_time - tv.tv_sec) * 1e+9);
while (1)
{
printf("tv.tv_sec = %d n",tv.tv_sec);
printf("tv.tv_nsec = %d n",tv.tv_nsec);
int rval = nanosleep (&tv, &tv);
printf("rval = %d ; nanosleep errno = %d n",rval,errno);
if (rval == 0)
return 0;
else if (errno == EINTR)
{
printf("Interruped n");
continue;
}
else
return rval;
}
return 0;
}
当调用: my_uint_sleep(5.1f); 的时候,errno 一直都是 516 (#define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */) 在程序的其他部分用到了定时器,所以nanosleep这个函数理论上会被中断而进入if (errno == EINTR)这个分支语句,实际情况确实返回 516 错误,请有碰到类似情况的朋友解答,谢谢!!!!!!
|
LZ ,我测试一切OK呢!
#include
#include
#include
#include
int my_uint_sleep (double sleep_time)
{
struct timespec tv;
tv.tv_sec = (time_t) sleep_time;
tv.tv_nsec = (long) ((sleep_time - tv.tv_sec) * 1e+9);
while (1)
{
printf("tv.tv_sec = %d n",tv.tv_sec);
printf("tv.tv_nsec = %d n",tv.tv_nsec);
int rval = nanosleep (&tv, &tv);
printf("rval = %d ; nanosleep errno = %d n",rval,errno);
if (rval == 0)
return 0;
else if (errno == EINTR)
{
printf("Interruped n");
continue;
}
else return rval;
}
return 0;
}
int main()
{
my_uint_sleep(6.1f);
}
|
运行输出结果:
./1
tv.tv_sec = 6
tv.tv_nsec = 99999904
rval = 0 ; nanosleep errno = 0