in reply to Re: multi-command sytem call
in thread multi-command sytem call

The main difference between system() and exec() is that exec() forks the child and does not wait for a return, meaning it does not wait until the child process does it's thing and then continue. The system() command does just that, When you use system() the call waits for the child process to do it's thing and then reports the exit status of the child. If the child fails it reports whatever failed status the child exited with.

Hell it could even be a status of zero. You could explicitly assign an exit(0) to an error exit if you so choose. Take the following code fore example:

#!/usr/bin/perl -w use strict; my $var = 1; if ($var == 1) { print "Hello World!\n"; }else{ exit(0); }
In this scenario if the program fails to do what it is suppose to do it will exit with a status of 0. Now this script did not fail to run but if you changed the value of $var it would failed to do what it was suppose to do.

exec() does not return the exit status of the child because , as already explianed, it does not wait for this status report. Instead it returns the status of the forking process itself. If it was able to fork the process into the background it returns a successfull status, if it does not it returns an error status.


www.perlskripts.com

Replies are listed 'Best First'.
Re^3: multi-command sytem call
by belg4mit (Prior) on Aug 25, 2004 at 04:53 UTC
    exec does not fork, it overwrites the current process. It doesn't return, unless something goes wrong, because there's nothing to return to, nor record that it should even try.

    --
    I'm not belgian but I play one on TV.

      I am sorry, yes you are right. Maybe "fork" was a bad word to use here. By it's very nature a forked process runs but the parent process continues to run also. The exec() function does overwrite the current process and is apparent in some simple code such as the following:
      #!/usr/bin/perl -w use strict; my $results = exec("lt", "-al", "."); print <<END; ########################### \$results \= $results ########################### END
      In the above example you will actually see the printed statement appear on the screen following an error message saying that the command "lt" does not exist. When the command or target program does not exist, or the call fails for any other reson is the only time you will get a "return" from exec().
      #!/usr/bin/perl -w use strict; my $results = exec("ls", "-al", "."); print <<END; ########################### \$results \= $results ########################### END
      Now this code will perform the list (ls) command on a *nix system feeding it "-al" as command line arguiments and "." (current dir) as the target to list. That is all the will appear on the screen and the following print statement will never be executed because the exec() function was successfull and overwrote this current process.

      Sorry for any confusion.


      www.perlskripts.com