Linux Process Basics

Source: Internet
Author: User


What the basic computer of the Linux Process can actually do is actually very simple, such as calculating the sum of two numbers, and finding an address in the memory. These basic computer actions are called commands ). A program is a collection of such commands. Through the program, we can let the computer complete complex operations. Programs are mostly stored as executable files. Such an executable file is like a recipe. Computers can make delicious meals according to the recipe. Www.2cto.com, what is the difference between a program and a process? A process is a specific implementation of a program. Only when recipes are useless can we make dishes by following the instructions of recipes. A process is a process of executing a program. It is similar to a process of cooking by recipe. The same program can be executed multiple times, and each time it can open up an independent space in the memory for loading, thus generating multiple processes. Different processes can also have their own independent IO interfaces. An important feature of the Operating System is to provide convenience for processes, such as allocating memory space for processes and managing process-related information. It is like preparing a beautiful kitchen for us. 1. first, we can use the $ ps command to query running processes, such as $ ps-eo pid, comm, and cmd: www.2cto.com (-e Indicates listing all processes,-o pid, comm, cmd indicates we need PID, COMMAND, CMD Information)
Each line represents a process. Each row is divided into three columns. The first PID (process IDentity) is an integer. Each process has a unique PID to represent its own IDentity. The process can also identify other processes based on the PID. The second column of COMMAND is short for this process. The third column CMD is the program corresponding to the process and the parameters contained in the runtime. (The third column contains some brackets. They are part of the features of the kernel and are dressed as processes to facilitate operating system management. We don't have to consider them .) In the first line, the PID is 1 and the name is init. This process is generated by executing the/bin/init file (Program. When Linux is started, init is the first process created by the system. This process will exist until we close the computer. This process has special importance and we will keep mentioning it. 2. How to create a process in fact, when the computer is started, only one init process is established in the kernel. Linux kernel does not provide system calls that directly create new processes. All the remaining processes are established by the init process through the fork mechanism. The new process needs to be copied by the old process itself. This is fork. Fork is a system call. The process is alive in the memory. Each process has its own address space in the memory ). When the fork process is running, Linux creates a new memory space in the memory for the new process, and copies the content in the old process space to the new space, then the two processes run simultaneously. The old process becomes the parent process of the new process. Correspondingly, the new process is the child process of the old process ). In addition to a PID, a process also has a PPID (parent PID) to store the parent process PID. If we keep tracing the PPID, we will always find that the source is the init process. Therefore, all processes also constitute a tree structure with init as the root. Query the processes in the current shell as follows: root @ vamei :~ # Ps-o pid, ppid, cmd pid ppid 201716935 3101 sudo-i16939 16935-bash23774 16939 ps-o pid, ppid, cmd www.2cto.com we can see, the second process bash is the sub-process of the first process sudo, and the third process ps is the sub-process of the second process. Fork is usually called as a function. This function returns the child process PID to the parent process twice, and returns 0 to the child process. In fact, a child process can always query its own PPID to know who its parent process is. In this way, a pair of parent and child processes can query each other at any time. After the fork function is called, the program will design an if selection structure. When the PID is equal to 0, it indicates that the process is a sub-process, so let it execute some commands, for example, using the exec library function to read a program file, execute in the space of the current process (this is actually a major purpose of using fork: to create a process for the Program). When the PID is not 0, it indicates that it is the parent process, then execute some other commands. Therefore, after the child process is created, it can execute different functions from the parent process. 3. termination of a child process when the child process ends, it notifies the parent process and clears the memory occupied by it, and leave your exit code in the kernel ). In this information, it explains why the process exits. The parent process has the responsibility to use the wait system to call the child process when it learns that the child process is terminated (from the programmer's point of view, it is a function ). This wait function can retrieve the exit information of the sub-process from the kernel and clear the space occupied by this information in the kernel. However, if the parent process is terminated earlier than the child process, the child process will become an orphan (orphand) process. The orphan process is passed to the init process, and the init process becomes the parent process of the process. The init process is responsible for calling the wait function when the child process ends. Www.2cto.com, of course, a bad program may also cause the exit information of the child process to remain in the kernel (the parent process does not call the wait function for the child process). In this case, sub-processes become zombie processes. When a large number of zombie processes accumulate, the memory space will be occupied. 4. Process and thread although in UNIX, the process and thread are two different things, but in Linux, the thread is just a special process. Multiple Threads can share memory space and I/O interfaces. Therefore, processes are the only Implementation Method of Linux programs. Summary: Program, process, PID, memory space sub-process, parent process, PPID, fork, wait address: http://www.cnblogs.com/vamei/archive/2012/09/20/2694466.html
 

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.