From: Robert Klemme Date: 2007-04-05T17:20:06+09:00 Subject: Re: Forking and copying variables On 05.04.2007 06:03, Brandon Casci wrote: > I have a script that sets a few variables and then forks. Each fork > executes the script from top to bottom. Is there any way for the forks > to get a copy of all the variables set before the fork, because each > fork thinks it;s running for the first time (and ya I see why because a > fork is a copy of the current process)? > > Maybe forking isn't what I'm looking for, but the performance is so much > better than threading for this task. Use the block form of fork, that will preserve all state: 10:14:12 [~]: ruby -e 'p $$; foo=1; 2.times do fork do while foo < 10 print $$, ":", foo, "\n" foo+=1 end end end' 3840 3468:1 3468:2 3468:3 3468:4 3468:5 3468:6 3468:7 3468:8 3468:9 10:14:42 [~]: 3600:1 3600:2 3600:3 3600:4 3600:5 3600:6 3600:7 3600:8 3600:9 10:14:54 [~]: Kind regards robert