Refer to: Linux commands, editors, and shell programming, and advanced programming in UNIX environment
Both exec and source are Bash Internal commands (builtins commands). Enter man exec or man source in Bash to view all internal command information.
Bash shell commands are divided into two types: External commands and Internal commands. External commands are called by the system or are independentProgramImplemented, such as SED and awk. Internal commands are implemented by special file formats (. Def), such as CD, history, and exec.
Before explaining the differences between EXE and source, describe the concept of fork.
Fork is a Linux System Call used to create child processes ). A child process is a copy of the parent process. It obtains a certain amount of resource allocation from the parent process and inherits the environment of the parent process. The unique difference between a child process and a parent process lies in the PID (process ID ).
Environment variables (variables passed to sub-processes, genetic is the fundamental difference between local variables and environment variables) can only be transmitted from the parent process to the sub-process in one way. How the environment variables of a non-process change will not affect the environment variables of the parent process.
Shell script:
There are two ways to execute shell scripts. One is to generate a new shell and then execute the corresponding shell scripts. The other is to execute the shell in the current shell and no longer enable other shells.
To generate a new shell and then execute scripts, add the following statement at the beginning of the scripts file:
#! /Bin/sh
This is the general usage of script files (. Sh. In this way, the new sub-shell (new sub-process) is started, and then the command is executed under it.
Another method is the source command mentioned above. instead of creating a new shell, execute all the commands in the current shell.
Source:
The source command is the dot (.) command.
Enter man source in bash and find the explanation of the source command. You can see the explanation "read and execute commands from filename in the Current Shell environment and ...". It can be seen that the source command is to execute the commands in the parameter file in the current process, rather than starting another sub-process (or sub-shell ).
Exec:
Enter man exec in bash and find the exec command explanation. We can see that there is an explanation like "no new process is created.", which means that the exec command does not generate a new sub-process. So what is the difference between exec and source?
When the exec command is executed, the current shell process is closed, and the subsequent command is switched to continue execution.
1. The system calls exec to replace the original process with a new process, but the PID of the process remains unchanged. Therefore, the exec system call does not create a new process, but replaces the content of the original process context. Original ProcessCodeSegment, data segment, and stack segment are replaced by new processes.
A process mainly includes the following aspects:
(1) an executable program
(2) All data associated with the process (including variables, memory, buffers)
(3) program context (program counter PC, where the program execution is saved)
2. exec is a function cluster consisting of six functions with excl and execv headers respectively.
Execute the exec system call. This is generally the case. Use the fork () function to create a process and then let the process execute the exec call. We know that after fork () creates a new process, the parent process shares the code segment with the child process, but the data space is separated, however, the parent process will copy the content of its data space to the child process, and the context will also copy the content to the child process. To improve efficiency, a copy-on-write policy is adopted, that is, when a child process is created, the address space of the parent process is not copied, and the parent and child processes have a common address space, only when the sub-process needs to write data (such as writing data to the buffer), the address space will be copied and the buffer will be copied to the sub-process. The Parent and Child processes have independent address spaces. After fork () executes exec, this policy can improve efficiency. If copy is started, data of sub-processes will be discarded after exec, replaced by a new process.
3. What is the difference between exec and system?
(1) exec directly replaces the original program with a new process, and does not return to the original program after running.
(2) system is to call shell to execute your command. System = fork + exec + waitpid. After the execution is complete, return to the original program. Continue to the following part.
In short, if you use exec to call, you should first fork a new process, and then exec. While system does not need you to fork a new process, it has been encapsulated.