exec and Source command parsing in shell

Source: Internet
Author: User

Most of the article comes from: http://www.cnblogs.com/zhaoyl/archive/2012/07/07/2580749.html as a note-keeping blog memo.

Both exec and source belong to the Bash Internal command (builtins commands), where you can view all the internal command information by entering man exec or man source under bash.

The bash shell commands are divided into two categories: external commands and internal commands. External commands are implemented through system calls or independent programs, such as SED, awk, and so on. Internal commands are implemented by a special file format (. def), such as CD, history, exec, and so on.

Before explaining the difference between EXE and source, explain the concept of fork and the common way that shell script executes .

Fork is a system call for Linux that is used to create a subprocess (child process). A 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 the parent process is the PID (process ID).

Environment variables (variables passed to child processes, hereditary is the fundamental difference between local variables and environment variables) can only be passed from the parent process to the child process in one direction. Regardless of how the environment variable of the child process changes, the environment variables of the parent process are not affected.

There are two common ways to execute shell script, one of which is to create a new shell, and then execute the corresponding shell scripts, one that executes under the current shell and no longer enables other shells.

1. The new way to create a shell and then execute scripts is to add the following statement at the beginning of the scripts file

#!/bin/sh

The usual script file (. sh) is this usage. This method first enables the new Sub-shell (the new child process) and then executes the command under it.

2. Another method is the source command mentioned above, which no longer produces a new shell, but executes all commands under the current shell.

Can do a test, we have a simple script called output.sh

The first line outputs the shell process ID that executed the script

The second line outputs its parent process ID

We start the shell window of the system and look at the shell's process ID and parent process ID:

Executive output.sh

Its process ID is different from the previous Shell process ID, and the parent process ID is the previous shell process ID, indicating that this execution is a new shell, and then execute the corresponding shell scripts;

Look again in the source mode of execution:

Both the process ID and the parent process ID are identical to the previous shell, stating that the source command executes under the current shell and no longer enables other shells.

The following exec,exec command, when executed, closes the current shell process and then switches to the subsequent command to continue execution.

1. The system calls exec to replace the original process with a new process, but the PID of the process remains the same. Therefore, it can be argued that the exec system call did not create a new process, but instead replaced 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 aspects:

(1) A program 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.

Executes the exec system call, which is generally the case with the fork () function to create a new process and then let the process execute the EXEC call. We know that after the fork () establishes a new process, the parent will share the code snippet with the child process, but the data space is separate, but the parent process can copy the contents of the data space into the child process, and the context will be copied to the child process. In order to improve efficiency, adopt a copy-on-write strategy, that is, when creating a child process, does not copy the address space of the parent process, the father-child process has a common address space, only when the child process needs to write data (such as to the buffer to write data), this time will copy the address space, copy the buffer into the child Thus, the parent-child process has an independent address space. When exec is executed after the fork (), this strategy can improve the efficiency, if copy at the beginning, then after exec, the child process data will be discarded, replaced by the new process.

3. The difference between exec and system

(1) Exec is directly with the new process to replace the original program run, after the completion of the operation is not back to the original program.

(2) system is called by the Shell to execute your command, system=fork+exec+waitpid, after execution, go back to the original program. Continue with the following section.

In short, if you call with Exec, you should first fork a new process and then exec. The system does not need you to fork the new process, has been encapsulated.

exec and Source command parsing in shell

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.