From: Gary Wright Date: 2007-04-06T04:03:01+09:00 Subject: Re: Forking and copying variables On Apr 5, 2007, at 4:20 AM, Robert Klemme wrote: > 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: I don't think the state preserving semantics are different for block or non-block forms: $ cat fork.rb foo = 1 if child = Process.fork puts "parent says: child is pid #{child}, foo is #{foo}" else puts "child says: my pid is #{$$}, foo is #{foo}" end $ ruby fork.rb child says: my pid is 7875, foo is 1 parent says: child is pid 7875, foo is 1 Gary Wright