Tim Pease
1/20/2009 7:42:00 PM
On Jan 20, 2009, at 8:55 AM, James Gray wrote:
> Am I understanding this example right:
>
> #!/usr/bin/env ruby -wKU
>
> printer =3D Thread.new do
> 10.times do
> sleep 1
> puts "Thread running in #{Process.pid}..."
> end
> end
>
> fork do
> p [Process.pid, printer.status]
> printer.join
> end
>
> p [Process.pid, printer.status]
> printer.join
> # >> [457, "sleep"]
> # >> [458, false]
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
> # >> Thread running in 457...
>
> __END__
>
> Are all other threads stopped as part of a process fork?
>
=46rom the fork documentation ....
"The thread calling fork is the only thread in the created child =20
process. fork doesn=92t copy other threads."
And I modified your script slightly so things are a little more =20
clear ...
#!/usr/bin/env ruby -wKU
p "parent: #{Process.pid}"
printer =3D Thread.new do
10.times do
sleep 1
puts "Thread running in #{Process.pid}..."
end
end
fork do
p "child: #{Process.pid}"
p [Process.pid, printer.status]
printer.join
end
p [Process.pid, printer.status]
printer.join
# >> "parent: 9409"
# >> [9409, "sleep"]
# >> "child: 9410"
# >> [9410, false]
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
# >> Thread running in 9409...
So when the child is forked, only the parent thread is copied over and =20=
the "printer" thread has been killed in the child. Hence, you get the =20=
"false" response for the status. And I'll stop being pedantic now ;)
Blessings,
TwP=