Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > delivering a signal doesn't interrupt read(2)

Reply
Thread Tools

delivering a signal doesn't interrupt read(2)

 
 
Ollie Cook
Guest
Posts: n/a
 
      07-26-2004
Hi,

I am having some difficulty with read(2) and interrupting signals. I
expect I am misunderstanding how the two work together, so would
appreciate some guidance.

I am trying to 'time out' a socket read after a certain delay. The logic
is (I will provide a test program below):

- create and connect socket
- associate signal handler function with SIGALRM
- start an alarm
- call read
- if read returns -1 check if errno is EINTR to detect the timeout

The manual page for read(2) on my OS (FreeBSD RELENG_4) says:

| ERRORS
|
| ...
| [EINTR] A read from a slow device was interrupted before any
| data arrived by the delivery of a signal.

I took this to mean that the delivery of a signal while read(2) was
blocking would cause it to return -1 and set errno to EINTR. However, I
believe I have a test case that shows this not to be true, so I must be
misunderstanding the interaction between read(2) and signals.

The test program (URL below), takes a hostname, port and timeout in
seconds as its arguments. It prints out a message to standard output
when the signal handler is called, and also when the read(2) completes
successfully.

If I use netcat to provide something for it to connect to, with three
test cases, I get the following results (netcat called as "nc -l -4 -p
3311" in all cases):

1) provide nothing for the program to read from netcat:

# ./readsignal localhost 3311 5
signal received: 14 (after 5 seconds)
*hangs*

2) provide data to be read before 5 seconds elapse

# ./readsignal localhost 3311 5
completed test successfully
#

3) provide data to be read after 5 seconds elapse

# ./readsignal localhost 3311 5
signal received: 14
completed test successfully
#

These results suggest to me that read(2) is not in fact interrupted by
the signal as the manual suggests.

My two questions are:

1) how does read(2) work in conjuction with signals, if not in the way
I interpreted it to?

2) what would be the recommended way of "timing out" a read from a
socket?

The test program that I was using to create these results is here:

http://www.olliecook.net/readsignal.c

Thanks for your time.

Ollie

--
Ollie Cook Systems Administrator, Claranet UK
 
Reply With Quote
 
 
 
 
Alex Fraser
Guest
Posts: n/a
 
      07-26-2004
"Ollie Cook" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am having some difficulty with read(2) and interrupting signals. I
> expect I am misunderstanding how the two work together, so would
> appreciate some guidance.


The read() function is not in ISO standard C. While signals and the signal()
function /are/ a part of standard C, their potential for use within the
confines of standard-guaranteed behaviour is (IMO) small to non-existent.
Therefore your post is OT on comp.lang.c.

<OT>
signal() is apparently causing the read to be restarted after the signal
occurs. Use sigaction() instead and you should have control over this and be
able to get the behaviour you desire.
</OT>

If you still have problems, comp.unix.programmer or a newsgroup dedicated to
your OS would be better places to ask for help.

Alex


 
Reply With Quote
 
 
 
 
Ollie Cook
Guest
Posts: n/a
 
      07-26-2004
On Mon, 26 Jul 2004 16:33:52 +0100, Alex Fraser wrote:
> If you still have problems, comp.unix.programmer or a newsgroup dedicated to
> your OS would be better places to ask for help.


Hi Alex,

Thanks for your time. I'll look up the references you've provided.

Everyone,

Sorry for the OT nature of my post - I wasn't aware that this was the
wrong forum in which to ask.

Ollie

--
Ollie Cook Systems Administrator, Claranet UK
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-26-2004
Ollie Cook <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> Hi,
> [sockets and signals question redacted]


Hi Ollie. You're OT here, this is for discussions about the language
itself, not specific APIs. You're probably better off over in
comp.unix.programmer
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      07-26-2004
Ollie Cook wrote:
>
> Hi,
>
> I am having some difficulty with read(2) and interrupting signals. I
> expect I am misunderstanding how the two work together, so would
> appreciate some guidance.
>
> I am trying to 'time out' a socket read after a certain delay. The logic
> is (I will provide a test program below):

[...]

As pointed out by others, this is off-topic for c.l.c, but you may want
to check out select() rather than trying to interrupt the read() from a
socket. If you need more help on this, comp.unix.programmer may be the
place to go.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET not delivering HTML MattC ASP .Net 3 06-21-2005 08:40 AM
Delivering of aspx pages fails John ASP .Net 0 01-06-2005 07:37 PM
Norton Antivirus Corp 8.1 nolonger delivering new definitions >Fry Computer Support 0 10-05-2004 12:03 PM
New free tool for delivering SCORM courses Albert Ip XML 0 07-10-2004 03:47 AM
Help delivering pdf to user Wendy S Java 1 12-08-2003 05:56 PM



Advertisments