Interrupted system calls (socket read) , EINTR and SIGTERM

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply sudhakar 11/19/2004 11:12:30 AM

See related articles to this posting


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
Reply Jens 11/19/2004 1:03:18 PM
comp.unix.programmer 10656 articles. 64 followers. Post

1 Replies
1185 Views

Similar Articles

[PageSpeed] 5


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

"Interrupted system call", read and socket
Hello, I have a server application and a client one which communicate with sockets and SSH tunneling (ssh -N -f -L8888:localhost:3333 user@localhost). I have to use ssh tunneling because of firewalls. My problem is that the read in the client has the "Interrupted system call" error even if I put this while(-1==(nr=read(DIST,ptr,nl))) { if(errno!=EINTR) break; } and it appears randomly. If I do not use ssh tunneling, it works. If someone has an idea ... Thanks, Karim. karim bernardet wrote: > Hello, > > I have a server...

Read() on serial port produces "Interrupted system call"
Hello all, I'm at a loss here. I control 7 different custom serial devices from an embedded system. One of them is causing me hell. Often when I read from it with read(), I get an error "Interrupted system call". But it seems to work fine from a minicom or microcom terminal. Here's the config: if (0 >= (Serial->fd = open(Serial->DevicePath, O_RDWR | O_NOCTTY | O_NDELAY )) ) { SimpleLog_Write(SL_ERROR, __func__, "Opening device %s failed: r=%d, errno=%d, %s", Serial->DevicePath, Serial->fd, errno, strerror(errno)); return Serial->fd; ...

pthread_create fails with return value = 4 and errno string is EINTR (Interrupted System Call)
Hi Folks, I browsed through some of the earlier posts where people were getting EPERM / EGAIN and were getting errno set ot EINTR; and the obvious solution was to not check errno. In my case my program runs for 3-4 hours after which I get a return value of 4 for pthread_create, the thread gets created though without problem; but pthread_create man page says only error I can get is EAGAIN but not EINTR (which is 4 in errno-base.h) Can somebody advise something; conf. is as follows : IA64 box, RedHat 2.6.9-34.EL Shrirang wrote: > Hi Folks, > I browsed...

How to use read() system call to read an 32bit integer
Hello, All Maybe this question is not proper to post here:( but anyway would someone help me? I am doing my operating system homework. I know how to use a buffer string to do read and write. But how about 32-bit int? Here is my method to do: typedef unsigned char *byte_pointer; write(fd, (byte_pointer)&i, sizeof(int)); read(fd, (byte_pointer)&j, sizeof(int)); it works. I don't know whether my method is a common way. or there's some other ways to do it. thanks in advance. In article <1189515918.904562.109800@r34g2000hsd.googlegroups.com>, Yin Zhu <zhuyin.nju...

Read system call is reading what is being writtein instead of OK when communicating with modem
Hi, I wrote a serial driver to communicate with modem.When I am reading my read is reading what is being written instead of OK.what can be thereason.Any suggestions are appreciaated.Thank you. "Ramakrishnaprasad.A" <Ramakrishnaprasad.a@gmail.com> writes: > Hi, > I wrote a serial driver to communicate with modem.When I am reading my > read is reading what is being written instead of OK.what can be > thereason.Any suggestions are appreciaated.Thank you. There is echo functionality somewhere on the system. This means that either the modem or some other ...

[tao-users] CORBA call throws Transient when EINTR is returned by system call
----------MB_8CBE263076E5687_1230_B83_webmail-mf04.sysops.aol.com Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii" ?? ?TAO VERSION: 1.6 ?? ?ACE VERSION: 5.6 ( This is repeatable in the latest version, however the stack traces ?and log come from this version ) ?? ?HOST MACHINE and OPERATING SYSTEM: ?? ? ? ?i686, Linux 2.6.27.5-117.fc10.i686 ?? ?TARGET MACHINE and OPERATING SYSTEM, if different from HOST: ?? ? i686, Linux 2.6.24.4-rt4 ?? ?COMPILER NAME AND VERSION (AND PATCHLEVEL): gcc 4.2...

sigaction and waitpid causes "interrupted system call" when calling fgets()
Hi, I'm using ubuntu 7.10. I'm trying to run "ls" in the background while I continue to receive input. I also want to do something when "ls" finishes. The program loops and waits for input. Whenever it receives input, it does the following. The parent calls fork(). The child uses execvp to run "ls". The parent doesn't call waitpid() An error happens when I call fgets(). Any help is appreciated. Below is the code: -------------------------------------------------------------------------------------- #include <stdio.h> #in...

Interrupted System Call
Hi, I have the following program, using message queues. As a message is received it is forked, so that execution occurs on another process. Some operations (mainly file operations) that are occuring on the other processes may take a long time. My problem is that as that as a new message is arrived(while another process is executing), an error is being given and the error set in errno is Interrupted System Call. I think the problem is with EINTR. Can someone tell exactly how to fix it in my program pls, so that it would not give any more problems hopefully. Thanks in Advance /// C...

read() system call
Hello, I've been busy with this problem for days and I still cannot solve it, so I hope someone else can help me. The problem is as follows: 1) My program uses UNIX sockets to wait for an incoming connection and creates a thread for each connection. 2) In this thread, I handle specific commands. 3) For example, if a user issues a request to download a file: I create a thread in which I handle the request. The program consists of a server process and a client program. This is the thread-prototype for sending a file to a connected client: void * sendFile(char * fullData); In an earlie...

read system-call
Hi, I'm working on a Linux-programm with a signal-handler and therfore I need to use the read-system-call instead of cin.get or sth. like that. With getline my program works fine, it reads a line from stdin and passes it to a buffer but when I try to replace getline with read, sth. is wrong!? can u help me? //this works fine length=cin.getline(cmd_buffer1,512).gcount(); for (z=0;z<length;++z){ cmd_buffer[z]=cmd_buffer1[z]; cmd_buffer[length]='\0'; //using read instead if cin.getline !? length=read(STDIN_FILENO, cmd_buffer1, 80); the reason why I use the for-loop (which...

about interruptable system calls
in my program, i found that sem_wait() is not always interrupted by the signal of a timer. after the program keep running for some time, sem_wait became interruptable by timer signal, which is SIGRTMIN. is this standard posix signal feature? or there is a bug in my code? On Jul 16, 11:54 am, steve yee <yiton...@gmail.com> wrote: > in my program, i found that sem_wait() is not always interrupted by > the signal of a timer. after the program keep running for some time, > sem_wait became interruptable by timer signal, which is SIGRTMIN. is > this standard posix signal feature?...

interrupted system call
If a particular system call is interrupted, where should it be restarted in kernel mode or in user mode ? Is there any special reason for it ? In article <8c7d4a6e.0407230537.557d472b@posting.google.com>, junky_fellow@yahoo.co.in (junky_fellow) wrote: > If a particular system call is interrupted, where should it be > restarted in kernel mode or in user mode ? > Is there any special reason for it ? If the system call supports the SA_RESTART functionality and you've requested this with sigaction(), it will be restarted in the kernel (as far as the caller of t...

read() system call
Hi, Basic question about read() - Suppose there's total of 10 bytes to read from a file, and they were read in the first attempt of the read() call, also the size_t count field of the read call is more than 10. Would read() return 10, or EOF in this case? My understanding is that in the first call, it returns 10, on the second call it would return EOF. Is that correct? Otherwise if it had read all 10 bytes in the first call and returned EOF we wouldn't know how much it read. Thanks, Bahadir Bilgehan.Balban@gmail.com wrote: > Hi, >=20 > Basic question abo...

"Interrupted system call" in C lib even with mp:without-interrupts CL 8.1
"Interrupted system call" in C lib even with mp:without-interrupts AllegroCL 8.1, Mac OS X Hi all again: I am doing I/O via USB and an external C library for communication handling to talk to a power meter (for HF power and standing wave ratio measurements). One particular task is to upload new firmware to the device. Uploading the firmware is a critical task and takes some 50 seconds when done directly using a command line utilty written in C to drive comm lib. It is critical that the upload process doesn't get interrupted - the device will be in a state requiring a power do...

tclx and interrupted system call
greetings all last week our production system had a problem running a tcl script: solaris 2.8 tclsh8.4 tclx8.3 couldn't open "20040206.txt": interrupted system call while executing "open $filename r" (procedure "for_file" line 3) invoked from within i haven't looked at the code - doesn't "open" check for EINTR? isillight@hotmail.com (isillight) wrote in message news:<8d99cbc0.0402090555.7035a14a@posting.google.com>... > greetings all > > last week our production system had a problem > running a tcl script: ...

pread() and read() system calls
I have written a character device driver. The driver implements the my_read() function. When user call read() or pread() system calls my_read() invoked but I dont know how to identify whether its from the pread() or read() call. It is important for me to know since the offset should be taken under consideration only upon pread() call. DO YOU KNOW HOW DO I DISTINGUISH READ() and PREAD() calls inside my_read() function?!?!? ...

reading input from a system call
I need to read the last line of a text file, then process its data in my C code. Something like tail DATAFILE | scanf .... unfortunately the first command is shell, the second is C I know I can perform a "system" command in my C code, but how do I feed its yield to scanf or whatever? Thanks in advance, jan olieslagers wrote: > I need to read the last line of a text file, then process its data in > my C code. Something like > > tail DATAFILE | scanf .... > > unfortunately the first command is shell, the second is C > I know I can perform a "system"...

List of interrupted system calls
Hello, Does anyone know of a list of interrupted system calls, for Linux, preferably on the Internet? Especially the ones not taken into account by SA_RESTART flag. I came once over such a list, in Stevens' Unix Network Programming, but it was the first edition, and I do not have it handy. Thanks :) Alfred Nez wrote: > Hello, > Does anyone know of a list of interrupted system calls, for Linux, > preferably on the Internet? Especially the ones not taken into account by > SA_RESTART flag. > > I came once over such a list, in Stevens' Unix Network Programming, bu...

read system call hangs
Hi, I am using "read" system call in my C++ project to read data from sockets. = I have also set read timeout (SO_RCVTIMEO) to 5 seconds using "setsockopt",= but when I run my application under load run then sometimes this "read" sy= stem call hangs, it neither returns data nor it times out. Any suggestion/help would be appreciated. Thank you, Jaydeep On 05/22/12 10:01 AM, Jaydeep Chovatia wrote: > Hi, > > I am using "read" system call in my C++ project to read data from sockets. I have also set read timeout (SO_RCVTIMEO) to 5 seconds using...

Interrupted system call during waitpid
hi, I am forking a process and then wait for the calling process by waitpid(pid, &status, WEXITED) ,where pid is the pid of the forked process .The invoked process works fine but waitpid returns -1 and on catching error i recieved "INTERRUPTED SYSTEM CALL". Actually forked process executes a shell script in which say i am executing a simple 'date' cmd but for the same i am receiving the "INTERRUPTED SYSTEM CALL". Kindly suggest some way tosolve this problem. Piece of code is : if (pid >0) { waitpid(pid, &status, WEXITED); if(WIFEXITED(status)) { *exi...

read system call hangs
Hi,=20 I am using "read" system call in my C++ project (Redhat Linux) to read data= from sockets. I have also set read timeout (SO_RCVTIMEO) to 5 seconds usin= g "setsockopt", but when I run my application under load run then sometimes= this "read" system call hangs, it neither returns data nor it times out.= =20 Any suggestion/help would be appreciated.=20 Thank you,=20 Jaydeep Jaydeep Chovatia <chovatia.jaydeep@gmail.com> wrote: > I am using "read" system call in my C++ project (Redhat Linux) to read > data from sockets. I have also ...

pread() and read() system calls
I have written a character device driver. The driver implements the my_read() function. When user call read() or pread() system calls my_read() invoked but I dont know how to identify whether its from the pread() or read() call. It is important for me to know since the offset should be taken under consideration only upon pread() call. DO YOU KNOW HOW DO I DISTINGUISH READ() and PREAD() calls inside my_read() function?!?!? Hagit wrote: > > I have written a character device driver. The driver implements the > my_read() function. When user call read() or pread() system calls ...

SIGSTOP and Interrupted System Call
Hello all, I'm trying to write a piece of software that requires an arbitrary process to be stopped (with SIGSTOP) before it performs some task. However, I'm having a problem, because one of the processes I'm testing has a SysV IPC message queue receive call in it that blocks. If this process is then stopped at this point, the program exits with: msgrcv: Interrupted system call By handling the EINTR error that is thrown, my test program can cope. However, I can't assume that all programs will be written to handle this can I? Can anyone recommend a solu...

Interrupted system call and telnet.rb
Has anyone else experienced an interrupted system call when using telnet.rb? I use it frequently to do a large provisioning job which very rarely completes because read(2) gets interrupted. The call to read(2) in made by this line in telnet.rb: c = @sock.sysread(1024 * 1024) Consequently, an exception is thrown of the type Errno::EINTR which causes my program to fail in the middle of a telnet session. I've fixed the problem by patching my telnet.rb with the following: --- /usr/lib/ruby/1.8/net/telnet.rb~ 2004-10-05 13:45:15.000000000 -0400 +++ /usr/lib/ruby/1.8/net/telnet.rb 20...