Processes in Linux

Source: Internet
Author: User

Said process before the first to say a procedure, I believe everyone knows what is a program, the program is an executable file, is a collection of instructions. The procedure is relatively static. And the program that runs is the process, is dynamic, is the procedure execution process. The program can run many times, such as QQ can start more than one, but each will have a separate space in memory for loading program code and data.


We can view the currently running processes in the Linux system via the PS command

Ps-aux

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/89/B6/wKiom1gaosLw7WIXAACiReMMKes324.jpg-wh_500x0-wm_3 -wmp_4-s_4231471388.jpg "title=" 1.JPG "alt=" Wkiom1gaoslw7wixaaciremmkes324.jpg-wh_50 "/>

For specific fields, see monitoring commands for Linux systems

Each line in this is a process, the PID is the process number, followed by the command this is the specific process or the program name. The first line is always the PID 1 system init process, this is the boot kernel created, it will run until shutdown, at the beginning of its production is in the kernel state of operation, and then through the system call, execute the/sbin/init program, from the kernel state to switch to the user state. All subsequent user processes are derived from this process, which is the parent process of all user processes, meaning that the Linux kernel does not directly establish user processes.

How does the user run the program into a process? It's actually the INIT process that copies itself by calling the fork function and then goes to the program that exec eventually executes.


About Fork:

The function of fork is to copy a process like itself, the variables and parameters of the new process are the same as the original, but it is a completely new process, and as a child process of the original process.

However, it is common to copy yourself and execute the EXEC function collection immediately, calling the executable to replace the process content that called the executing program, which results in a waste of the original data being copied. Therefore, when the fork is copied using the write-time replication technology, that is, the new child process and the parent process has a separate virtual memory address, but this virtual address point to the same physical address, which ensures that they have a separate logical space, if the child process needs to modify the data, in the allocation of independent physical space, Then copy the data in the past. However, it is important to note that it is not all that is used to replicate, such as virtual address space structure (mm structure), Parent Process page table information is actually copied past, because any process is an instance of the TASK_STRUCT structure.

So fork is to copy a process that is the same as yourself and exist as a child process, and then the subroutine calls the program that really needs to be executed. This will use the EXEC function set.

Another is called vfork, as long as the memory-related things are not replicated, parent-child process memory is fully shared. To avoid working together on the same stack, when a child process is generated, the parent process is suspended until the child process calls the EXEC function and has its own independent space or the child process exits. If you use Vfork and then execute exec immediately, it will be more efficient than fork. Compared to fork, Vfork saves the most cost of a copy of the page table.

Description: The process is an instance of the TASK_STRUCT data structure, also known as the process descriptor, which records the context of the process, which has a data structure called a Memory descriptor (MM_STRUCT) that describes all the information about the process address space, including code snippets, data segments, and so on. Each process has its own independent mm_struct. Even if the fork is a process, the child process has a separate task_struct and has a separate mm_struct, but its virtual address space is mapped to the physical address space of the parent process, if vfork is not used at all, father and son use the same virtual address space, Of course, the same physical address space is shared.


About the EXEC function collection:

The function of the EXEC function collection is to find the executable file according to the specified file name (using the system environment variable or to receive a passed environment variable) and replace the calling process (the calling process is the process that invokes the EXEC function set, and understands the child process mentioned above).

The EXEC function does not return information after successful execution, because the entity that invokes the process (child process), including code, data, and stack, is present, only replaced by the new executable, only the process ID is the same as the original (the child process and the parent process ID are different, Fork a process will assign a separate ID number for it, you can understand to put a program into a child process, so it is not a completely new creation, but if the failure of execution will return-1.


The EXEC function collection, as follows:

ARG as list parameter

int execl (const char *path, const char *arg, ...); Path is the full path to the executable program

int EXECLP (const char *file, const char *arg, ...); File is the filename of the executable program, either path or file will automatically use the system current environment variable

int execle (const char *path, const char *arg, ..., char * const envp[]); ENVP is to receive the custom environment variable to find the executable file

ARG is an array parameter

int execv (const char *path, char *const argv[]);

int EXECVP (const char *file, char *const argv[]);

int Execve (const char *path, char *const argv[], char *const envp[]); ENVP is to receive the custom environment variable to find the executable file

Only the execve is really going to be executed, and the others are in different packages because the parameters passed are different, but they will eventually be executed Execve.

Again, when the child process exits, the parent process is notified, the parent process cleans up its own memory space and leaves its own exit information in the kernel, if the code is 0 if the exit is normal, if it is not normal, it is an integer greater than 0. The parent process calls the wait function to clean up the memory space used by the child process, and can get the exit information to the child process. If the child process does not exit, and the parent process exits? Then the child process is taken over by the Init process and becomes the new parent process of the child process, and the parent process that was previously exited is a child of the INIT process, so init calls the wait function to clean up the memory it uses and then gets the exit information.

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/89/B8/wKioL1ga3azhKqwjAACJ_mxkizI407.jpg-wh_500x0-wm_3 -wmp_4-s_3415067929.jpg "title=" 4.JPG "alt=" Wkiol1ga3azhkqwjaacj_mxkizi407.jpg-wh_50 "/>

From the tree structure we can see the process of init at the top level.


Is there a thread concept in Linux? If the thread you understand is the kind of thread under the Windows platform, it's not in Linux. In fact, Linux is a process, and without a real thread, its thread is actually a special process. Fork out the process you can also say that it is a thread, but it is more lightweight than the parent process. What does the line libraries in Linux do? That's just for simulating threading.

This article is from the "Little Demon's Home" blog, so be sure to keep this source http://littledevil.blog.51cto.com/9445436/1868967

Processes in Linux

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.