Reputation: 678
Seems like an easy problem, but I am not able to figure it out . And I am new to ruby on rails.
1 pid = Process.fork
2
3 if pid.nil? then
4 puts "Child process"
5 else
6 puts " Continuing parent proceesss"
7 end
8
9 puts "This line should be printed only once, by parent"
My understanding was that line 9 should be printed only once by the parent. But, its being printed twice. By both parent and child !!
How do I execute line 9 ONLY in parent? i.e. I want child to exit after line 4.
Thanks for your help.
Upvotes: 0
Views: 445
Reputation: 10564
ha, you almost had it. You need to put exit
after line 4.
if pid.nil? then
puts "Child process"
exit
else
...
Forking a process is designed to allow both processes to continue, right? You're splitting your process into two copies and then they both continue from where they were forked. If you want the child to exit, you should tell it to exit.
Upvotes: 3