From: slaskbrev1 on
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!
From: Lew Pitcher on
In comp.unix.programmer, slaskbrev1(a)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. ------


From: William on
> 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


From: Eric Sosman on
slaskbrev1(a)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.")

> If not, what would be a better approach?

There are many definitions of "better," and I don't know
which you prefer. Some of your options are shared memory,
pipes, sockets, files, doors, the child's exit status (for
small amounts of data), signals (Morse code the hard way),
and others I can't think of at the moment. The general
topic is "inter-process communcation," often abbreviated
"IPC," and a search engine will turn up lots of information.

--
Eric.Sosman(a)sun.com
From: David Mathog on
slaskbrev1(a)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/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