I am trying to understand processes with Ruby. I am creating 4 child processes from my parent process. The main process starts by writting to a file, then creates the child processes, each of them writte to the same file :
require 'csv'
a = [1, 2, 3, 4]
CSV.open("temp_and_cases_batch_parallel.csv", "ab") do |target_file|
target_file << ["hello from parent process #{Process.pid}"]
a.each do |num|
pid = Process.fork do
target_file << ["hello from child Process #{Process.pid}"]
end
puts "parent, pid #{Process.pid}, waiting on child pid #{pid}"
end
end
Process.wait
puts "parent exiting"
The file output I expect
hello from parent process 3336
hello from child Process 3350
hello from child Process 3351
hello from child Process 3349
hello from child Process 3352
The file output I actually get :
hello from parent process 3336
hello from parent process 3336
hello from child Process 3350
hello from parent process 3336
hello from child Process 3351
hello from parent process 3336
hello from child Process 3349
hello from parent process 3336
hello from child Process 3352
seems like the insert from the parent process is rerun 5 times. How is that possible ? what is going on here ?