一个进程使用多个计时器(不链接到 rt)
有没有办法将多个计时器注册到单个进程?我尝试过以下代码,但没有成功。 (使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
create_timer
中缺少:se.sigev_notify=SIGEV_SIGNAL;
install_sighandler
方法。Install_sighandler2
有一个有关指针类型不匹配的编译器警告。while (1)
会占用你的处理器,休眠。create_timer
:se.sigev_notify=SIGEV_SIGNAL;
install_sighandler
methods.Install_sighandler2
has a compler warning about mismatching pointer types.while (1)
will hog your processor, sleep.