What is the difference between system
and exec
family commands? Especially I want to know which one of them creates child process to work?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
System() will create child process and invoke another sub shell while exec() will not create child process.Given Example will clear difference.
some code...
exec('ls -l')
echo "1 2 3" // This will not executed in bash (as exec command use same shell)
some code ...
system (ls -l) echo "1 2 3" // This will be executed after finishing System child process as they are different from parent PID.
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.
In general, "system" is so inefficient and you should not use it unless you have a small code. If you need to execute several programs in your process, you would better use fork&exec though you make it more complicated. Here is a list of differences between them:
1- "system" command creates a copy of shell to execute your program. Every time you call a system, you create a copy of shell. So do not use it when you have lots of programs to execute inside your process.
2- Specifically, if you want to execute system functions such as "mv", "mkdir", it would be better to use routines such as mkdir(), unlink() or remove() instead of executing them through "system("rm ....") or system("mkdir ....")".
3- Since system calls shell to execute your desired program, you may have some user permission problems. For example, someone may crack your code and execute something else instead of the program you intended to execute through system command.
For more information, you may read chapter 11 of this book: "UNIX Systems Programming" by David Curry.
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 usingexec
, you'll have tofork()
your process beforehand.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.JonSpencer answer is fine, except that child_status must be an int (no a pointer to int) and has to be passed to wait function by reference.
So, the code would be mainly the same, just changing those couple of things:
(Point out that I don't have enough reputation yet to comment Jon's post so I edited it. Some people rejected the edition asking me to answer the question instead of editing it, but I think that in this case it is much simpler, practical and clear to edit an existing code just correcting a small mistake than to write a full copy/paste/modify answer.) Anyway, thanks JonSpencer for your answer, it was really useful for me!