Both exec and source belong to the Bash Internal command (builtins commands), which is entered under bash for man exec or
Man source can view all the internal command information.
The bash shell commands fall into two categories: external commands and internal commands. An external command is implemented by a system call or by a separate program.
such as SED, awk, and so on. Internal commands are implemented by a special file format (. def), such as CDs, history, exec, and so on.
Before explaining the difference between EXE and source, explain the concept of fork first .
Fork is a system call to Linux to create a subprocess (child process). Child process is
a copy of the parent process that obtains a certain resource allocation from the parent process and
the environment that inherits the parent process. The only difference between a child process and a parent process is the PID (process ID).
environment variables (variables passed to the subprocess, which are fundamentally different from local variables and environment variables) can only be
A one-way pass from the parent process to the child process. does not affect the parent process regardless of how the child process's environment variable changes
Environment variables.
Shell script:
There are two ways to execute shell scripts, one is to create a new shell, and then execute the corresponding shell scripts;
One is executed under the current shell and no more shells are enabled.
The way to create a new shell and then execute scripts is to add the following statement at the beginning of the scripts file
#!/bin/sh
The General script file (. sh) is this usage. This method first enables the new Sub-shell (the new subprocess), and then in its
Execute the command. Another method is the source command mentioned above, which no longer produces a new shell, but under the current shell
Execute all orders.
Source:
SOURCE command dot (.) Command.
Enter man source under bash and find the source command interpretation where you can see the explanation
"Read and execute commands from filename to the current shell environment and ...".
As you can see, theSource command executes individual commands in the parameter file in the current process, not
another screwdriver process (or Sub-shell).
exec:
Enter man exec under bash and find the EXEC command interpreter where you can see the "No new process is created."
This explanation is that the EXEC command does not produce a new child process . So what is the difference between exec and source?
When executed, the EXEC command closes the current shell process and then continues execution with the subsequent command.
1. The system call exec is a new process to replace the original process, but the process of the PID remains unchanged.
Therefore, it can be argued that the exec system call does not create a new process, but replaces the
the contents of the original process context. the code snippet, data segment, and stack segment of the original process are replaced by the new process.
A process consists mainly of the following elements:
(1) a procedure that can be executed
(2) All data associated with the process (including variables, memory, buffers)
(3) Program context (program counter PC, save program execution location)
2. Exec is a function cluster, consisting of 6 functions, which begin with excl and EXECV respectively.
Execute the EXEC system call, generally, to create a new process with the fork () function, and then let the process go
executes the EXEC call. we know that after fork () establishes a new process, the parent shares the code snippet with the child process,
But the data space is separate, but the parent process will copy the contents of its data space into the subprocess, and
The context is also copy to the subprocess. and in order to improve efficiency, the use of a write-time copy of the strategy, namely
When you create a subprocess, you do not copy the address space of the parent process, and the parent-child process has a common address space.
The address space is replicated when a child process needs to write data, such as writing to a buffer, and the
Buffer to the subprocess. The parent-child process thus has a separate address space. After executing exec after fork (),
This strategy can be very efficient, if the first copy, then exec, the child process data will be discarded,
Replaced by a new process.
3. The difference between exec and system
(1) Exec is directly with the new process to replace the original program to run, after the operation is not back to the original program.
(2) system is to invoke the shell to execute your command, system=fork+exec+waitpid, after the execution is completed, return to
in the original program. Continue to the following section.
In short, if you call with Exec, you should first fork a new process and then exec. and system does not need
You fork the new process, which has been encapsulated.