gpt4 book ai didi

c++ - 睡后杀

转载 作者:IT王子 更新时间:2023-10-29 00:59:20 26 4
gpt4 key购买 nike

在使用 kill(pid,SIGTERM) 后我无法让我的程序休眠()我能做什么?

我正在使用的代码:

kill(PID_of_Process_to_be_killed,SIGTERM);
sleep(5); --> this is not working
sleep(5); --> this is working

目前的解决方案是:

kill(PID_of_Process_to_be_killed,SIGTERM);
sleep(sleep(5));

但为什么 kill 后的第一个 sleep 返回 0 呢?

最佳答案

您的 sleep() 调用可能会因收到信号而提前终止。检查返回值。如果它是正数,您可能需要再次对该值进行 sleep()。来自 http://www.manpagez.com/man/3/Sleep/ :

If the sleep() function returns because the requested time has elapsed, the value returned will be zero. If the sleep() function returns due to the delivery of a signal, the value returned will be the unslept amount (the requested time minus the time actually slept) in seconds

关于c++ - 睡后杀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4319170/

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