472,984 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 software developers and data experts.

delivering a signal doesn't interrupt read(2)

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
Nov 14 '05 #1
4 3802
"Ollie Cook" <ne**@mutare.noc.clara.net> wrote in message
news:slrncga716.183u.ne**@mutare.noc.clara.net...
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
Nov 14 '05 #2
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
Nov 14 '05 #3
Ollie Cook <ne**@mutare.noc.clara.net> wrote in message news:<slrncga716.183u.ne**@mutare.noc.clara.net>.. .
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
Nov 14 '05 #4
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> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ian Pellew | last post by:
Hi; I am on WinXPPro and want to catch the keyboard interrupt signal. Will Python 2.3.3 signal handler catch it? Cannot find this in the Pyt docs, Mind nor can I find `inc += 1` statement...
3
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
11
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
2
by: None | last post by:
Hello, 1. The prototype of function signal in signal.h: void (*signal(int sig, void (*func)(int)))(int); is some complex to me. Would you please explain it to me in detail with the C...
2
by: lovecreatesbeauty | last post by:
Hello, I'm confused by some complex function prototypes. Would you please explain those to me in detail with C language syntax itself with your rich knowledge & experiences. Thank you. 1....
4
by: manochavishal | last post by:
Why would a signal handler cannot in general call standard library functions. I have read whats is said in Standard but needs an explaination on that as i couldn't uunderstand it. Thanx in...
3
by: norm4h8 | last post by:
Hi! I have a question about how to create a process in such a way that it would terminate itself if its wated for input for too long. Here is the story. I have 2 different files, say R.c and...
3
by: joelq1981 | last post by:
Dear friends, I want to extend the interrupt handler of a program, my code is: ... if ($pid = fork) { local $SIG{INT} = sub { print "Please select:\n"; $choice =...
10
by: Udai Kiran | last post by:
Hi all Iam having trouble with signal handler. Where will the execution return after invoking a signal handler when that particular signal is recieved? will return to the point where we register...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.