My program goes through a loop like this:
...
while(1){
read(sockfd,buf,sizeof(buf));
...
}
The read function blocks when it is waiting for input, which happens to be from a socket. I want to handle SIGINT and basically tell it to stop the read function if it is reading and then call an arbitrary function. What is the best way to do this?
When your process receives a signal,
read()
will return and the value oferrno
will be set toEINTR
.From
read(2)
:If you amend your code to look more like:
This lets
arbitrary_function()
decide if theread(2)
should be re-tried or not.Update
You need to handle the signal in order to get the
EINTR
behavior fromread(2)
:Gives output: