The system function is used to execute shell commands in the process. Note that the Exec family function can actually be used to execute shell commands in the process, but the two functions implement the shell command in a completely different principle.
- The system function is equivalent to a procedure such as fork->exec->wait, which means that the system function calls the fork function to create a child process and then executes the shell command in the child process without affecting the execution of the parent process.
- The direct use of the Exec family function to execute the shell command is to cover the code snippet and data segment of the shell command directly out of the original code snippet and data segment of the process, and the code in the original process will never be executed.
=======================================================The following is a source code for the implementation of the system function:from this code you can see the return values of the various system functions when they are in error:
- When the passed-in parameter is NULL, 1 is returned
- Returns 1 when the fork call fails
- When the EXECL call fails, which means that the shell command cannot be executed, returns 127
- Returns 1 when the Waitpid function is interrupted by a signal
- Returns the state of the shell command itself exiting when the shell command is executed normally
Process Control (12)---system function