Re: Get variables from forked child



slaskbrev1@xxxxxxxxx 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/software/linux_or_unix_tools/beta-nettee.tar.gz

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

http://saf.bio.caltech.edu/saf_manuals/beta_nettee_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
.



Relevant Pages

  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)