虚拟时间已过
我有以下代码,它会引发错误
虚拟时间已过期。
程序不应该无限循环运行吗?
#define KTHREAD_VTALRM_SEC 0
#define KTHREAD_VTALRM_USEC 100000
#include <stdio.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sched.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <time.h>
int main()
{
struct itimerval timeslice;
timeslice.it_interval.tv_sec = KTHREAD_VTALRM_SEC;
timeslice.it_interval.tv_usec = KTHREAD_VTALRM_USEC;
timeslice.it_value.tv_sec = KTHREAD_VTALRM_SEC;
timeslice.it_value.tv_usec = KTHREAD_VTALRM_USEC;
setitimer(ITIMER_VIRTUAL,×lice,NULL);
while(1)
{
;
}
}
I have the following code, which throws the error
Virtual time expired.
Shouldn't the program run in an infinite loop?
#define KTHREAD_VTALRM_SEC 0
#define KTHREAD_VTALRM_USEC 100000
#include <stdio.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sched.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <time.h>
int main()
{
struct itimerval timeslice;
timeslice.it_interval.tv_sec = KTHREAD_VTALRM_SEC;
timeslice.it_interval.tv_usec = KTHREAD_VTALRM_USEC;
timeslice.it_value.tv_sec = KTHREAD_VTALRM_SEC;
timeslice.it_value.tv_usec = KTHREAD_VTALRM_USEC;
setitimer(ITIMER_VIRTUAL,×lice,NULL);
while(1)
{
;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当计时器到期时,它将发送一个
SIGVTALRM
信号,但您不会处理该信号。请参阅
setitimer()
和signal()
的手册页。When the timer expires it will deliver a
SIGVTALRM
signal, which you are not handling.See the man pages for
setitimer()
andsignal()
.您没有设置信号处理程序。
You did not set a signal handler.
VTALRM 信号的默认处理程序是“exit”(参考:http ://manpages.ubuntu.com/manpages//precise/en/man1/kill.1.html。)
因此,如果您想用自己的行为替换默认行为,则必须定义一个新的信号处理程序并注册它:
PS:如果您使用
signal
方法,请确保它在您的系统上得到了很好的实现(参考: http://manpages.ubuntu.com/manpages//精确/en/man2/signal.2.html)The default handler for VTALRM signal is "exit" (ref: http://manpages.ubuntu.com/manpages//precise/en/man1/kill.1.html.)
So if you want to replace the default behavior with yours, you have to define a new signal handler and register it:
PS: If you utilize
signal
method, sure that it is well implemented on your system (ref: http://manpages.ubuntu.com/manpages//precise/en/man2/signal.2.html)