Raspberry Pi_Eng_20.2.2 How Shell Runs Commands


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


20.2.2  How Shell Runs Commands

 

20.2.2.1    Types of Kernel's System Calls

 

   "fork" system call 

 

This call is one of the system call methods that a process uses to execute another process. When a command needs to be executed, a child process is created and executed separately.

 

When the child process is created like this, a new process ID is assigned and new memory is allocated for the new process. The basic information of the parent process that called "fork" to create a new process is copied all to the new process area. When a new process is created, the original parent process is executed as it is, and the child process created using "fork" call is also executed in parallel.

 

In this call, the parent's environment variables, user information, and working directory are all copied to child, but changes in child are not passed to parent.

 

 

   "exec" system call

 

This call is one of the system call methods that a process uses to execute another process. When a command needs to be executed, the parent process is still used without creating a new process, but the parent process is replaced with a child process.

 

Since the existing process is used as it is, the existing process ID is used as it is, and instead of allocating a new memory space like "fork" call, the memory of the existing process is used as it is. It is executed in the way that the memory of the parent process that called the "exec" is overwritten by the codes of the new child process, so only the child process created by the "exec" call, not the parent process that called the "exec" call, remains in memory.

 

   "wait" system call

 

When a new child process is created and executed in the parent process, this call causes the parent process to wait without doing anything until the child process terminates.

 

 

   "exit" system call

 

This call terminates the process that executed the call. All processes can terminate execution with an "exit" call. When an "exit" call is used in a child process, an exit signal notified and an exit status is transmitted to the parent process.

 

20.2.2.2    How Shell Runs According to Command Type

 

There are internal commands and external commands in commands executed by Shell.

 

An internal command is a command embedded in the shell itself, and it is the command that there is no separate executable file to execute, but Shell itself interprets and executes. Commands such as "cd" and "echo" are one of the internal commands.

 

An external command, on the other hand, refers to a command that is executed in such a way that it finds an executable file necessary to execute the command using the file search path set in the environment variable, and execute the executable file. That is, it refers the command that have an executable file corresponding to an external command, and the executable file for the command is usually stored under "/bin" directory.

 

The shell uses different process processing methods for handling internal and external commands. The internal command is designed to process commands in the Shell's own process, whereas the external command creates a new child process using a "fork" call.

 


 

20.2.2.3    Bash's Working Procedure

 

Init script refers a script that has the ability to create a new process using the Start-Stop-daemon. The init script allows you to use the "fork" call function to create another process in the currently running process and perform the necessary processing.

 

The following is a basic example of how init scripts work in Bash.