# System() and \`\` problem in Multithreading

**URL:** <https://www.ruby-forum.com/t/system-and-problem-in-multithreading/139639>\
**Category:** Ruby\
**Created:** [June 5, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/system-and-problem-in-multithreading/139639 "2008-06-05T18:25:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Youser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@Lee\_Youser](https://www.ruby-forum.com/u/Lee_Youser)\
**Post date:** [June 5, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/system-and-problem-in-multithreading/139639/1 "2008-06-05T18:25:53Z")

</div>

Hi:

I want to execute external routines in my ruby program in a  
multithreading way and capture the error information occured in external  
routines into a log file. But when i executed external routines in this  
way:  
error\_msg = /_`...execute cmd...`_/  
All threads in my program didn’t seem to run in synchronous way, when i  
used /_system("_//_…execute cmd…_//_"),_/ all threads did run  
synchronously but i can’t get the error information except using this  
method: /_system("_//_…execute cmd…_//\* \> log\_file") \*/and then get  
the information from the log\_file.

i made a curt routines to recur this problem:

* * *

test.rb:

## threads = [] 5.times do |num| threads \<\< Thread.new do system(“ruby a.rb thread#{num+1}”) #`ruby a.rb thread#{num+1}` system(“ruby b.rb thread#{num+1}”) #`ruby b.rb thread#{num+1}` system(“ruby c.rb thread#{num+1}”) #`ruby c.rb thread#{num+1}` system(“ruby d.rb thread#{num+1}”) #`ruby d.rb thread#{num+1}` end sleep(1) end threads.each {|t| t.join}

* * *

a.rb:

sleep(5)  
time\_now = Time.now.strftime("%H:%M:%S")  
raise “#{time\_now} AAAAA Exception #{ARGV}”

* * *

* * *

b.rb:

sleep(5)  
time\_now = Time.now.strftime("%H:%M:%S")  
raise “#{time\_now} BBBBB Exception #{ARGV}”

* * *

* * *

c.rb:

sleep(5)  
time\_now = Time.now.strftime("%H:%M:%S")  
raise “#{time\_now} CCCCC Exception #{ARGV}”

* * *

* * *

d.rb:

sleep(5)  
time\_now = Time.now.strftime("%H:%M:%S")  
raise “#{time\_now} DDDDD Exception #{ARGV}”

* * *

when use \*/system /\*the output as follow:

D:\Mywork\Ruby\project\>ruby test.rb  
a.rb:3: 00:12:58 AAAAA Exception thread1 (RuntimeError)  
a.rb:3: 00:12:59 AAAAA Exception thread2 (RuntimeError)  
a.rb:3: 00:13:00 AAAAA Exception thread3 (RuntimeError)  
a.rb:3: 00:13:01 AAAAA Exception thread4 (RuntimeError)  
a.rb:3: 00:13:02 AAAAA Exception thread5 (RuntimeError)  
b.rb:3: 00:13:03 BBBBB Exception thread1 (RuntimeError)  
b.rb:3: 00:13:04 BBBBB Exception thread2 (RuntimeError)  
b.rb:3: 00:13:05 BBBBB Exception thread3 (RuntimeError)  
b.rb:3: 00:13:06 BBBBB Exception thread4 (RuntimeError)  
b.rb:3: 00:13:07 BBBBB Exception thread5 (RuntimeError)  
c.rb:3: 00:13:08 CCCCC Exception thread1 (RuntimeError)  
c.rb:3: 00:13:09 CCCCC Exception thread2 (RuntimeError)  
c.rb:3: 00:13:10 CCCCC Exception thread3 (RuntimeError)  
c.rb:3: 00:13:11 CCCCC Exception thread4 (RuntimeError)  
c.rb:3: 00:13:12 CCCCC Exception thread5 (RuntimeError)  
d.rb:3: 00:13:13 DDDDD Exception thread1 (RuntimeError)  
d.rb:3: 00:13:14 DDDDD Exception thread2 (RuntimeError)  
d.rb:3: 00:13:15 DDDDD Exception thread3 (RuntimeError)  
d.rb:3: 00:13:16 DDDDD Exception thread4 (RuntimeError)  
d.rb:3: 00:13:18 DDDDD Exception thread5 (RuntimeError)

but when use \*/`cmd` /\*the output as follow:

D:\Mywork\Ruby\project\>ruby test.rb  
a.rb:3: 00:16:17 AAAAA Exception thread1 (RuntimeError)  
a.rb:3: 00:16:22 AAAAA Exception thread2 (RuntimeError)  
b.rb:3: 00:16:22 BBBBB Exception thread1 (RuntimeError)  
b.rb:3: 00:16:28 BBBBB Exception thread2 (RuntimeError)  
a.rb:3: 00:16:28 AAAAA Exception thread3 (RuntimeError)  
c.rb:3: 00:16:28 CCCCC Exception thread1 (RuntimeError)  
b.rb:3: 00:16:33 BBBBB Exception thread3 (RuntimeError)  
d.rb:3: 00:16:33 DDDDD Exception thread1 (RuntimeError)  
c.rb:3: 00:16:33 CCCCC Exception thread2 (RuntimeError)  
a.rb:3: 00:16:33 AAAAA Exception thread4 (RuntimeError)  
c.rb:3: 00:16:38 CCCCC Exception thread3 (RuntimeError)  
d.rb:3: 00:16:38 DDDDD Exception thread2 (RuntimeError)  
b.rb:3: 00:16:38 BBBBB Exception thread4 (RuntimeError)  
d.rb:3: 00:16:44 DDDDD Exception thread3 (RuntimeError)  
a.rb:3: 00:16:44 AAAAA Exception thread5 (RuntimeError)  
c.rb:3: 00:16:44 CCCCC Exception thread4 (RuntimeError)  
b.rb:3: 00:16:49 BBBBB Exception thread5 (RuntimeError)  
d.rb:3: 00:16:49 DDDDD Exception thread4 (RuntimeError)  
c.rb:3: 00:16:54 CCCCC Exception thread5 (RuntimeError)  
d.rb:3: 00:16:59 DDDDD Exception thread5 (RuntimeError)

The expected output is the first one(use _/system/_), but i don’t know  
why the output is different between the two ways. Could you someone can  
tell me why? 🙂  
Thank you very much!

regards

---

<div class="post-metadata">

**Author:** ![Lee\_Youser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@Lee\_Youser](https://www.ruby-forum.com/u/Lee_Youser)\
**Post date:** [June 5, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/system-and-problem-in-multithreading/139639/2 "2008-06-05T21:21:37Z")

</div>

On Jun 5, 2008, at 10:23 AM, Lee Youser wrote:

> The expected output is the first one(use _/system/_), but i don’t know  
> why the output is different between the two ways. Could you someone  
> can  
> tell me why? 🙂  
> Thank you very much!

the order threads run is _always_ undefined unless you coordinate  
them. it’s just luck they are run in order for you - make your system  
very busy and you will see this.

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![Lee\_Youser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@Lee\_Youser](https://www.ruby-forum.com/u/Lee_Youser)\
**Post date:** [June 5, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/system-and-problem-in-multithreading/139639/3 "2008-06-05T22:00:46Z")

</div>

On Jun 5, 2:19 pm, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> very busy and you will see this.
> 
> regards.
> 
> ## a @[http://codeforpeople.com/](http://codeforpeople.com/)
> 
> we can deny everything, except that we have the possibility of being  
> better. simply reflect on that.  
> h.h. the 14th dalai lama

As our Opearting Systems professor drove this into our brains daily,  
so shall I repeat it here: You can make no assumptions about the  
relative rate of concurrent processes.
