f



Interrupted system calls (socket read) , EINTR and SIGTERM

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
0
sudhakar
11/19/2004 11:12:30 AM
comp.unix.programmer 10825 articles. 0 followers. kokososo56 (349) is leader. Post Follow

1 Replies
1590 Views

Similar Articles

[PageSpeed] 4

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
0
Jens
11/19/2004 1:03:18 PM
Reply: