开发者

%exec, system, IO.popopen .. which coomand to use in Ruby?

I am trying to understand which of the %exec, system, IO.popopen commands I can use in Ruby for what circumstances.

These pages explains it but I dont think so I quite understand what does starting a new application etc mean. http://humblelittlerubybook.com/book/html/chapter4.html

From my understand开发者_开发知识库ing : %exec: takes over your application process and runs the new command. So does that mean that once the system command is finished, it just exits OR does it resumes the parent application process ?

io.popopen: spawns a new thread and returns output and errs back to parent application ?. Is this safe command to use when you want to spawn a thread and do something in parallel ?

system: spawns a sub-process. This probably means that the control returns back to parent once the spawned process is done ? Does the parent process halts until the child completes execution ? [that sounds dangerous]

Can someone explain to me in English, in what circumstances do we use these commands?

Thanks


exec() is used to replace the currently executing program with the program specified as a parameter to exec(). The program you specify as a parameter to exec() becomes the currently executing program. If you're using RoR, you probably do not want to do this. If you're using plain old Ruby for a program, you might want to do this under certain circumstances.

system() is used to invoke a new program in a subshell. The currently executing program will block, or wait, until the program you invoked has finished running. You are right - this can be dangerous if you have a long-running program. If you have a program which can run-away but you want to use system, you can wrap the system call in the child part of a fork() call and have the parent kill the child process if it runs for too long. More on fork(): http://en.wikipedia.org/wiki/Fork_%28operating_system%29

system() will not give you the STDOUT or STDERR of the application in its return value. Instead, you'll get the exit status code. It's a general practice that programs will exit with a status code of 0 if they are successful, and anything else if they fail.

The backticks are an alternative to system() which return not the exit status code but the actual STDOUT of the program which was executed. You can obtain the exit status code of the program through the "$?" variable.

IO.popopen() (and Open3 and Open4) allows you to work with the STDIN and the STDOUT of a subprocess.

This seems to be a good place to get a little more familiar with some of these concepts as they are implemented in Ruby: http://tech.natemurray.com/2007/03/ruby-shell-commands.html


exec is something that most people don't need to use. The executed program becomes the active program, your original program looses execution, file handles etc.

popen is about standard input and output, meaning it's good to be used with filters, i.e. programs that write or read from standard input/output.

system is better used to execute a program, your original program keeps executing but waits till that program invoked by system finishes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜