Get variables from forked child - Unix

This is a discussion on Get variables from forked child - Unix ; Hi. Im writing a small appliction in C under linux 2.6.24. I use fork() to create a child process which does some computing (based on a listening socket). Is it somehow possible that from the parent (main method), access variables ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Get variables from forked child

  1. Get variables from forked child

    Hi.
    Im writing a small appliction in C under linux 2.6.24.
    I use fork() to create a child process which does some computing
    (based on a listening socket).
    Is it somehow possible that from the parent (main method), access
    variables set in the child?
    If not, what would be a better approach?

    Thanks!

  2. Re: Get variables from forked child

    In comp.unix.programmer, slaskbrev1@gmail.com wrote:

    > Hi.
    > Im writing a small appliction in C under linux 2.6.24.
    > I use fork() to create a child process which does some computing
    > (based on a listening socket).
    > Is it somehow possible that from the parent (main method), access
    > variables set in the child?


    Yes and no.

    A child process' environment (internal variables, files, etc) is separate
    and hidden from the parent process. Normally, the parent process cannot
    access anything within the child process at all.

    To permit the parent process to retrieve data from the child process, both
    the child process /and/ the parent process must /share/ a communications
    method. You can use pipes or sockets or files, have the child write the
    results to the I/O mechanism, and have the parent read the results from the
    I/O mechanism. You can also use "shared memory", have the child write the
    results to a known memory location, and have the parent read the results
    from the known memory location.

    > If not, what would be a better approach?
    >
    > Thanks!


    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------



  3. Re: Get variables from forked child

    > Im writing a small appliction in C under linux 2.6.24.
    > I use fork() to create a child process which does some computing
    > (based on a listening socket).
    > Is it somehow possible that from the parent (main method), access
    > variables set in the child?


    It is not normally possible for the another process' address space (and
    therefore variables) from another process (this is a good thing as it helps
    stop one program crashing another).

    You therefore need some sort of interprocess communication instead. This
    comes in many forms and it depends on exactly what you're wanting to do as
    which one is more appropriate.

    I suggest you do a search on Google for "Unix IPC" to get more information.
    HTH



  4. Re: Get variables from forked child

    slaskbrev1@gmail.com wrote:
    > Hi.
    > Im writing a small appliction in C under linux 2.6.24.
    > I use fork() to create a child process which does some computing
    > (based on a listening socket).
    > Is it somehow possible that from the parent (main method), access
    > variables set in the child?


    As others have already pointed out, this is an IPC question and there
    are various methods available for doing it.

    Perhaps you want to see some example code?

    For message queues see my "msgqueue" program, which has a man page here:

    http://saf.bio.caltech.edu/saf_manuals/msgqueue.html

    For bidirectional serial communications (the parent and child write
    short messages to each other through sockets and both poll) see nio.c
    and nio.h files in the current beta release of nettee here:

    http://saf.bio.caltech.edu/pub/softw...-nettee.tar.gz

    The interface implemented for _this_ program using this method is
    described here:

    http://saf.bio.caltech.edu/saf_manua...ettee_cmd.html

    but you could certainly rearrange the parser to accomplish what you
    want. The communication need not be bidirectional - the child could
    certainly be configured to just send up whatever information the parent
    will need to know (so the child won't have to poll for input) and the
    parent could keep an eye out for those messages (it would still need to
    poll).

    Both of these methods require the child to respond to the parent. If
    you want the parent to be able to pull data out of the child, no matter
    what the child is up to, you pretty much have to use shared memory.

    Regards,

    David Mathog

  5. Re: Get variables from forked child

    slaskbrev1@gmail.com wrote:

    > Im writing a small appliction in C under linux 2.6.24.
    > I use fork() to create a child process which does some computing
    > (based on a listening socket).
    > Is it somehow possible that from the parent (main method), access
    > variables set in the child?
    > If not, what would be a better approach?


    As already mentionned, this is IPC.

    One thing that wasn't mentionned is that if you can use threads instead
    of forking (don't know about your application), you don't need special
    communications, you can simply modify a variable in one thread and read
    the new value in the other one.

  6. Re: Get variables from forked child

    Eric Sosman writes:
    > slaskbrev1@gmail.com wrote:
    >> Hi.
    >> Im writing a small appliction in C under linux 2.6.24.
    >> I use fork() to create a child process which does some computing
    >> (based on a listening socket).
    >> Is it somehow possible that from the parent (main method), access
    >> variables set in the child?

    >
    > Not unless they are in shared memory. (In which case they
    > wouldn't be "variables" in the strict C sense, but they would
    > still be "objects.")


    There are two places where the C-standard does not use the term
    'variable' as an adjective:

    If clause-1 is a declaration, the scope of any variables it
    declares is the remainder of the declaration and the entire
    loop, including the other two expressions; it is reached in
    the order of execution
    [6.8.5.3|1]

    and

    A floating-point status flag is a system variable whose value is set
    (but never cleared) when a floating-point exception is raised,
    [7.6|1]

    Everything otherwise declared is an object. C provides no way for an
    application to place a variable (C++ does). Insofar the C-standard is
    concerned, use of shared memory (or any other type of memory
    management done by the application) is just undefined behaviour.

    It is not overly complicated to find Germans with a (German) degree in
    computer science advocating that use of such facilities should be
    considered an error.




+ Reply to Thread