# Threads and daemons

**URL:** <https://www.ruby-forum.com/t/threads-and-daemons/114387>\
**Category:** Ruby\
**Created:** [September 12, 2007, 3:15am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387 "2007-09-12T03:15:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 3:15am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/1 "2007-09-12T03:15:04Z")

</div>

The getty is a daeamon

When I do soemthing like

#!/usr/local/bin/ruby

threads=[]

4.times do |i|  
threads[i]=Thread.new do  
%x{/usr/libexec/getty std.19200 tty00}  
end  
end

I see the following  
-bash-3.2$ ps -j  
cd 1877 10367 1877 d6bd16c0 1 I ph 0:00.01  
/usr/local/bin/rub  
cd 29342 1877 1877 d6bd16c0 1 I ph 0:00.00  
/usr/libexec/getty  
cd 2773 1877 1877 d6bd16c0 1 I ph 0:00.00 /usr/libexec/getty  
cd 30538 1877 1877 d6bd16c0 1 I ph 0:00.01  
/usr/libexec/getty  
cd 6442 1877 1877 d6bd16c0 1 I ph 0:00.01  
/usr/libexec/getty

Each thread is creating a new process.

However, when I change to the same code to a program that isnt a unix  
daemon

like

#!/usr/local/bin/ruby

threads=[]

4.times do |i|  
threads[i]=Thread.new do  
%x{/usr/local/bin/party  
}  
end  
end

I see the following  
-bash-3.2$ ps -j  
cd 29735 15232 29735 d6bd1d80 0 Is+ ph 0:00.06 -bash (bash)  
cd 12524 29735 12524 d6bd1d80 1 T ph 0:00.02  
/usr/local/bin/rub

In this case, the thread isn’t creating a process. Can someone shed some  
light on this behavior or discrepancy.

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 3:47am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/2 "2007-09-12T03:47:54Z")

</div>

On Wed, 2007-09-12 at 10:15 +0900, Cd Cd wrote:

> In this case, the thread isn’t creating a process. Can someone shed some  
> light on this behavior or discrepancy.

It sounds like in the latter case, most of the threads aren’t living  
long enough to create the process. If you don’t want your program to  
exit early and kill some or all of the threads, you need your main  
thread to call Thread#join on each one of them.

-mental

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 4:13am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/3 "2007-09-12T04:13:38Z")

</div>

Mental G. wrote:

> On Wed, 2007-09-12 at 10:15 +0900, Cd Cd wrote:
> 
> > In this case, the thread isn’t creating a process. Can someone shed some  
> > light on this behavior or discrepancy.
> 
> It sounds like in the latter case, most of the threads aren’t living  
> long enough to create the process. If you don’t want your program to  
> exit early and kill some or all of the threads, you need your main  
> thread to call Thread#join on each one of them.
> 
> -mental

Okay, bear with this. When I copied the program, I forgot part of it -(  
Here is the entire thing.

#!/usr/local/bin/ruby

threads=[]

4.times do |i|  
threads[i]=Thread.new do  
%x{/usr/local/bin/party}  
end  
end

threads.each{|thr| thr.join}

So do I move join into the do/end block?

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 6:37am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/4 "2007-09-12T06:37:36Z")

</div>

> Whenever I see `...` or %x{…} without it being an assignment it  
> triggers me to question it.
> 
> > So do I move join into the do/end block?
> 
> Try this modification to your program:
> 
> #!/usr/bin/env ruby  
> threads = []  
> 4.times do  
> threads \<\< Thread.new do  
> output = %x{/usr/local/bin/party}  
> puts "party output: " + output  
> end  
> end  
> threads.each{|thr| thr.join}  
> exit 0
> 
> Is there any output from your “party” program? I am hoping there will  
> be errors shown there that were not displayed before that will lead  
> you to the problem.

I see  
Welcome to PARTY! Type ‘?’ for help:

When I step through the program via the ruby debugger. Sometimes ill see  
the same thing when I run the program. It varies from run to run

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 6:05am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/5 "2007-09-12T06:05:06Z")

</div>

Cd Cd wrote:

> Okay, bear with this. When I copied the program, I forgot part of it -(  
> Here is the entire thing.

I think your “party” process is exiting very quickly. I think there  
is a problem when it is run like this and since the output from the  
program is being discarded you are not seeing it.

> #!/usr/local/bin/ruby
> 
> threads=[]
> 
> 4.times do |i|  
> threads[i]=Thread.new do  
> %x{/usr/local/bin/party}

Ouch. Please think about indention here. That line should be  
indented one more level than the previous line. And an indention of  
eight is pretty big for one level of indention.

> ```
> end
> 
> ```
> 
> end
> 
> threads.each{|thr| thr.join}

The %x{} is executing the party program but any output is being  
discarded. Personally I think using ‘system’ is better in that case  
because it does not need to collect the output at all. Using %x{…}  
or using `...` means that ruby needs to wait until the process has  
terminated and then do something with the string of output collected.  
In this case the do something is nothing and it is garbage collected  
but then in that case I think it is better not to collect it at all.

Whenever I see `...` or %x{…} without it being an assignment it  
triggers me to question it.

> So do I move join into the do/end block?

Try this modification to your program:

#!/usr/bin/env ruby  
threads = []  
4.times do  
threads \<\< Thread.new do  
output = %x{/usr/local/bin/party}  
puts "party output: " + output  
end  
end  
threads.each{|thr| thr.join}  
exit 0

Is there any output from your “party” program? I am hoping there will  
be errors shown there that were not displayed before that will lead  
you to the problem.

When I create a simply /tmp/testdaemon script like this:

#!/usr/bin/env ruby  
puts “Hello from testdaemon.”  
exit 0

And then run your example like this:

#!/usr/bin/env ruby  
threads = []  
4.times do  
threads \<\< Thread.new do  
output = %x{/tmp/testdaemon}  
puts "party output: " + output  
end  
end  
threads.each{|thr| thr.join}  
exit 0

Then I see this output:

party output: Hello from testdaemon.  
party output: Hello from testdaemon.  
party output: Hello from testdaemon.  
party output: Hello from testdaemon.

Bob

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 6:55am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/6 "2007-09-12T06:55:23Z")

</div>

Cd Cd wrote:

> Cd Cd wrote:
> 
> > > Whenever I see `...` or %x{…} without it being an assignment it  
> > > triggers me to question it.
> > > 
> > > > So do I move join into the do/end block?
> > > 
> > > Try this modification to your program:
> > > 
> > > #!/usr/bin/env ruby  
> > > threads = []  
> > > 4.times do  
> > > threads \<\< Thread.new do  
> > > output = %x{/usr/local/bin/party}  
> > > puts "party output: " + output  
> > > end  
> > > end  
> > > threads.each{|thr| thr.join}  
> > > exit 0
> > > 
> > > Is there any output from your “party” program? I am hoping there will  
> > > be errors shown there that were not displayed before that will lead  
> > > you to the problem.

There are two errors that I haven’t before.

1)When I hit ctrl-c, I see the following  
./party.rb:9:in `join': Interrupt from ./party.rb:9 from ./party.rb:9:in`each’  
from ./party.rb:9

2)Then, when I’m at the bash prompt, anything I type doesn’t echo back.

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 6:42am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/7 "2007-09-12T06:42:02Z")

</div>

Cd Cd wrote:

> > Whenever I see `...` or %x{…} without it being an assignment it  
> > triggers me to question it.
> > 
> > > So do I move join into the do/end block?
> > 
> > Try this modification to your program:
> > 
> > #!/usr/bin/env ruby  
> > threads = []  
> > 4.times do  
> > threads \<\< Thread.new do  
> > output = %x{/usr/local/bin/party}  
> > puts "party output: " + output  
> > end  
> > end  
> > threads.each{|thr| thr.join}  
> > exit 0
> > 
> > Is there any output from your “party” program? I am hoping there will  
> > be errors shown there that were not displayed before that will lead  
> > you to the problem.
> 
> I see  
> Welcome to PARTY! Type ‘?’ for help:
> 
> When I step through the program via the ruby debugger. Sometimes ill see  
> the same thing when I run the program. It varies from run to run

And on an interesting side note, the status of the program is sleeping  
when I run your version  
10123 0.0 0.4 1076 1900 pb S+ 12:39AM 0:00.01 ruby ./party.rb

However, when I run my version, the status of the program show that it  
has been stopped.

---

<div class="post-metadata">

**Author:** ![overworked\_phd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b5a626/32.png) [@overworked\_phd](https://www.ruby-forum.com/u/overworked_phd)\
**Post date:** [September 12, 2007, 7:21am UTC](https://www.ruby-forum.com/t/threads-and-daemons/114387/8 "2007-09-12T07:21:10Z")

</div>

Cd Cd wrote:

> I see  
> Welcome to PARTY! Type ‘?’ for help:

You said it was a daemon? But the above looks like an interactive  
program to me.

The %x{…} will wait for the program to exit and take the output that  
it collected and assign it or garbage collect it. But if your program  
is interactive and does not exit then the %x{…} can’t either since  
it needs to wait for it to exit.

If you want your “party” program to be a daemon then it needs to be  
modified to be a non-interactive daemon program.

> When I step through the program via the ruby debugger. Sometimes ill see  
> the same thing when I run the program. It varies from run to run

Varying run to run does not sound good either.

Bob
