exec parsing in shell

Source: Internet
Author: User

Reference: Linux commands, editor and Shell programming, advanced Programming for UNIX environments

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, let's explain the concept of fork.

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).

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 that executes under the current shell and no longer enables other shells.

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.

Another way to do this is by the source command, which no longer produces a new shell, but executes all commands under the current shell.

Source

The source command is the point (.) Command.

Enter man source under Bash, find the source command explanation, and you can see the explanation "Read and execute commands from filename in the current shell environment and ...". As you can tell, the source command executes each command in the parameter file in the current process, not the other driver process (or Sub-shell).

Exec:

Enter man exec under bash and find the EXEC command interpreter where you can see "No new process is created." Such an explanation, which means that the EXEC command does not produce new sub-processes. So what's the difference between exec and source?

The EXEC command closes the current shell process when it executes, 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)

    1. 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. After executing exec for fork (), this strategy can improve the efficiency, if copy at the beginning, then after exec, the child process data will be discarded and replaced by the new process.

    1. 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 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.