Difference between "system" and "exec" in Linux?
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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 calledexec
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
Solution 5
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 least 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 the 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.
Admin
Updated on September 20, 2020Comments
-
Admin over 3 years
What is the difference between
system
andexec
family commands? Especially I want to know which one of them creates child process to work? -
Krishna Oza over 7 yearsdoes the
system
call spawns a new shell also to execute the command given or it executes the command in the same shell. -
Carl Norum over 7 years@Krishna_Oza - there is no "same shell", unless the program calling
system()
is itself a shell. I'm not sure I follow. My documentation here says: "Thesystem()
function hands the argument command to the command interpretersh(1)
." -
patryk.beza over 7 yearsQuote from
system
POSIX manual: Thesystem()
function shall behave as if a child process were created usingfork()
, and the child process invoked the sh utility usingexecl()
as follows:execl(<shell path>, "sh", "-c", command, (char *)0);
.