Linux0.11 Process creation Function fork () _ Android

Source: Internet
Author: User



In Linux, there is a special function fork (). This function returns the process number PID of the subprocess to the parent process, and returns 0 to the child process. Have you ever wondered how a function might have two different return values? In Linux0.11, each process has a Process control block structure task_struct. The system supports up to 64 processes, defined in the Global array task. Where process 0 is the initial process, all other processes are generated through fork. The fork function of the user state ultimately invokes the system call Sys_fork (). The Sys_fork () system call is completed in 2 steps, the first step is to call the function find_empty_process (), find an idle item in the task array, and the second step to call the Copy_process () function to copy the process.

Processes that are generated for all fork () calls are assigned the process number by incrementing and looping. There is a global variable last_pid to record the last used process number:

Long last_pid=0;

In find_empty_process, increments the last_pid to find the first process number that is not being used by another process as the process number for the new process. If the incremented fat out positive indicates a range, start again from 1.

The process Control block also holds the process's task state segment data Structure TSS, which is used to store all the information for the processor management process. That is, during task switching, the current value of each register in the processor is first automatically saved to the TSS of the current process, and then the TSS of the next process is loaded and extracted from each value to the processor's register. This shows that the task can be switched by preserving the full image of the state of the registers in the task field in TSS.

struct Tss_struct TSS;

Therefore, once you find the free items and process numbers in the task array, we can request the memory of a page for the process control block structure of the processes. This work is done in the copy_process () function. Of course the primary task of the copy_process () function is to copy the parent process information for the child process and set the task status segment of the child process, with the two most critical steps:

1. The EIP in the process TSS is set to the parent process system call return address so that when the child process is selected by the scheduler, it will start at the fork () return address of the parent process.

P->tss.eip = EIP;

2. The eax in TSS of the handle process is set to 0, and EAX is where the function return value is stored, which returns 0 in the process. Note that the subprocess does not perform the fork () function, the system stack of the child process does not operate, and there is certainly no fork function call like the parent process. But when the child process starts running, it's like it's returning from the fork.

P->tss.eax = 0;

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.