Linux Process Basics (i)

Source: Internet
Author: User

What the computer actually does is simple, that is, through the execution of the program, complete the user-specified series of operations. This process we can in analogy, the computer is a kitchen, the program is a menu, a la carte window is an input, the process of the middle program is the computer to stir fry, and the program to complete the results presented to the user when the program runs the output, that is, the waiter will fry a good dish to customers.

So what is the difference between a process and a program? Obviously the program is a collection of code stored on the hard disk, similar to the menu in the metaphor above, and the process is the running program, which is mainly the memory of the transport activity space. The same program can be executed multiple times, resulting in multiple processes, different processes can have their own required running resources, such as IO read-write interface, address space, which also indicates that the process is not sharing data between them, but they can communicate between the processes. Combined with the actual situation, the process of the computer can be divided into the system process and user process, the system process is the process generated by the system program, the user process is the process of user program running process, it can be understood, the system process provides the user process to run the "material support", similar to the former metaphor of the kitchen role.

The focus of the process is on the creation of processes, the demise of processes, the state of processes, the communication between processes, and the scheduling of processes.

I. Creation of processes

Actually, When the computer is powered on, the kernel (kernel) only establishes an INIT process. Linux kernel does not provide a system call to directly establish a new process. All remaining processes of are created by the Init process through the fork mechanism. The new process is going through the old process copy itself get, this is fork Fork is a system call . The process survives in memory. Each process allocates its own piece of space in memory (address space). When the process is fork, Linux opens up a new memory space in memory to the new process, and copies the contents of the old process space into the new space, after which two processes run concurrently.
The old process becomes the parent process of the new process, and, accordingly, the new process is the child process of the old process. A process In addition to a PID, there will also be a PPID(parent PID) to store the parent process PID. If we follow the ppid, we will always find that its source is the init process. Therefore, all processes also constitute a tree-like structure with the root of init. Fork is usually called as a function. This function returns two times , returns the PID of the child process to the parent process, and 0 returns to the child process. In fact, child processes can always query their own ppid to know who their parent process is, so that a pair of parent processes and child processes can query each other at any time.

Ii. the demise of the process

When the child process is terminated, it notifies the parent process and empties the memory it occupies and leaves its own exit message in the kernel (exit code, 0 if it runs smoothly, or an integer that is >0 if there is an error or an exception condition). In this message, it explains why the process exited. When the parent process learns that the child process is terminated, it is the responsibility to use the wait system call for that child process. This wait function takes out the exit information of the child process from the kernel and empties the space occupied by the information in kernel. However, if the parent process is older than the child process end, the child process becomes an orphan (orphand) process. The orphan process is passed on to the Init process, and the Init process becomes the parent process of the process. The INIT process calls the wait function when the child process is terminated.
Of course, a bad program can also completely cause the exit information of the child process to remain in the kernel (the parent process does not call the wait function on the child process), in which case the child process becomes a zombie (zombie) process. When a large number of zombie processes accumulate, the memory space is squeezed out.

This article from "Simple New Life" blog, declined reprint!

Linux Process Basics (i)

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.