Reputation: 1814
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?
Upvotes: 3
Views: 684
Reputation: 104070
The usual solution to this problem is to use SOCK_NONBLOCK
to socket(2)
or O_NONBLOCK
to fcntl(2)
's F_SETFL
command. 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 sure select(2)
set the filedescriptor? The select(2)
manpage does describe one reason why you see what you're seeing, but it doesn't seem likely:
Under Linux,
select()
may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to useO_NONBLOCK
on sockets that should not block.
If you really just want to prevent the automatic restart, look into SA_RESTART
in sigaction(2)
to prevent restartable system calls from restarting.
Upvotes: 2