The difference between fork, source, and exec in Linux

Source: Internet
Author: User

RPM: The difference between fork, source, and exec in Linux

The shell commands can be divided into internal commands and external commands. Internal commands are implemented by special file formats. Def, such as cd,ls. While external commands are implemented through system calls or standalone programs, such as awk,sed. Source and exec are internal commands.

Fork
When you run a script using fork, the shell (parent process) produces a child process to execute the script, and when the child process finishes, it returns to the parent process, but The environment of parent process is not changed by the change of child process.

Source
When you run a script using source, the script executes within the current process, rather than creating a child process. As all execution results are completed within the current process, if the context of the script changes, it will certainly change the current process environment.
source./my.sh or. ./my.sh

Exec
When you run script with exec, it is the same as source, where script executes within the current process, but the remainder of the original code within the process is terminated. Similarly, the environment within the process changes as the script changes.

Conclusion: Usually, if we execute, it is the default fork. You can see the relationship of the parent-child process through the Pstree command. As above, if you want the parent process to get the environment variable of the child process, it is the source mode.

* Fork (/DIRECTORY/SCRIPT.SH)
Fork is the most common, is directly in the script with/directory/script.sh to call script.sh this script.run the time to open a Sub-shell execute the call script, Sub-shell execution, Parent-shell is still in. Sub-shell returns Parent-shell when execution is complete. Sub-shell inherit the environment from Parent-shellVariable. However, the environment variables in Sub-shell are not brought back to Parent-shell
* Source (source/directory/script.sh)
The difference from fork is that it does not open a new Sub-shell to execute the called script, but ratherexecuted in the same shell. So the variables and environment variables declared in the invoked script can be obtained and used in the main script..
* EXEC (exec/directory/script.sh)
Unlike fork, Exec does not need to open a new Sub-shell to execute the called script. The invoked script executes within the same shell as the parent script. But usingafter exec invokes a new script, the content after the Exec line in the parent script is no longer executed. This is the difference between exec and source.

Exp:

1.sh
Code:

#!/bin/Bash A=BEcho "PID for 1.sh before Exec/source/fork: $$"Export AEcho "1.sh: \ $A is $A"  Case$1 inchexec)Echo "using exec ..."exec./2.SH ;; SOURCE)Echo "using Source ..."                 . ./2.SH ;; *)                 Echo "using fork by default ..."                 ./2.SH ;; Esac Echo "PID for 1.sh after Exec/source/fork: $$" Echo "1.sh: \ $A is $A" 

2.sh
Code:

#!/bin/echo"PID for 2.sh: $$"echo"2.sh get \ $A =$ A from 1.sh"  a=echo"2.sh: \ $A is $A"

Then, run the following parameters to observe the results:
$./1.sh Fork
$./1.sh Source
$./1.sh Exec

#fork下主进程 (PID:8523) environment variable for change, and subprocess (PID:8524after execution, return to the main process [[email protected]~]# ./1.SHForkpid for 1.SHBefore Exec/source/fork:85231.SH: $A is busing fork by default ... PID for 2.SH:85242.SHGet $A =b from1.SH2.SH: $A is CPID for 1.SHAfter Exec/source/fork:85231.SH: $A is b#source change of process environment variable,1. SH and 2.sh pid are all 8530, indicating that the same process is performed [[email protected]~]# ./1.SHSourcepid for 1.SHBefore Exec/source/fork:85301.SH: $A is busing source ... PID for 2.SH:85302.SHGet $A =b from1.SH2.SH: $A is CPID for 1.SHAfter Exec/source/fork:85301.SH: $A is c#exec change of environment variable;1the. Sh and 2.sh PID, although the same, but after the completion of 2.sh, and did not return to the original process to continue to execute the remaining code; The main script exits after calling exec, and the rest of the code is not executed. [[Email protected]~]# ./1.SHExecpid for 1.SHBefore Exec/source/fork:85341.SH: $A is busing exec ... PID for 2.SH:85342.SHGet $A =b from1.SH2.SH: $A is C

***********************************************************

It is never too late to learn. --high-base

***********************************************************

The difference between fork, source, and exec in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.