Sunday 21 November 2010

Diff between system and exec

You want to run an external program and wait for it to finish. To do this you would need to fork the current process into two processes (parent and child) and replace the child process with an exec call to the external program while the parent process
waits for the child process to complete. This is what the system function** does. It is implemented in terms of a fork***, an exec,and a waitpid****. Due to the way fork works the file handles of the parent are inherited by the child, so STDOUT and STDERR in the process created by system are the same as in the Perl program. This is why
output shows up when you use system or exec.

The exec function* replaces the current process with the one specified
in the arguments, so exec("bash", "-l") would replace the perl process
with bash login shell running interactively.

The qx// operator***** (also known as ``) is very different. It is
most likely implemented via the popen****** function from
SUSv2*******. It captures the STDOUT (but not the STDERR) of an
external program and returns it as a list of lines (in list context)
or as string (in scalar context).

No comments:

Post a Comment

Tweets by @sriramperumalla