[C++] 纯文本查看 复制代码
#include <sys/time.h>
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
[C++] 纯文本查看 复制代码
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
struct itimerval interval;
struct sigaction act;
void handler_sigvalrm (int signo)
{
//getitimer (ITIMER_VIRTUAL, &curr_value, NULL);
static i = 0;
if (!i) {
printf ("First, after 10s... \n");
getitimer (ITIMER_VIRTUAL, &curr_value);
}
else
printf ("3s after...\n");
i++;
if (i == 5)
exit (0);
}
int main()
{
act.sa_handler = handler_sigvalrm;
interval.it_interval.tv_sec = 3;
interval.it_interval.tv_usec = 0;
interval.it_value.tv_sec = 10;
interval.it_value.tv_usec = 0;
sigaction (SIGVTALRM, &act, NULL);
setitimer (ITIMER_VIRTUAL, &interval, NULL);
while (1);
return (0);
}