美文网首页
C语言定时器

C语言定时器

作者: louyang | 来源:发表于2017-12-08 17:59 被阅读14次
image.png
#include <signal.h> //struct sigaction,sigemptyset()
#include <unistd.h> //alarm()
#include <stdio.h>  //printf()
#include <stdlib.h> //exit(),system()

volatile int stop = 0;

void sigalrm_handler(int sig)
{
    stop = 1;
}

int main()
{
    struct sigaction sa;
    int num = 0;

    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = sigalrm_handler;
    sigaction(SIGALRM, &sa, NULL);

    alarm(60);  /* Request SIGALRM in 60 seconds */

    while (!stop) {
        system("ls -F"); // do something here
        num++;
    }

    printf("%d\n", num);
    exit(0);
}

相关文章

网友评论

      本文标题:C语言定时器

      本文链接:https://www.haomeiwen.com/subject/aefyixtx.html