Linux kernel--process management

Source: Internet
Author: User

A process is a program that handles the execution period (the target code is stored on a storage medium). The process is not limited to an executable code. Typically processes include:

  • Open a file
  • Suspended signal
  • Kernel internal data
  • Processor status
  • Address space
  • One or more execution threads
  • Data segments that hold global variables

For Linux, threads are special processes and are not specifically differentiated. In modern operating systems, processes provide two virtual mechanisms: virtual processors and virtual memory. While it may actually be that many processes are sharing the same processor, the virtual processor gives the process a false impression that the process feels like it is in exclusive processor. Virtual memory lets the process feel that it has all the memory resources of the entire system when it acquires and uses memory. Multiple threads in the same process can share virtual memory, but have their own virtual processors.

Process descriptors and task structures

The kernel stores the process in a two-way circular list called the task queue. Each item in the list is a structure of type task_struct, called the process descriptor, and the process descriptor contains all the information for a specific process. The data contained in the process descriptor fully describes an executing program: The file It opens, the address space of the process, the pending signal, the status of the process, and so on.

Process Descriptor Structure diagram:

650) this.width=650; "title=" Process Descriptor "style=" border-top:0px; border-right:0px; border-bottom:0px; border-left:0px; Display:inline "border=" 0 "alt=" process Descriptor "src=" Http://img1.51cto.com/attachment/201409/7/817917_1410058281OJzq.jpg "" 394 "height=" 342 "/>

The kernel identifies each process by a unique process identity value (process identification value) or PID. The kernel stores the PID of each process in their respective process descriptors. The default PID maximum for Linux is 32768, and you can increase the upper limit by modifying the kernel parameter Kernel.pid_max = 65535来.

The state domain in the process descriptor describes the current status of the process, and each process in the system must handle one of the 5 process states:

    • Task_running (Run): The process is executable, it is either executing, or waiting to be executed in the run queue. This is the only possible state that the process executes in the user space, or it can be applied to the process being executed in the kernel space
    • Task_interruptible (interruptible): The process is sleeping (that is, it is blocked), waiting for certain conditions to be reached. Once these conditions are reached, the kernel should set the process state to run. Processes that are in some state will also be woken up and put into operation because they receive signals.
    • Task_uninterruptible (non-disruptive): This state is the same as the interruptible state, except that it does not go into operation because it receives a signal and then wakes up. This state usually occurs when the process must wait without interference or wait for events to occur soon.
    • Task_zombie (Zombie): The process has ended, but its parent process has not yet called the WAIT4 () system call, and the process descriptor for the child process is still preserved in order for the parent process to be able to learn its message. Once the parent process calls WAIT4 (), the process descriptor is freed.
    • Task_stopped (STOP): The process stops executing and the process is not operational or operational. Usually this state occurs when a signal such as Sigstop, SIGTSTP, Sigtin, Sigttou is received. In addition, any signal received during debugging will cause the process to enter this state.

State transition diagram for the process:

650) this.width=650; The state transition of the "title=" process "style=" border-top:0px; border-right:0px; border-bottom:0px; border-left:0px; Display:inline "border=" 0 "alt=" process state transitions "src=" http://img1.51cto.com/attachment/201409/7/817917_1410058282Wqh6.jpg "" 642 "height=" 525 "/>

There is a significant inheritance between Linux system processes, all processes are the descendants of the PID 1 init process, the kernel starts the INIT process in the final stage of system startup, the process reads the initialization script of the system and executes other related programs, and finally completes the system startup process. Each process in the system must have a parent process and, accordingly, each process can have 0 or more child processes. The relationship between processes is stored in the process descriptor, and each task_struct contains a pointer to its parent process task_struct, called parent, and a list of child processes called children.

Process creation

Linux process creation uses two function fork () and exec () to complete. First, fork () creates a child process by copying the current process. The difference between a child process and a parent process is only the PID (unique per process), PPID (the process number of the parent process), and some resources and statistics. The exec () function is responsible for reading the executable file and loading it into the address space to start running.

Traditional fork () system calls replicate all resources directly to the newly created process, and the implementation is too simple and inefficient because the data it copies may not be shared, and if the new process intends to execute a new image immediately, all copies will be wasted. The Linux Fork () is implemented using a write-time copy (Copy-on-write). Write-time copying is a technique that can postpone or even eliminate copy data. The kernel does not replicate the entire address space at this time, but instead allows the parent process and child processes to share the same copy at the same time. Data is copied only when it needs to be written, so that each process has its own copy. That is, resources are replicated only when they need to be written, and before that, they are only shared in read-only mode. This technique enables a copy of the page 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: calling exec () immediately after fork (), the actual cost of fork () is to copy the page table of the parent process and create a unique process descriptor for the child process. In general, a process can run an executable file immediately after it is created, which avoids copying large amounts of data that is not used at all.

The implementation of threads in Linux

The threading mechanism provides a set of threads that run within the same program shared shared memory address space. These threads can also share open files and other resources. The threading mechanism supports concurrent programming techniques, and it also guarantees true parallel processing on multiprocessor systems. Linux implements all threads as processes, and threads are only considered a process that shares certain resources with other processes. Each thread has its own task_struct, so in the kernel it looks like a normal process.

For Linux, a thread is simply a means of sharing resources between processes, and if there is a process with 4 threads, there will usually be a process descriptor that contains pointers to 4 different thread processes. This descriptor is responsible for describing shared resources such as address space, open files, and so on.

Process End

When a process is terminated, the kernel must release the resources it occupies and inform its parent process of this misfortune. When the parent process exits before the child process, there must be a mechanism to ensure that the child process finds a new parent, otherwise these orphaned processes will always dispose of the zombie state at the time of exiting and consume memory in vain. For this problem, the workaround is to find a thread in the current thread group as the parent row for the child process, and if not, let the INIT process act as their father.

Linux kernel--process management

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.