Reputation: 15562
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)
{
;
}
}
Upvotes: 0
Views: 1857
Reputation: 17724
When the timer expires it will deliver a SIGVTALRM
signal, which you are not handling.
See the man pages for setitimer()
and signal()
.
Upvotes: 2
Reputation: 344
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:
void vtHandler(int sig)
{
// Do something here
}
int main()
{
// Register timer handler
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = timer_handler;
if (sigaction(SIGVTALRM, &sa, NULL) == -1)
//error handle
;
}
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)
Upvotes: 0