Linux Job Six--description of the process and creation of the process

Source: Internet
Author: User
Tags error handling

Description of the process and creation of the process

One, Process descriptor task_struct

To manage the process, the kernel must have a clear description of each process, and the process descriptor provides the process information that the kernel needs to understand.

Code key points:

1.Struct list_head task process linked list, bidirectional loop link table connection.

2.Struct mm_struct *mm,*active_mm process address space, memory management

3. each process has its own independent 4G process address space.

4.Struct thread Struct Thread current task-related CPU code

5.Struct fs_struct *fs file system data structure

6.Struct files_struct *files Open file descriptor list

7.struct signal_stract *signal Signal Processing

Ii. creation of the process

A process is the ancestor of all user-state processes, and the second process is the ancestor of all kernel threads.

Fork,vfork , and clone Three system calls can create a new process, all by calling do_fork To implement the process creation;

1.fork system call, the user state is used to create a child process. (Part of the code below)

if (PID < 0) error handling

10 {

One/* error occurred */

fprintf (stderr, "Fork failed!");

Exit (-1);

14}

All else if (PID = = 0)

16 {

+/ * Child process */subprocess pid=0 if and else will execute the C7>fork system calls are returned once in the parent and child processes

printf ("This is a child process!\n");

19}

Else

21 {

*/* Parent Process */

printf ("This is the Parent process!\n");

*/* Parent'll wait for the child to complete*/

Wait (NULL);

-printf ("Child complete!\n");

27}

2. Understanding of the Code

The function performs for else and else if , that is , it becomes two processes after the fork , and the PID in the child process The return value is 0 , in the parent process PID The return value of the child process is the ID , Fork system calls are returned once in the child process and in the parent process.

Creating a process is accomplished by replicating the current process. It is broadly divided into three steps:

1. Duplicating the PCB(copyprocess)

2. Modify the PCB

3. Allocating a new kernel stack (partial data copies the parent process to return)

*childregs = *current_pt_regs (); Copy the kernel stack

Childregs->ax = 0; Why the fork of the subprocess returns 0, here is the reason!

P->THREAD.SP = (unsigned long) childregs; top of the kernel stack when dispatched to a child process

P->thread.ip = (unsigned long) ret_from_fork; the address of the first instruction when dispatched to a child process

3. Where does the child process start?

Childregs The kernel stack dispatched to the child process

Ret_from_fork The first instruction address dispatched to the child process

Syscall_exit

Restore_all normal Return to user state

Third, the experiment

Start tracking debugging after updating to the latest version:

Note here: Executing a fork, you will find that only a fork command description is output, and it is not executed because it is parked in the sys_clone position.

20135123 Qin

Linux Job Six--description of the process and creation of the process

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.