一个进程使用多个计时器(不链接到 rt)

发布于 2024-10-15 19:24:31 字数 2490 浏览 5 评论 0原文

有没有办法将多个计时器注册到单个进程?我尝试过以下代码,但没有成功。 (使用“gcc -lrt”来编译它......)。程序没有输出任何内容,至少应该打印“test”。是否可能是由于依赖于链接到 rt?

#define TT_SIGUSR1 (SIGRTMAX)
#define TT_SIGUSR2 (SIGRTMAX - 1)
#define TIME_INTERVAL_1 1
#define TIME_INTERVAL_2 2

#include <signal.h>
#include <time.h>

#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>


timer_t create_timer(int signo) {
    timer_t timerid;
    struct sigevent se;
    se.sigev_signo = signo;
    if (timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
        fprintf(stderr, "Failed to create timer\n");
        exit(-1);
    }
    return timerid;
}

void set_timer(timer_t timerid, int seconds) {
    struct itimerspec timervals;
    timervals.it_value.tv_sec = seconds;
    timervals.it_value.tv_nsec = 0;
    timervals.it_interval.tv_sec = seconds;
    timervals.it_interval.tv_nsec = 0;

    if (timer_settime(timerid, 0, &timervals, NULL) == -1) {
        fprintf(stderr, "Failed to start timer\n");
        exit(-1);
    }
    return;
}

void install_sighandler2(int signo, void(*handler)(int)) {
    struct sigaction sigact;

    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = SA_SIGINFO;
    //register the Signal Handler
    sigact.sa_sigaction = handler;

    // Set up sigaction to catch signal first timer
    if (sigaction(signo, &sigact, NULL) == -1)

    {
        printf("sigaction failed");
        return -1;
    }
}

void install_sighandler(int signo, void(*handler)(int)) {
    sigset_t set;
    struct sigaction act;

    /* Setup the handler */
    act.sa_handler = handler;
    act.sa_flags = SA_RESTART;
    sigaction(signo, &act, 0);

    /* Unblock the signal */
    sigemptyset(&set);
    sigaddset(&set, signo);
    sigprocmask(SIG_UNBLOCK, &set, NULL);

    return;
}


void signal_handler(int signo) {
    printf("receiving sig %d", signo);
}

int main()
{
    printf("test");
    timer_t timer1 = create_timer(TT_SIGUSR1);
    timer_t timer2 = create_timer(TT_SIGUSR2);

    set_timer(timer1, TIME_INTERVAL_1);
    set_timer(timer2, TIME_INTERVAL_2);

    install_sighandler2(TT_SIGUSR1, signal_handler);
    install_sighandler(TT_SIGUSR2, signal_handler);

    while (1)
        ;
    return 0;
}

is there any way to register multiple timer to a single process? I have tried following code, yet without success. (Use "gcc -lrt" to compile it...). Program output nothing, which should atleast print "test". Is it possibly due to the dependence to linking to rt?

#define TT_SIGUSR1 (SIGRTMAX)
#define TT_SIGUSR2 (SIGRTMAX - 1)
#define TIME_INTERVAL_1 1
#define TIME_INTERVAL_2 2

#include <signal.h>
#include <time.h>

#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>


timer_t create_timer(int signo) {
    timer_t timerid;
    struct sigevent se;
    se.sigev_signo = signo;
    if (timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
        fprintf(stderr, "Failed to create timer\n");
        exit(-1);
    }
    return timerid;
}

void set_timer(timer_t timerid, int seconds) {
    struct itimerspec timervals;
    timervals.it_value.tv_sec = seconds;
    timervals.it_value.tv_nsec = 0;
    timervals.it_interval.tv_sec = seconds;
    timervals.it_interval.tv_nsec = 0;

    if (timer_settime(timerid, 0, &timervals, NULL) == -1) {
        fprintf(stderr, "Failed to start timer\n");
        exit(-1);
    }
    return;
}

void install_sighandler2(int signo, void(*handler)(int)) {
    struct sigaction sigact;

    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = SA_SIGINFO;
    //register the Signal Handler
    sigact.sa_sigaction = handler;

    // Set up sigaction to catch signal first timer
    if (sigaction(signo, &sigact, NULL) == -1)

    {
        printf("sigaction failed");
        return -1;
    }
}

void install_sighandler(int signo, void(*handler)(int)) {
    sigset_t set;
    struct sigaction act;

    /* Setup the handler */
    act.sa_handler = handler;
    act.sa_flags = SA_RESTART;
    sigaction(signo, &act, 0);

    /* Unblock the signal */
    sigemptyset(&set);
    sigaddset(&set, signo);
    sigprocmask(SIG_UNBLOCK, &set, NULL);

    return;
}


void signal_handler(int signo) {
    printf("receiving sig %d", signo);
}

int main()
{
    printf("test");
    timer_t timer1 = create_timer(TT_SIGUSR1);
    timer_t timer2 = create_timer(TT_SIGUSR2);

    set_timer(timer1, TIME_INTERVAL_1);
    set_timer(timer2, TIME_INTERVAL_2);

    install_sighandler2(TT_SIGUSR1, signal_handler);
    install_sighandler(TT_SIGUSR2, signal_handler);

    while (1)
        ;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2024-10-22 19:24:31
  • create_timer 中缺少:se.sigev_notify=SIGEV_SIGNAL;
  • 两个 install_sighandler 方法。 Install_sighandler2 有一个有关指针类型不匹配的编译器警告。
  • while (1) 会占用你的处理器,休眠。

#define TT_SIGUSR1 (SIGUSR1)
#define TT_SIGUSR2 (SIGUSR2)
#define TIME_INTERVAL_1 1
#define TIME_INTERVAL_2 2

#include <signal.h>
#include <time.h>

#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 <stdlib.h>

timer_t create_timer(int signo) {
    timer_t timerid;
    struct sigevent se;
    se.sigev_notify=SIGEV_SIGNAL;
    se.sigev_signo = signo;
    if (timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
        perror("Failed to create timer");
        exit(-1);
    }
    return timerid;
}

void set_timer(timer_t timerid, int seconds) {
    struct itimerspec timervals;
    timervals.it_value.tv_sec = seconds;
    timervals.it_value.tv_nsec = 0;
    timervals.it_interval.tv_sec = seconds;
    timervals.it_interval.tv_nsec = 0;

    if (timer_settime(timerid, 0, &timervals, NULL) == -1) {
        perror("Failed to start timer");
        exit(-1);
    }
    return;
}

void install_sighandler(int signo, void(*handler)(int)) {
    sigset_t set;
    struct sigaction act;

    /* Setup the handler */
    act.sa_handler = handler;
    act.sa_flags = SA_RESTART;
    sigaction(signo, &act, 0);

    /* Unblock the signal */
    sigemptyset(&set);
    sigaddset(&set, signo);
    sigprocmask(SIG_UNBLOCK, &set, NULL);

    return;
}


void signal_handler(int signo) {
    printf("receiving sig %d\n", signo);
}

int main()
{
    printf("test\n");
    timer_t timer1 = create_timer(TT_SIGUSR1);
    timer_t timer2 = create_timer(TT_SIGUSR2);

    install_sighandler(TT_SIGUSR1, signal_handler);
    install_sighandler(TT_SIGUSR2, signal_handler);

    set_timer(timer1, TIME_INTERVAL_1);
    set_timer(timer2, TIME_INTERVAL_2);

    while (1) sleep(1);
    return 0;
}

  • missing in create_timer: se.sigev_notify=SIGEV_SIGNAL;
  • two install_sighandler methods. Install_sighandler2 has a compler warning about mismatching pointer types.
  • while (1) will hog your processor, sleep.

#define TT_SIGUSR1 (SIGUSR1)
#define TT_SIGUSR2 (SIGUSR2)
#define TIME_INTERVAL_1 1
#define TIME_INTERVAL_2 2

#include <signal.h>
#include <time.h>

#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 <stdlib.h>

timer_t create_timer(int signo) {
    timer_t timerid;
    struct sigevent se;
    se.sigev_notify=SIGEV_SIGNAL;
    se.sigev_signo = signo;
    if (timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
        perror("Failed to create timer");
        exit(-1);
    }
    return timerid;
}

void set_timer(timer_t timerid, int seconds) {
    struct itimerspec timervals;
    timervals.it_value.tv_sec = seconds;
    timervals.it_value.tv_nsec = 0;
    timervals.it_interval.tv_sec = seconds;
    timervals.it_interval.tv_nsec = 0;

    if (timer_settime(timerid, 0, &timervals, NULL) == -1) {
        perror("Failed to start timer");
        exit(-1);
    }
    return;
}

void install_sighandler(int signo, void(*handler)(int)) {
    sigset_t set;
    struct sigaction act;

    /* Setup the handler */
    act.sa_handler = handler;
    act.sa_flags = SA_RESTART;
    sigaction(signo, &act, 0);

    /* Unblock the signal */
    sigemptyset(&set);
    sigaddset(&set, signo);
    sigprocmask(SIG_UNBLOCK, &set, NULL);

    return;
}


void signal_handler(int signo) {
    printf("receiving sig %d\n", signo);
}

int main()
{
    printf("test\n");
    timer_t timer1 = create_timer(TT_SIGUSR1);
    timer_t timer2 = create_timer(TT_SIGUSR2);

    install_sighandler(TT_SIGUSR1, signal_handler);
    install_sighandler(TT_SIGUSR2, signal_handler);

    set_timer(timer1, TIME_INTERVAL_1);
    set_timer(timer2, TIME_INTERVAL_2);

    while (1) sleep(1);
    return 0;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文