Understanding of the process of child process generation

Source: Internet
Author: User

Directory

Memory management

Implementing the Shell

Generating child processes

Child process Number

Where the process code, data, and stack come from

Execv How to organize the data needed for the processing process

The LDT segment of the sub-process and the selection of the LDT in the GDT

To share open files

Receiving commands from a terminal

Memory management

That is memory management, in fact, just to achieve the shell, the generation of sub-process and the memory space is simply divided. In the words of the author is a grid, each process is zoned out 1M memory load image. Also, page tables are linear mappings, and both linear and physical addresses are consistent. But no matter how simple, to achieve the goal is a good design.

Implementing the Shell

Think about what the shell is. It seems to be able to accept commands entered by the user and execute the command. So what does "execute this name" mean? That is, the command is actually the name of a program, which is to create a sub-process to run the program, and this program is placed in the system can be searched for several paths.

Generating child processes

In order for the shell to receive and respond to the user's input and continue to process the user's next input, it must be a way to let the shell walk two branches, one is to handle the last user input, and a branch is to continue to wait for the user's next input, If all of these two branches are mixed into the shell, I am afraid the logic will be very complicated.

I don't know what the original sub-process concept was due to what was needed. But it's a good fit for the shell's implementation. We only need to arrange two branches on the code, before entering the branch, cloning a child process out, the child process into the processing of the user's branch, and the shell itself can continue to receive user input.

Child process Number

So what does it take to divert the parent-child process? That is, after cloning a process, how to determine which is the parent process which is a child process? This is the function of the child process number, the kernel returns the child process number to the parent process, but returns to the child process is 0, so in this way, the parent-child process logic is diverted.

Where the process code, data, and stack come from

Where does the data from the process come from? That is the function of the EXECV function, in fact, the shell code is not complex, just the user's input is separated by a space, each string your address is assigned to a pointer array of the corresponding elements. Then pass to the EXECV function, and the rest is the responsibility of the EXECV function.

Execv How to organize the data needed for the processing process

Compared to the Linux 0.12来 said, do not consider too large files, do not consider the page table mapping, not to worry about fault pages, but only the logical address and the linear address of each process is the physical address of the instruction, data or stack.

Code, Data: reads the image file of the command program on the hard disk, and then follows the format of the elf file into the corresponding location of the child process space.

Stack: The shell passes over a pointer array of arguments, and the string itself is at another address, execv to do is to combine the pointer array and the string itself into a piece, so that the kernel then copy parameters to the child process to facilitate some, not a string copy of a string. All of a brain is to use it.

The LDT segment of the sub-process and the selection of the LDT in the GDT

Do not know if you have thought, with the LDT to address, the LDT itself is stored in the process table, we cloned the child process can not be cloned process table Ah, just in the process table array to find an idle table entry for the child process. And the cloning of the time did not move the GDT, the child process is how to operate correctly?

Is this another chicken and egg problem? Since we are all entangled in each other, we do not artificially determine the initial state of the situation, so that later processes can run.

Then in advance in the initialization process, the process table in the LDT and GDT in the LDT descriptor are initialized, and so on when assigned to the child process can be used directly.

To share open files

Another problem is that the parent process opens the file, the child process also inherits, this time to tell Task_fs, the parent process of the file descriptor of the number of references to add 1.

Receiving commands from a terminal

So how does the shell construct the array of pointers when it accepts user input from the terminal?

    1. Read the user's input to a buffer
    2. Defines a pointer array
    3. The initial stack data is then assigned the starting address of each string to the corresponding position in the 2 pointer array, separated by a space string. It would be nice to just put the two together in the EXECV.

Understanding of the process of child process generation

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.