From: Nathan Ricci Date: 2002-07-24T23:48:01+09:00 Subject: Re: Can I get data back from a fork? On Wed, 24 Jul 2002 13:15:06 +0000, Jim Freeze wrote: > Hi: > > Is there a way to get data back from a fork? Here is a little code > snippet. I think I understand what it is doing and why it is not > working, but I don't have much experience in this area. > > class Batch > def initialize > @Job = Struct.new("Job", :pid, :result, :cmd) @job_table = {} > end > > def launch > job.pid = Process.fork { redirect(job) } @job_table[pid] = job > ensure > wait > end > > def redirect(job) > .. setup stuff > job.result = system(job.cmd) > end The problem is that when you fork, you are creating a new process. So, sure, the two objects may have the same ID, but it is the same ID in two seperate processes. So, any way, to do what you want you have to have some way to get the processes to talk to eachother using some kind of IPC. The most common thing to do would be to use pipes. The other option would be to use threads instead of seperate processes. Threads are similar, except that the variables are all shared between the two threads of execution so there is no need for IPC. --Nathan Ricci