Creation and finalization of Linux------processes

Source: Internet
Author: User

Creation of the process:

Linux creates new processes in two steps: fork () and exec (). Where fork can create a copy of the current process (the parent process), the child process. The parent and child processes are only different from the PID. After that, there are two processes in the system that perform the same operation. The contents of the parent process are copied, but a write-time replication (copy on write) technique is used in Linux to make process creation more efficient. Next exec will read the executable file into the load address space to run. Such a process will be created!

The entry point of the fork system call is Sys_fork, which is related to the architecture, where the task is to extract the information provided by the user space from the processor's registers, and to call the architecture-independent do_fork () function for process replication. Then do_fork mainly do the following work to do the actual work of process generation:

1. Call the Copy_process replication process.

2. Get the PID of the new process

3. Wake the child process with wake_up_new_taskhuanxing and add its task_struct to the scheduler queue, but that does not mean that it can be executed immediately, but that the scheduler can select it to execute at this time.


The replication of the process includes the following steps:

1. Check the various identities, similar to CLONE_FS (shared file system with parent process) Wait, some combinations of identities are meaningless, so you need to check

2. Call Dup_task_struct to create a copy of the parent process task_struct. The task_struct process of a parent-child process is a different member of the core stack

3. Check the resource limits, because the number of processes that each user can have is limited, and the process is discarded to create

4. Call the interface function Sched_fork so that the scheduler can set up the new process. Prevent any other part of the kernel from scheduling processes before the process is set up

5. Invoke many copy_xyz-like routines to replicate and share various resources of the kernel subsystem

6. Setting individual IDs and process relationships

So the whole process will be copied over!


The implementation of EXEC (), which replaces the old code with new code, starts a new program.

The entry point for the system call is an architecture-related SYS_EXECVE function, but the function will soon be handed over to the system-independent DO_EXECVE routines, which are mainly doing the following:

1. Open the executable file

2. Call Bprm_init to handle several administrative transactions: generate the Mm_alloc management process address space, Init_new_context initialize the instance, __bprm_mm_init establish the initial stack

3. Call PREPARE_BINPRM to provide some related values for the parent process

4. Copy environment and parameter array contents

5. Call Search_binary_handler, at the end of Do_execve, to find an appropriate binary format for the file to be executed. The binary format handler is used to load the data from the new program into the old address space

In this way, a new program will start to execute!


The end of the process:

The exit () function is called at the end of the process, but as before, the process is eventually terminated by calling Do_exit (). Simply put, the individual reference counter is reduced by one, if the counter is 0, the corresponding memory is freed. Then call Exit_notify () to tell the parent process to find the adoptive father for the child process and set the process state to Exit_zombie. Finally call schedule () to switch to another process. The process will not be called again from this time. At this point the process consumes only memory stacks, thread_info, and Task_struct. When the parent process detects the information provided by the zombie process, the remaining memory held by the process is freed. This is the end 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.