linux sleep 信号,关于linux信号对sleep的影响与相关对策

关于linux信号对sleep的影响

首先想说用sleep来定时是不靠谱的

简单记录一下

当前有个功能,底层传递一个信号上来,然后linux应用会调用相关的中断处理函数。但是每次触发了信号,主程序里面sleep函数就直接退出了。主程序就乱了。

那么首先想的是为什么sleep会退出?

NAME top

sleep - sleep for a specified number of seconds

SYNOPSIS top

#include unsigned int sleep(unsigned int seconds);

DESCRIPTION top

sleep() causes the calling thread to sleep either until the number of

real-time seconds specified in seconds have elapsed or until a signal arrives which is not ignored.

RETURN VALUE top

Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler.

ATTRIBUTES top

返回值:

Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler.

这里说明了sleep的确是有这个机制的,只要来信号就会退出,并返回剩余值。

那么方法就有了:

只要把剩余的时间在sleep过去就好了

但是进过测试发现,这个方法不是很靠谱,应为如果我sleep了1.1s,就直接退出,那么就等于少延时了0.9s。

那就用usleep(10000000),延时10s来做。

but 看了下发现并不能正常使用,因为usleep和sleep不同,他不会返回剩余时间。0正确-1错误。

这样的话,10s就直接过去了。

最终那么就时间切片 1000次每次usleep(10000),每次就算是退出了我也不管他,退出一次也就是10ms。

最终没有解决这个问题,只是绕过了他。

如果有更好的方法请告知多谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值