Creation and description of the process

Source: Internet
Author: User

1. Concept of the process

A process is a program that is in the execution period (the target code is stored on a storage medium). But the process is not confined to just a

Executable code (Unix calls it a code snippet, text section). Typically processes also include other resources, such as open files, pending signals, kernel internal data, processor state, one or more memory-mapped address spaces and one or more thread of execution, and, of course, data segments to hold global variables. In fact, a process is a real-time result of the program code being executed. The kernel needs to manage all the details efficiently and transparently.

2 process descriptors and task structures

Atruct T Hread_info {

struct TASK_STRUCT task;

struct Exec_domain exec_d. Main

_ U32 Flags;

_ U32 status;

_U32 CPU;

int Preempt_ count;

mm_segment_t Addr_limit;

struct restart BL. CK restart BL. ck

V. ID Sysenter_return;

int Uaccess_err;}

3. Storage of process descriptors

The kernel identifies each process by a unique process identity value (process identification value) or PID. The PID is a number, expressed as pid_t implied type θ, which is actually a four type. In order to be compatible with older versions of UNIX and Linux, the maximum PID value is set to 32768 (the maximum value of the SH-mortar t int short integer) By default, although this value can also be increased to a limit of the maximum PID value defined in up to 4 million 〈<linux/reads.h>. The kernel stores the PID of each process in their respective process descriptors.

4 Process Creation

4.1 write-time copy

Data is copied only when it needs to be written, so that each process has its own copy. In other words, the capital

The source's replication only takes place when it needs to be written, and before that, it is shared only in read-only mode. This technique allows copies of pages on the address space to be deferred until the actual write occurs. In the case where the page is not written at all (for example, when you call exec () immediately after fork (), they do not need to be duplicated. The actual cost of fork () is to copy the page table of the parent process to give the process a unique process descriptor. In general, an executable file is run immediately after the process is created, which avoids copying a large amount of data that is not used at all, often containing dozens of of her data in the address space. This optimization is important because UNIX emphasizes the ability of the process to execute quickly.

4.2 fork ()

Linux implements Fork () through clone () system calls. This call indicates the resources that the parent and child processes need to share through a series of parameter flags (for more information on these flags, refer to section 3.4 later in this chapter). The fork (), vfork (), and a clone () library function call Clone () according to the required parameters, and then call Do_fork () by Clone (). Do_fork completed most of the work in the creation, which is defined in KEMEVFORK.C File. The function calls the Copy_process () function and then lets the process start running. The work done by the copy_process () function is interesting:

L) Call the Dup_task_ struct () to create a kernel branch, read_info structure, and task_struct for the new process, which are the same values as the current process. At this point, the child process and the parent process descriptor are exactly the same.

2) Check to make sure that the number of processes currently owned by the current user does not exceed the paint-allocated resources after the new child process is created

The restrictions.

3) The subprocess distinguishes itself from the parent process. Many members within the process descriptor are cleared 0 or set to the initial value. The process descriptor members, which are not inherited, are primarily statistical. Most of the data in Task_struct is still unmodified.

4) The status of the child process is set to task_unjnterruptible to ensure that it is not put into operation.

5) Copy _process () calls Copy_flags () to update Task_struct's group AGS members. The PF_SUPE flag that indicates whether the process has superuser privileges is cleared by the Riv 0. Indicates that the process has not yet called the EXEC () function's pf_for. The KNOEXEC flag is set.

6) Call Alloc _pid () to assign a valid PID to the new process.

7) copy_process () copies or shares open files, file system information, signal processing functions, process address space, and namespaces, depending on the parameter flags passed to clone (). In general, these resources are shared by all threads of a given process: otherwise, these resources are different for each process and are therefore copied here. Finally, Copy_process () is proud of the work and returns a pointer to the child process. Back to the Do_fork () function, if the copy_process () function returns successfully, the newly created child process is awakened and put into operation. The kernel intentionally chooses the child process to execute first: Because the normal child process calls the Exec () function immediately, this avoids the extra overhead of copy-on-write, and may start writing to the address space if the parent process executes first.

Summary : In this chapter, we examine the process of core concepts in the operating system. We also discussed the general nature of the process, why it is so important, and the relationship between processes and threads. Then, we discuss how Linux stores and represents processes (with task_ struct and thread_info), how to create a process (via fork (), essentially clone ()), and how to load a new execution image into the address space (through the EXECO system call family). How to represent the hierarchical relationship of a process, how the parent process collects information about its descendants (through the wait () system call family), and how the process eventually dies out (forced or voluntary call to exit ()). The process is a very basic, very critical abstraction, located at the heart of every modern operating system, and the ultimate reason we have an operating system (to run programs).

Creation and description 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: 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.