In my application, when alarm fires, i write an integer to a pipe from the signal handler.
In my main application, i forever loop on select() with 0 timeout value.
The problem is that signal handler successfully writes to the write end of the pipe, but select NEVER shows that readability of the read end of the pipe. So in the below pseudo code , return value from select would be 0.
Even what is more preplexing is that if i remove select and simply do a blocking read(), it works!!! i can read the integer written by the handler. It is just that select always think that the read end of the pipe is not readable and hence always return 0.
So pseduo code looks like
int x=0;
for(;;)
{
x = select(maxfd, &readfd, NULL, NULL, &dontWait); // dontWait is timeval with tv_sec and tv_usec = 0
if (x == 1)
//check for read descriptor and do something
}
The
selectfunction will modify both the sets and the timeout. You have to re-initialize the descriptor sets and timeout each iteration of the loop before theselectcall.Also, you shouldn’t really use
if (x == 1), but use theif (FD_ISSET(some_fd, &readfd)). And remember thatmaxfdshould be the max filedescriptor used in any set plus one.