Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

On a (sufficiently) UNIX system when you create a child process by forking, the child process inherits open file descriptors from the parent (server parents most often use this to share the "listen" socket with a host of children). What you want to do is take a file descriptor created and obtained by the "child" to be handed back to the parent. I don't think that is possible (again, in UNIX).

You can certainly keep the child around and have it use a preexisting connection with the parent to keep using the connection it created. So everytime you create a child, have a file descriptor created between the parent and the child that is used by the child to "forward" traffic back to the parent. Then when the child starts, it can attempt a connection with the server and if succeeds, it tells the parent: which kills all the other children, and then deals with the child as the forwarder to the socket.

Another way to do this could be to have siblings that go at it, and the first successful connection has the parent kill all the other children. Then the successful child continues as the main application, while the parent hangs around to handle the signals etc. Coordination is still required, but the child does not have to be coded as a special gateway.

You can also use pipes and redirects to use STDIN/OUT for these purposes, but one thing I am pretty sure of is that the socket will be killed (shutdown) if your child that created it exits.

-- termix


In reply to Re: Passing a socket to a parent by termix
in thread Passing a socket to a parent by suaveant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found