Unix Programming - Interrupted system calls (socket read) , EINTR and SIGTERM

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2004 > Interrupted system calls (socket read) , EINTR and SIGTERM





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Interrupted system calls (socket read) , EINTR and SIGTERM
Tadi

2004-11-19, 7:47 am

Hi All,

I have a scenario like this


Function 1

{

Send a message
Wait for the reply for that message in a while(1) loop

}


Function 2

{
Processes requests and replies.

I have a code snippet like this

while(1)
{

while((n = read(connection->sock, buf, len)) < 0 && errno ==
EINTR);

if (n>0)
return n;
if(n==0)
close the connection

if(errno==EINTR)
retry the read call


}


In one of the scenario I receive SIGTERM, and code switched to the
signal handler. From the
signal handler I will do a siglong jump and do some celan up
processing.



If I receive the signal when middle of the read doing some thing,
after signal handler return. The read function trying to read some
thing on the socket and got hung there.Is it because, after returnig
from the signal handler, I lost the context to previous read, and the
other guy continuously looking for the reply for this message. So the
read() got the garbage after return from the signal handler
Some sample log
---------------
Sent the message 0x307 looking for reply
Entering Readit before while(1) of sock read()with len 4000<---signal
hit
TADI:Iam in handle_arbot_sig and the signal mask before calling
siglongjmp:
TADI: abortsig with signo 15 sig_flag 1

Entering Readit before while(1) of sock read()
sock read() n> 0 cas and returning 48 bytese
Entering Readit before while(1) of sock read()
<Got hung here>

If I receive a signal, not in the middle of the read everyting I will
get a reply for the message which I sent.

What could be the possible solution.

This what I thought, set a global flag in the signal handler, check
that flag in the Function 2 and return -1 from there, this will make
the thread 1 guy to take appropriate action and come out of the while
loop.


I couldn't understad why the read is hung after return from the signal
handler,is it because of I am siglongjumping in the signal handler,
After return to the read in Function 2, I lost the previous read
context.

Looking for suggestions.

Thanks
-Tadi
Jens.Toerring@physik.fu-berlin.de

2004-11-19, 7:47 am

Tadi <sudhakar.tadi@gmail.com> wrote:
> I have a scenario like this
> Function 1


> {
> Send a message
> Wait for the reply for that message in a while(1) loop
> }


> Function 2


> {
> Processes requests and replies.


> I have a code snippet like this


> while(1)
> {
> while((n = read(connection->sock, buf, len)) < 0 && errno ==
> EINTR);
> if (n>0)
> return n;
> if(n==0)
> close the connection


> if(errno==EINTR)
> retry the read call


How is this supposed to happen? You already checked errno within the
loop where you are reading. And you are supposed to check errno only
when the call returned -1, otherwise the value stored in errno does
not have any significance and shouldn't be evaluated.

> }


> In one of the scenario I receive SIGTERM, and code switched to the
> signal handler. From the
> signal handler I will do a siglong jump and do some celan up
> processing.


Why a longjmp()? Sounds rather dangerous unless you know extremely
well what you're doing... And how do you want to return from the
signal handler back to the function that does the reading/writing
after a longjmp() call?

> If I receive the signal when middle of the read doing some thing,


A read() call is interrupted by a signal only before any data are
read, never while the read is already in progress. But I don't
know if you are aware that read() can return less bytes than you
are waiting for. If you are sure that more data are going to come
you have to call read() again. The same holds for write(), so
always check how much got read or written and, if reasonable, call
the function again for the rest of the data.

> after signal handler return. The read function trying to read some
> thing on the socket and got hung there.Is it because, after returnig
> from the signal handler, I lost the context to previous read, and the
> other guy continuously looking for the reply for this message. So the
> read() got the garbage after return from the signal handler


Sorry, but I am at a loss to understand what you're writing here.
What's "the context to previous read"?

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com