fork without fork? 
Author Message
 fork without fork?

I'm looking for a way to do my bidding in Perl.

I'm writing a program where I need two filehandles open - an intermediary,
of sorts.  One's <STDIN>, and the other is to a network socket.

My problem comes when I want to share some variables.  The way I'm doing
the program now is that I call fork, and it spawns the child, and everything
runs well, except when I want to variable share.

The while(<STDIN>) of the child doesn't share variables with the while(<S>) of
the parent.  I need a way to share variables.  For example, I want to set one
with the user input, and clear it when text comes across the socket, but I
can't get the variable to clear.  Using subroutines didn't seem to help.

I could do it with a cache file on disk, but that'd be a waste, both disk
wise and resource wise.  If someone's got a good idea of how I can check
both filehandles for use and be able to separate them, i.e. keep STDIN and
S separate while checking them both, I'd appreciate it.

-Pete K
--

"So you still think you need to know UNIX?" - Ed Krol       DNRC God of IRC
"I hope you guys aren't really this clueless." -Heather Arnold         ENFP



Tue, 05 Oct 1999 03:00:00 GMT  
 fork without fork?

Quote:

> The while(<STDIN>) of the child doesn't share variables with the
> while(<S>) of the parent.  I need a way to share variables.  For
> example, I want to set one with the user input, and clear it when
> text comes across the socket, but I can't get the variable to clear.
> Using subroutines didn't seem to help.

Variables aren't shared between processes.  You could try using the
SysV shared memory calls, if you have them.  The easiest way is
probably to not fork, and instead use select() to decide where input
is waiting.  See the perlfunc(1) and perlipc(1) manpages for more
details.

Nat



Tue, 05 Oct 1999 03:00:00 GMT  
 fork without fork?



Quote:
> I'm looking for a way to do my bidding in Perl.

> I'm writing a program where I need two filehandles open - an
> intermediary,
> of sorts.  One's <STDIN>, and the other is to a network socket.

> My problem comes when I want to share some variables.  The way I'm doing
> the program now is that I call fork, and it spawns the child, and
> everything
> runs well, except when I want to variable share.

> The while(<STDIN>) of the child doesn't share variables with the
> while(<S>) of
> the parent.  I need a way to share variables.  For example, I want to
> set one
> with the user input, and clear it when text comes across the socket,
> but I
> can't get the variable to clear.  Using subroutines didn't seem to help.

> I could do it with a cache file on disk, but that'd be a waste, both
> disk
> wise and resource wise.  If someone's got a good idea of how I can check
> both filehandles for use and be able to separate them, i.e. keep STDIN
> and
> S separate while checking them both, I'd appreciate it.

Well, if you're using Unix, whats wrong with.....

$ENV{'VARNAME'}='value';

Then do your fork.

Of course, if the parent is supposed to know that the child has modified
these values, this won't work.

In that case, you need to use some form of inter-process communication,
like shared memory semaphores for example.

There are a number of choices "Unix Network Programming" explains them
all.

Regards
Neil



Wed, 06 Oct 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Fork Does not Fork

2. Wait for input without select or fork?!

3. asynch io without fork...suggestions?

4. Forking without exec.

5. fork, childs, zombies, start a process in the background without waiting for it

6. forking without wait

7. Please help me choose which to use! exec(), system(), fork(), setsid(), fork &&

8. Fork goddamit! Fork!

9. Forking problems with CGI, Apache, Win2K

10. passing filehandles to a pre-forked child

11. Test::Harness problem with forked process...

12. Debugging forked procedure

 

 
Powered by phpBB® Forum Software