I have a select()
to update my UI every second and also to handle user actions from X11. Here's a snippet of this source code:
XEvent e;
/* Input file descriptor */
fd_set in_fd;
/* Get the file descriptor of the link with X11 */
int dpy_fd = ConnectionNumber(disp->dpy);
while (!finish) {
FD_ZERO(&in_fd);
FD_SET(dpy_fd, &in_fd);
if (select(dpy_fd+1, &in_fd, 0, 0, &tv)) {
printf("Event Received!\n");
XNextEvent(disp->dpy, &e);
/* do something */
}
else {
printf("Timer Fired!\n");
/* do something else*/
}
}
So far, everything's ok.
In parallel, I need to use an alarm to do another thing every 500 ms so I implemented this:
static void timer_handler(int sig)
{
signal(SIGALRM, SIG_IGN); /* ignore this signal */
printf("timer_handler\n");
signal(SIGALRM, timer_handler); /* reinstall the handler */
}
int test_timer()
{
printf("test_timer\n");
signal(SIGALRM, timer_handler);
ualarm(1, 500000); /* every 500 ms */
return 0;
}
I get timer_handler in my console every 500 ms but it's like it consumes the event from select()
because I don't have Timer Fired! anymore (no more update of the UI). If I press a key or move the mouse over the UI, I get Event Received! in the console and the alarm is still responding.
Is select()
using the SIGALRM signal? What am I doing wrong? I just want to use select()
to handle the UI and an alarm to call a method every 500 ms (this method multiplexes hardware performance counters).
XNextEvent()
, but we don't have enough information to help you troubleshoot.select()
failed. It returns-1
on error.tv
) passed toselect()
during each iteration, as they might get modified byselect()
itself.