Ruby, Threads and Processes

Hello everybody,

One thread of my programm calls the following code:


%x{ convert -scale ‘100x100>’ -antialias #{src} #{dst} }

my question is: does this freeze all other threads until the os call is completed?

should i use something like

fork {
%x{ convert -scale ‘100x100>’ -antialias #{src} #{dst} }
} }
Process.wait

instead?

Any help appreciated!

-g.

%x{ convert -scale '100x100>' -antialias #{src} #{dst} }

When you use %x{}, ruby use a pipe you can see it

svg% cat b.rb
#!/usr/bin/ruby
t = Thread.new do
   p "before"
   12.times {|i| Thread.pass; p "after #{i}"; sleep 1 }
   p "after"
end

t1 = Thread.new do
   p "before sleep"
   p %x{a.sh 2}
   p "after sleep"
end

t.join
t1.join
svg%

svg% cat a.sh
#!/bin/sh
sleep $1
echo "a.sh"
svg%

svg% b.rb
"before"
"before sleep"
"after 0"
"after 1"
"after 2"
"a.sh\n"
"after sleep"
"after 3"
"after 4"
"after 5"
"after 6"
"after 7"
"after 8"
"after 9"
"after 10"
"after 11"
"after"
svg%

Guy Decoux

When you use %x{}, ruby use a pipe you can see it

Guy Decoux

Hi,

thanks for your time, but i do not understand the answer. Let me
rephrase the question:

From what i have seen in the docs, Kernel.exec executes an OS command
in the current process, and Kernel.system creates a new subprocess to
execute the command. The question is, %x{} is an alias for exec or
system? ie does it create a new process?

-g.

execute the command. The question is, %x{} is an alias for exec or
system? ie does it create a new process?

%x{} use `(backquote)

The pickaxe say (p. 415)

   returns the standard output of running cmd in a subshell.

Guy Decoux

Definately a new process.

if any program (under *nix – win32 is totally different this way) calls
the exec() function (in C), the process calling it is replaced by the
new program. The usual way a C program spawns a sub-process is with a
fork() call, to make a copy of the parent process, then one of them will
call exec(), getting itself replaced with the subprocess. Not entirely
intuitive, but it works. If Ruby were to use exec, the script would stop
at the first use of %x{}. %x{} is an alias for system.

···

On Sat, 2003-07-05 at 03:04, George Moschovitis wrote:

When you use %x{}, ruby use a pipe you can see it

Guy Decoux

Hi,

thanks for your time, but i do not understand the answer. Let me
rephrase the question:

From what i have seen in the docs, Kernel.exec executes an OS command
in the current process, and Kernel.system creates a new subprocess to
execute the command. The question is, %x{} is an alias for exec or
system? ie does it create a new process?

if any program (under *nix – win32 is totally different this way) calls
the exec() function (in C), the process calling it is replaced by the
new program. The usual way a C program spawns a sub-process is with a
fork() call, to make a copy of the parent process, then one of them will
call exec(), getting itself replaced with the subprocess. Not entirely
intuitive, but it works. If Ruby were to use exec, the script would stop
at the first use of %x{}. %x{} is an alias for system.

slightly related…
could’nt we emulate Kernel.fork on windows using some CreateProcess
magic?
At least, would’nt be useful a Kernel.spawn that appears as fork+exec
on nix and CreateProcess on win ?

The pickaxe say (p. 415)
returns the standard output of running cmd in a subshell.

thank you!
-g.

Not exactly. system doesn’t redirect stdout; %x{} captures stdout using a
pipe and sticks it in a string.

Regards,

Brian.

···

On Sun, Jul 06, 2003 at 12:45:59AM +0900, Aredridel wrote:

%x{} is an alias for system.