Forking and copying variables

Discussion in 'Ruby' started by Brandon Casci, Apr 5, 2007.

  1. 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.

    Thanks for reading.
    Brandon Casci, Apr 5, 2007
    1. Advertisements

  2. Are you using the win32/process version of fork? If so, what you are
    seeing is one of the differences between that and native fork on unix/linux.

    For example, this code:

    puts "foop"

    Process.fork do
    puts "fork"

    on linux produces the following output:


    and on windows (requiring 'win32/process' first) produces:

    Joel VanderWerf, Apr 5, 2007
    1. Advertisements

  3. I see!

    I am on Windows. I was hoping to write something that would work on both
    Windows and Linux so I tried to keep things simple by only using

    I'll read a little more on Win32 Process.create and see if this can get
    me what I need. If so I can detect the platform and fork the appropriate
    way on Linux or Windows.
    Brandon Casci, Apr 5, 2007
  4. Brandon Casci

    Gary Wright Guest

    This seems like a typical IO buffering problem rather than
    different semantics regarding process data shared across

    It looks like in the Windows version stdout is buffered such
    that 'foop' is not written before the fork. After the fork
    the output buffer still has 'foop\n' in it. At some point
    the parent flushes its buffer (with just 'foop\n') and then
    later the child flushes its buffer (with 'foop\nfork\n')
    giving you the result you see.

    This is a common 'gotcha' in Unix if stdout is going to
    a file and so it isn't line buffered. If stdout goes to
    a terminal device it is line buffered and so the problem
    doesn't occur since each call to puts flushes a line of

    I'm not a Windows guy but that's what seems to be happening
    in Joel's example.

    I'm still not sure what problem the original poster is
    encountering. Some sample code and output would help.

    Gary Wright
    Gary Wright, Apr 5, 2007
  5. 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"
    end end'
    10:14:42 [~]: 3600:1

    10:14:54 [~]:

    Kind regards

    Robert Klemme, Apr 5, 2007
  6. The source for win32/process makes it clear what is going on:

    cmd = 'ruby -I "' + $LOAD_PATH.join(File::pATH_SEPARATOR) << '" "'
    cmd << File.expand_path($PROGRAM_NAME) << '" ' << ARGV.join(' ')
    cmd << ' child#' << @child_pids.length.to_s

    startinfo = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    startinfo = startinfo.pack('LLLLLLLLLLLLSSLLLL')
    procinfo = [0,0,0,0].pack('LLLL')

    rv = CreateProcess(0, cmd, 0, 0, 1, 0, 0, 0, startinfo, procinfo)

    It runs the same ruby script again in another process. It's not really
    fork in the unix sense.
    Joel VanderWerf, Apr 5, 2007
  7. Brandon Casci

    Gary Wright Guest

    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}"
    puts "child says: my pid is #{$$}, foo is #{foo}"

    $ ruby fork.rb
    child says: my pid is 7875, foo is 1
    parent says: child is pid 7875, foo is 1

    Gary Wright
    Gary Wright, Apr 5, 2007
  8. Brandon Casci

    Gary Wright Guest

    Thanks for clarifying things Joel. As I said, I'm not a Windows guy...

    Seems like a bad choice to use the name 'fork' for this behavior.
    It is more like a fork/exec of the original script (in Unix
    terminology). It would make more sense to me to have Process.fork
    fail with an exception on Windows and come up with a different name
    for this Windows-only behavior.

    The pseudo-portability of Ruby code has always seemed a bit odd to
    me. Obviously this sort of issue comes up with File IO, OS services,
    process and thread semantics and so on.

    I almost wish that the differences were more obvious via the class
    structure rather than methods of the same class having different
    semantics on different platforms.

    Gary Wright
    Gary Wright, Apr 5, 2007
  9. I agree with all of the above. What the win32 fork does is useful in its
    own way, and it might even be useful to have something on unix/linux
    that has the same semantics (fork/exec original script). But it needs a
    different name...
    Joel VanderWerf, Apr 5, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.