Differences between fork, source, and exec in linux
Source: Internet
Author: User
In linux, fork, source and exec are different. shell commands can be divided into internal commands and external commands. internal commands are in special file format. def implementation, such as cd and ls. external commands are implemented through system calls or independent programs, such as awk and sed. both source and exec are internal commands... in linux, fork, source and exec are different. shell commands can be divided into internal commands and external commands. internal commands are in special file format. def implementation, such as cd and ls. external commands are implemented through system calls or independent programs, such as awk and sed. both source and exec are internal commands. when fork uses the fork method to run the script, shell (parent process) will generate a child process to execute the script. when the child process ends, it will return the parent process, but the ring of the parent process The environment will not change because of the change of child process. when the source method is used to run a script, the script is executed in the current process, rather than a child process. because all execution results are completed in the current process, if the script environment changes, the current process environment will also change. source. /my. sh or .. /my. when sh exec runs a script in exec mode, it is executed in the current process in the same way as source, but the remaining part of the original code in the process will be terminated. similarly, the environment in the process changes with the script. conclusion: generally, fork is used by default during execution. You can run the pstree command to check the relationship between parent and child processes. If you want the parent process to obtain the environment variable of the child process, the * fork (/directory/script. sh) fork is the most common, that is, directly using/directory/script in the script. sh to call the script. sh script. run a sub-shell script to execute the call. when sub-shell is executed, parent-shell is still running. After sub-shell is executed, return parent-shell. sub-shell inherits environment variables from parent-shell. however, the environment variables in sub-shell do not bring back parent-shell * source (source/directory/script. sh) the difference with fork is that instead of opening a sub-shell to execute the called script, it is executed in the same shell. therefore, the variables and environment variables declared in the called script can be obtained and used in the main script. * exec (exec/directory/script. sh) exec is different from fork. you do not need to create a new sub-shell to execute the called script. the called script and the parent script are executed in the same shell. However, after exec is used to call a new script, the content after the exec line in the parent script will not be executed again. This is the difference between exec and source. 1.sh code :#! /Bin/bash A = B echo "PID for 1.sh before exec/source/fork: $" export A echo "1.sh:\ $ A is $ A" case $1 in exec) 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/bash echo "PID for 2.sh:$ $" echo "2.sh get \ $ A = $ A from 1.sh" A = C export A echo" 2.sh:\ $ A is $ A "however later, points? E runs as follows ??? ^? Result: $./1.sh fork $./1.sh source $./1.sh exec
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.