tags:

views:

496

answers:

5

What is the difference between system and exec family commands? Especially i want to know which one of them creates child process to work?

+7  A: 

The exec function replace the currently running process image when successful, no child is created (unless you do that yourself previously with fork()). The system() function does fork a child process and returns when the command supplied is finished executing or an error occurs.

BobbyShaftoe
+17  A: 

system() calls out to sh to handle your command line, so you can get wildcard expansion, etc. exec() and its friends replace the current process image with a new process image.

With system(), your program continues running and you get back some status about the external command you called. With exec(), your process is obliterated.

In general, I guess you could think of system() as a higher-level interface. You could duplicate its functionality yourself using some combination fork(), exec(), and wait().

To answer your final question, system() causes a child process to be created, and the exec() family do not. You would need to use fork() for that.

Carl Norum
+3  A: 

system() will execute the supplied command in a child process that it spawns. exec() will replace the current process with the invocation of the new executable that you specify. If you want to spawn a child process using exec, you'll have to fork() your process beforehand.

Timo Geusch
A: 

system() will invoke your systems default command shell, which will execute the command string passed as an argument, that itself may or may not create further processes, that would depend on the command and the system. Either way, at leas a command shell process will be created.

With system() you can invoke any command, whereas with exec(), you can only invoke an executable file. Shell scripts and batch files must be executed by the command shell.

Basically they are entirely different used for different purposes. Moreover exec() replaces teh calling process, and does not return. A more useful comparison would be between system() and spawn(). While system may be simpler to invoke, it returns a value that tells you whether the command shell was invoked, and tells you nothing about the success of the command itself. With spawn() you can get the process's exit code; by convention non-zero is used to indicate error conditions. Like exec() spawn() must invoke an executable, not a shell script or built-in command.

Clifford
A: 

To create a process:

  • fork(2), a system call directly to the kernel

To execute a program, replacing the current image:

  • execve(2), a system call directly to the kernel, usually just called exec

To wait for a child process to finish:

  • wait(2), a system call directly to the kernel

To run a program in a shell in a child process and wait for it to finish:

  • system(3), a library function

To get the man pages for all of the above:

   $ man 2 fork execve wait
   $ man 3 system
DigitalRoss