in reply to Closing socket handle of child process
The listening parent is just used to listen for a command to stop the connection which kills the child process
You want the child to send a command to the parent to kill that same child? Isn't that a complicated way to just exit?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Closing socket handle of child process (complicated?)
by tye (Sage) on Jan 03, 2012 at 05:17 UTC | |
exit isn't complicated. Waiting to read a command while busy writing output, in Perl, certainly can be complicated. Given that sockets are being used here, select would likely be a useful solution. But fork is equally useful and likely makes for a simpler solution (well, at least if you aren't using a Perl that emulates fork(), such as non-Cygwin Windows Perl does). I'd be curious what doesn't work about the presented code. I don't see any obvious problem with it. And I don't see where the failing of the code is described. Also, what the OP calls the "listening parent" seems to me likely (based on the code and on other parts of the description) to actually be the middle process that is reading (not "listening") while the parent of that middle process is the one listening for new connections to come in (and the middle's child, the grandchild process, is the one writing). Certainly, the code presented is incomplete. But I fail to imagine a likely problem with the grandparent process that would likely point the OP to the problem being with the code presented. paulc1976, please describe, precisely, how your code fails to behave as you desired. And please provide more code. Best would be to pare down the other code so that you have a minimalist, self-contained example that others can run to observe how it fails to meet your described desired requirements. - tye | [reply] |
by paulc1976 (Novice) on Jan 03, 2012 at 15:36 UTC | |
Thank you for your reply, I didn't post the entire code as I didn't want to take up too much space, instead of posting the entire daemon code, I thought it might be better to post the code for the helper program. This program (helper) acts as an intermediary between my Perl TK program and a forking server which accepts connection requests from helper. helper also has a bidirectional pipe connected to communicate with the TK program. My aim is to send a text command to the helper such as Close_Server which when detected would close the socket connection with the forking server. The problem was that due to blocking I couldnt implement listening to STDIN (this is the piped output from TK program) and listening to the socket in the same while loop without using select (as you suggest). I've tried to use fork which works great but killing the child process does not close the socket, the data still keeps coming through. The only way to stop this is by killing the entire process.
Any guidance would be much appreciated, Paul | [reply] [d/l] |
by tye (Sage) on Jan 03, 2012 at 16:33 UTC | |
You could have the parent call shutdown:
That might cause the child to get a "RESET" response if it continues to write after that point (which should cause the TCP stack to refuse to forward future write requests, even if the program ignored the failure and continued to try to write data). But it might just be that the child is ignoring SIGTERM. You could set up a $SIG{TERM} handler in hopes of preventing that problem. I recall that children ignore the signals that their parents were ignoring. But I'm surprised to not find documentation of that fact right now. I think that setting $SIG{TERM} would override that but I have a nagging doubt about that. - tye | [reply] [d/l] |