I have found a server by select(), which I want to receive from some clients.
But I find that the server will get blocked in read() by gdb.
So I thought of solving it by adding a SIGALRM, but
when a timeout occurs, it’s still blocked in read().
This happens because, system calls are automatically restarted, the read()
is not interrupted when the SIGALRM signal handler returns.
Is this interpretation correct?
The usual solution to this problem is to use
SOCK_NONBLOCKtosocket(2)orO_NONBLOCKtofcntl(2)‘sF_SETFLcommand. Once the socket is marked non-blocking, it’ll never block when you try to read from it, and you won’t need to try to straddle the divide between blocking or non-blocking. Are you sureselect(2)set the filedescriptor? Theselect(2)manpage does describe one reason why you see what you’re seeing, but it doesn’t seem likely:If you really just want to prevent the automatic restart, look into
SA_RESTARTinsigaction(2)to prevent restartable system calls from restarting.