Threads and daemons

C

Cd Cd

The getty is a daeamon

When I do soemthing like

#!/usr/local/bin/ruby

threads=[]

4.times do |i|
threads=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=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.
 
M

MenTaLguY

--=-k2V859oTR9eGflf1LDux
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

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

--=-k2V859oTR9eGflf1LDux
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD4DBQBG50UHSuZBmZzm14ERAiXKAKCAwIptd6avHT8s3dsP4U3OcGAHewCYpRC8
YHgcn/Go7WgdbLsR4YJUBA==
=eU/V
-----END PGP SIGNATURE-----

--=-k2V859oTR9eGflf1LDux--
 
C

Cd Cd

Mental said:
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=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?
 
B

Bob Proulx

Cd said:
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=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
 
C

Cd Cd

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
 
C

Cd Cd

Cd said:
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.
 
C

Cd Cd

Cd said:
Cd said:
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.
 
B

Bob Proulx

Cd said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top