Understanding of the Linux process

Source: Internet
Author: User
Tags terminates unique id

  1. Description of the process
    In layman's words, a process is a program or code that is being executed. We know that the program itself is a bunch of code, at the beginning of the storage on disk, it is static, lifeless, only when the code of the program is loaded into memory, the code has a life, can be CPU dynamic execution.
    The problem is that the current operating system can execute multiple programs in parallel, that is, the memory of multiple programs stored in the code, in order to facilitate management, it is necessary to organize them reasonably. The way is by the operating system to each piece of code to add some metadata, these metadata is the PCB, that is, the task control block.
    It is not difficult to understand that the code of each program can actually be divided into two parts: the data of the instruction. Instructions are the various operations prescribed by the program code, and the data is the object of those operations. A program can be loaded into memory multiple times to become more than one process, such as the simultaneous opening of two Vim separately editing different files. So the question is: is it necessary to store multiple copy of the program in memory at this time? The answer is no. The instruction section is set to read-only and allows the code to be shared between two or more processes running in the system, while the data part is private to the individual processes and cannot be shared, for example, each vim can only edit its own files.
    So what's inside the PCB?
    • The process ID. Each process in the system has a unique ID, represented in the C language by the pid_t type, which is actually a nonnegative integer.
    • The status of the process, there are running, hang, stop, zombies and other states.
    • Some CPU registers that need to be saved and restored during process switching.
    • Describes the virtual address space information.
    • Describes the control terminal information.
    • Current working directory (working directory).
    • Umask Mask.
    • A file descriptor chart that contains a number of pointers that point to file structures.
    • and signal-related information.
    • User ID and group ID.
    • Control terminal, session, and process groups.
    • The resource cap (Resource limit) that the process can use.
      Visible, operating system in order to control the process, the structure of the PCB is quite complex.
  2. process creation
    often hear about "creating a process", what is going on? The first thing you can think of is that the process is not the Sun monkey, can not jump out of their own, it must be someone else "born". In Linux, the process is created by the parent process, which, to be precise, is the instruction part of the code in the parent process actively uses the function fork () that created the process, and then a child process is "born". How does the fork function work? Since each process has a PCB, it is first to request a PCB with the operating system (the PCB is limited), then allocate the new process memory, and then copy the parent process code. In fact, fork is lazy to copy the parent process, that is, in the fork function call process, there will be two almost identical process in memory, of course, except for the process number (it is unique). After the replication of the process is complete, two processes have a fork function waiting to return ( note, is returned, because the fork function itself is a code of one, the previous part of the completion of the copy function, the child process appears after the return of the part of the Code ), The return result is different (the operating system controls the return result): The fork in the parent process returns the PID of the child process, and the fork in the child process returns 0, or 1 if the fork fails.
    Fork just created two almost the same process, they run the same code, which is different from the beginning, because we create a new process is mostly used to execute the new program code. At this point we need the Exec class function, when the process calls an EXEC function, the program code of the process is completely replaced by the new program, the start of the new program start routine execution. Calling exec does not create a new process, so the ID of the process before and after calling exec has not changed. If the call succeeds, the new program is loaded starting from the startup code, is no longer returned, and returns 1 if an error is called, so the EXEC function has only the return value of the error and no successful return value. The exec system calls the command-line arguments and the environment variable table to the main function when executing a new program. The environment variable table is a description of the system environment of the process, and the environment variable table is an abstraction of the system resources which must be used for the normal execution of the code. However, the Exec class function needs to be explicitly called, and the child process does not actively load the new program code! So, typically in the parent process's code, a branch is written based on the return value of the fork, and exec is explicitly called in the branch of the child process.
  3. process Termination (reference: Linux C one-stop programming)
    When a process terminates, it closes all file descriptors, frees the memory allocated in the user space, but its PCB remains, and the operating system holds some information in it: if it is normal termination, the exit status is saved. If it is an abnormal termination, it holds the signal that caused the process to terminate. The parent process of this process can call wait or waitpid to get the information and then completely erase the process. We know the exit status of a process can be used in the shell with a special variable $? View, because the shell is its parent process, and when it terminates, the shell calls wait or waitpid to get its exit state while completely erasing the process.
    If a process has been terminated, but its parent process has not yet called wait or waitpid to clean it, the process state is called the Zombie (Zombie) process. Any process that just terminates is a zombie process, and normally the zombie process is immediately cleaned up by the parent process.
    If a parent process terminates and its child processes are either still running or is already a zombie process, the parent process of those child processes is changed to the init process. Init is a special process in the system, usually the program file is/sbin/init, the process ID is 1, when the system starts to start a variety of system services, then responsible for cleaning up the child process, as long as there is a child process termination, INIT will call the wait function to clean it. The
    Zombie process cannot be purged with the kill command, because the KILL command is used only to terminate the process, and the zombie process is terminated. So one possible way is to kill the parent process. The prototype for the
    wait and Waitpid functions is:
#include <sys/types.h>#include <sys/wait.h>wait(int*statuswaitpidint*statusint options);

If the call succeeds, the cleared child process ID is returned, and 1 is returned if an error is called. When a parent process calls wait or waitpid, it may:
+ Block (if all of its child processes are still running).
+ The termination information of the tape process is returned immediately (if a child process is terminated, waiting for the parent process to read its termination information).
+ Error immediately returns (if it does not have any child processes).

Understanding of the Linux process

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.