From: Paul Lutus Date: 2006-08-27T20:35:11+09:00 Subject: Re: Ruby threads and the system call Vincent Fourmond wrote: / ... > Thoughts ? Try this code: ---------------------------------------- #!/usr/bin/ruby -w system "rm temp.txt" if File.exists? "temp.txt" 10.times { |i| x = Thread.new(i) { 10.times { |j| fork { system "echo #{i}, #{j} >> temp.txt" } Process::wait } } x.join } system "wc -l temp.txt" # should print "100 temp.txt" ---------------------------------------- According to the documentation for "fork", "Process::wait" is needed to avoid the creation of zombie processes. "x.join" assures that the log file isn't written by multiple processes in a way that corrupts it. If you want to test simultaneous independent threads, you could write to a separate file per thread, then combine the files after all threads have completed as an accuracy check. But I don't see this as anything but multiple writes to a single file (or console output) that creates I/O inconsistencies, in an otherwise consistent program. > The third behaves a little better, but > crashes after, say, 3 to 4 system calls in a thread... As to "crash", what error messages did you see? How do you define "crash"? This may be system-related. You might not have the resources required for this program, if all threads and forks run at once. Just a guess. -- Paul Lutus http://www.arachnoid.com