Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Thread + fork warning

Reply
Thread Tools

Thread + fork warning

 
 
Ariff Abdullah
Guest
Posts: n/a
 
      10-08-2003
# ruby -e 'a = Thread.new { fork {} }; a.join'
-e:1: warning: fork terminates thread at -e:1
# ruby -v
ruby 1.8.0 (2003-10-05) [i386-freebsd4]

is this intentional?

--

Ariff Abdullah
MyBSD
http://www.MyBSD.org.my (IPv4)
http://staff.MyBSD.org.my (IPv6/IPv4)
http://tomoyo.MyBSD.org.my (IPv6/IPv4)

 
Reply With Quote
 
 
 
 
Bedo Sandor
Guest
Posts: n/a
 
      10-15-2003
On Wed, Oct 08, 2003 at 12:00:50PM +0900, Ariff Abdullah wrote:
> # ruby -e 'a = Thread.new { fork {} }; a.join'
> -e:1: warning: fork terminates thread at -e:1
> # ruby -v
> ruby 1.8.0 (2003-10-05) [i386-freebsd4]
>
> is this intentional?



Hello!

I don't know, but I have the same warning, if I make a
fork{} in the main thread, and there is one or more
other running thread.

% vi thread.rb
1.: #!/usr/bin/ruby -w
2.:
3.: def forkAndWaitInThread
4.: Kernel.fork {
5.: Kernel.exec("sleep 5")
6.: }
7.: Thread.new {
8.: Process.wait
9.: }
10.: end
11.:
12.: 2.times { forkAndWaitInThread }
13.:
14.: Thread.list.each { |t| t.join unless t == Thread.current }
15.:

% chmod +x thread.rb ; ./thread.rb
./thread.rb:4: warning: fork terminates thread at ./thread.rb:8

Seems to work as I expected, but warnings make me misgiving.
Could somebody explain me the reasons? Am I doing wrong
if I leave out of consideration this warning? What is
the correct/usual method to execute processes in background?

% ruby -v
ruby 1.8.0 (2003-10-05) [i386-linux]
...from Debian/sid package

--
bSanyI

 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      10-15-2003
Hi,

In message "Re: Thread + fork warning"
on 03/10/15, Bedo Sandor <(E-Mail Removed)> writes:

|% chmod +x thread.rb ; ./thread.rb
|./thread.rb:4: warning: fork terminates thread at ./thread.rb:8
|
|Seems to work as I expected, but warnings make me misgiving.
|Could somebody explain me the reasons? Am I doing wrong
|if I leave out of consideration this warning? What is
|the correct/usual method to execute processes in background?

When you fork, all other threads are terminated in the child process.
The simplest solution is not mixing fork and threads, for example, use
system instead of fork+exec.

matz.

 
Reply With Quote
 
Bedo Sandor
Guest
Posts: n/a
 
      10-17-2003
On Wed, Oct 15, 2003 at 11:16:43PM +0900, Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: Thread + fork warning"
> on 03/10/15, Bedo Sandor <(E-Mail Removed)> writes:
>
> |% chmod +x thread.rb ; ./thread.rb
> |./thread.rb:4: warning: fork terminates thread at ./thread.rb:8
> |
>
> When you fork, all other threads are terminated in the child process.


But only in the child process!? This is exactly what I
want! In my program the main process forks&execs other
processes, and in the main process creates a thread to
wait/waitpid the forked process, and log the time and
the pid when done. I don't need thread in the Ruby
interpreter in the child processes, they only execs an
external command. I think the correct behavior would
be in a threaded Ruby program that fork does not harm
threads in the main process, but starts an other
interpreter without threads running only with the
commands in the block passed to the Kernel.fork.


> The simplest solution is not mixing fork and threads, for example, use
> system instead of fork+exec.


I have two problems with system():

- I can't log the termination of the spawned
process.

- Don't know if this is a correct way to start
background processes: (I need background processes,
so I have to continue the main Ruby program!)

def startInBg(unixCommand)
system(unixCommand.to_s + "&")
end
...
startInBg 'zcat /var/log/messages*.gz | logClassifier'

It works, and there's no warnin, but what
do You think about +"&" ?

--
bSanyI

 
Reply With Quote
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      10-17-2003
Hi,

In message "Re: Thread + fork warning"
on 03/10/17, Bedo Sandor <(E-Mail Removed)> writes:

|- I can't log the termination of the spawned
| process.

How do you want to watch process? If you want to store logs into a
file, you can use shell redirect such as ">" etc.

If you want to get logs as stream output, you can use popen3 library.

|- Don't know if this is a correct way to start
| background processes: (I need background processes,
| so I have to continue the main Ruby program!)
|
| def startInBg(unixCommand)
| system(unixCommand.to_s + "&")
| end
| ...
| startInBg 'zcat /var/log/messages*.gz | logClassifier'
|
| It works, and there's no warnin, but what
| do You think about +"&" ?

+"&" should work OK; you are working on Unix.

matz.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby fork (thread like) on windows Adam Lauper Ruby 2 05-27-2011 01:05 AM
os.fork and pty.fork Eric Snow Python 0 01-08-2009 06:32 AM
Problem with fork inside a thread and execv() CMorgan C Programming 3 01-02-2008 09:42 PM
thread death callback to be called after fork ara.t.howard@noaa.gov Ruby 2 11-10-2006 09:45 PM
"warning: fork terminates thread" Hal Fulton Ruby 2 06-21-2004 02:08 PM



Advertisments