[KLUG Programming] perl fork question

Dirk H Bartley programming@kalamazoolinux.org
19 Sep 2003 11:38:13 -0400


On Fri, 2003-09-19 at 10:56, Adam Williams wrote:
> > When you fork in perl, my understanding is that the programs variables
> > are shared, or more specifically that the child and parent have all of
> > the existing variables but each process then has it's own copy of the
> > variables in the program with the same initial state. 
> 
> This isn't just perl, but the standard way a fork() works.

I just wanted to know if I could describe it in words that were correct.

> 
> >  How can I have a
> > variable that can be modified in the child and the parent will see it's
> > change. 
> 
> IPC (Inter-Process Communication).  Can you create a message queue in
> perl?  It is pretty easy in C (on UNIX at least).
> 
> >  What I am interested in specifically is a filehandle.  I would
> > be willing to print a variable to a file in the child and read it in the
> > parent but I am not sure how to accomplish this with a scalar
> > filehandle.
> 
> I don't think you can pass filehandles as values.  The fork'd child does
> inherit all the filehandles however.

I'm going to skin the cat in a different way.  Looks like there are
three good ways.

http://iis1.cps.unizar.es/Oreilly/perl/advprog/ch12_03.htm

Different processes alone will not do it for me because I would still
get blocked in the child reading the ipc from the parent.  So I'm going
to try the multiplexing method.  I may end up having the child and using
the IPC to communicate in a nonblocking fashion.  This is sure opening
my eyes to lots of possibilities.

This is one of those projects which is teaching me that I need to learn
a little more of the pre-requisite knowledge.  I am getting it step by
step.

I'll probably still use fork to send files so I can keep polling the
other receiving handles.

Dirk
> 
> _______________________________________________
> Programming mailing list
> Programming@kalamazoolinux.org
> http://www.kalamazoolinux.org/mailman/listinfo/programming