Reputation: 23
When the software is hit by a signal (example: SIGINT), if you previously used signal(SIGINT, SIGINT_HANDLER) (yea, I know signal is evil and all, but just to give the idea) then the software will react this way:
Ok. I need something like this, but not with signals. In particular:
I have the client socket, which I declered with setsockopt and gave it 4 minutes time. If, after 4 minutes, there's nothing on the socket, then the process shall take back control. Well, there's a problem.
Imagine this:
struct timeval tv;
tv.tv_sec = 240; // 4 minutes timeout;
if (setsockopt(my_socket, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval))) {
perror("setsockopt: rcvtimeo");
exit(1);
}
/* I've seen another stack answer where someone suggested this as the correct way to set a timeout. If there's anything wrong with it, please let me know! */
// The user must write a mex on the socket. I know scanf is inappropriate, but it's just to make it simple
char buff[4096];
fgets("%s", buff);
write(my_socket, buff, strlen(buff) // Yes, in my code I check for errors on write;
Ok, now the client wants a coffee, leaves the room and the client machine is waiting for something to put on the socket. Well, 1,2,3,4 minutes pass.
Now, notice, the client is stuck in the scanf function (or fgets, because that's what I should use) when the timeout hits. Therefore, what happens? I don't have the slightest idea. Because my teacher's slide say "the process takes control back". Fine, but what does it mean?
The reason why I talked about signals before is to know if there's like a way to tell the software this:
I know it's very difficult, but is there a way to do this in C?
My professor wants a very robust architecture, but this stuff is quite advanced and I don't know where to look for information.
Upvotes: 0
Views: 349
Reputation: 58868
Nothing happens. SO_RCVTIMEO
sets how long it will be, when you call recv
(or any of the variations like recvfrom
or recvmsg
- or read
), before it returns a timeout error.
You don't call recv
, so you don't get any timeout error.
Even if you did call recv
, this has nothing to do with signals. It doesn't cause recv
to raise a signal after 4 minutes. It just causes recv
to return an error code if it's still waiting after 4 minutes. It doesn't even mess up the socket - you can call recv
again after that, and it will wait some more.
Programs that handle multiple input sources and timing usually use poll
(or one of its variants or select
). This is a function that lets you wait for several things at the same time, so a poll
call can wait for the socket to receive data or for the user to type something or for 4 minutes to be up. You didn't actually ask about this, so I haven't explained it any further.
Upvotes: 1