Re: When to call signal(...)
Re: When to call signal(...)
- Subject: Re: When to call signal(...)
- From: email@hidden
- Date: Fri, 10 May 2002 10:57:39 -0700
Andrew Baldwin wrote:
|I am writing to a socket and once in a while I get a SIGPIPE signal. I
|essentially want to be able to handle this in such a way that I can
|disconnect the socket and raise a socket-not-connected type of cocoa
|exception so that my cocoa code can handle it.
|
|How do I write a C-function which can disconnect the socket (if that's
|possible) and raise a cocoa exception? How do I pass the function to
|"void (*signal(int sigcatch, void (*func)(int sigraised))) (int);" -- I
|don't remember how to use function pointers.
Actually, a signal handler is probably not the best way to handle this. If you get a signal, your handler will be called, but the system call that caused the signal should also return an error code if the signal doesn't kill the program. It would be better to ignore the signal (which will keep it from killing the program), and just check the error code. (You need to check the error code anyway, so it's not like there's new code involved.) Once the system call has returned, it should be safe to disconnect the socket (assuming nothing else is preventing it), and you can raise the Cocoa exception safely. (The rule of thumb for signal handler functions is, do as little as possible; ideally, do nothing. Signal handlers are one of the great ill-defined areas of the system; it's best to avoid them where possible.)
To ignore the signal, write "signal(SIGPIPE, SIG_IGN)". (If you really want a signal handler, you'd pass the address of the handler instead of SIG_IGN. To get the address, just write the name of the function. The handler itself would take a single "int" argument--the signal number--and "return" void.)
|when do I call signal(...): each time I'm about to write to a socket,
|each time after I write to a socket, or once at the beginning of the
|program?
You call signal() once, to install a handler (or, as recommended, to ignore the signal). You'd call it again if you wanted to change how your program responded to a signal.
Glen Fisher
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.