# Threads After Fork

**URL:** <https://www.ruby-forum.com/t/threads-after-fork/158215>\
**Category:** Ruby\
**Created:** [January 20, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/threads-after-fork/158215 "2009-01-20T17:14:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [January 20, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/threads-after-fork/158215/1 "2009-01-20T17:14:58Z")

</div>

Am I understanding this example right:

#!/usr/bin/env ruby -wKU

printer = 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?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [January 20, 2009, 8:44pm UTC](https://www.ruby-forum.com/t/threads-after-fork/158215/2 "2009-01-20T20:44:32Z")

</div>

On Jan 20, 2009, at 8:55 AM, James G. wrote:

> # \>\> Thread running in 457…
> 
> Are all other threads stopped as part of a process fork?

From the fork documentation …

“The thread calling fork is the only thread in the created child  
process. fork doesn’t copy other threads.”

And I modified your script slightly so things are a little more  
clear …

#!/usr/bin/env ruby -wKU

p “parent: #{Process.pid}”

printer = 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  
the “printer” thread has been killed in the child. Hence, you get the  
“false” response for the status. And I’ll stop being pedantic now 😉

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [January 20, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/threads-after-fork/158215/3 "2009-01-20T20:50:28Z")

</div>

On Jan 20, 2009, at 1:42 PM, Tim P. wrote:

> > end
> > 
> > # \>\> [458, false]
> > 
> > **END**
> > 
> > Are all other threads stopped as part of a process fork?
> 
> From the fork documentation …
> 
> “The thread calling fork is the only thread in the created child  
> process. fork doesn’t copy other threads.”

Man, I swear I’ve read that documentation 400 times now and just keep  
missing that. Thanks for the reading lesson Tim!

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [January 21, 2009, 12:44pm UTC](https://www.ruby-forum.com/t/threads-after-fork/158215/4 "2009-01-21T12:44:27Z")

</div>

Hi,

Am Mittwoch, 21. Jan 2009, 04:48:14 +0900 schrieb James G.:

> > > fork do  
> > > p [Process.pid, printer.status]  
> > > printer.join  
> > > end
> > > 
> > > Are all other threads stopped as part of a process fork?
> > 
> > fork doesnâ€™t copy other threads.
> 
> Man, I swear I’ve read that documentation 400 times now and just keep  
> missing that.

A soothing fact that even the well-experienced Ruby programmer  
sometimes detects new aspects.

I think the amazing strike is that the variable containing the  
Thread instance still exists whilst the Thread will surely be  
dead. Joining a dead thread does just nothing. I reduced it to  
these two lines:

t = Thread.new do sleep 100 end  
fork do puts t.inspect end

Output:

#\<Thread:0x28456bcc dead\>

Bertram
