Linux Process Management Analysis

Source: Internet
Author: User
Article Title: Linux operating system process management analysis. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Linux? How does one create and manage a user-space process with UNIX? There are many things in common, but there are also some unique characteristics specific to Linux. In this article, we will learn about the Linux Process lifecycle and explore the kernel details of user Process Creation, memory management, scheduling, and destruction.

Linux is a dynamic system that can adapt to changing computing needs. Linux computing requirements are centered on the general abstraction of processes. A process can be short-lived (a command executed from a command line) or long-lived (a network service ). Therefore, it is extremely important to manage processes and their scheduling.

In user space, a process is represented by a PID. From the user's point of view, a PID is a numerical value that uniquely identifies a process. A pid will not be changed throughout the life of the process, but it can be reused after the process is destroyed, so it is not always ideal to cache them.

In user space, you can create a process in several ways. You can execute a program (which leads to the creation of a new process) or call a fork or exec system call in the program. Fork call will lead to the creation of a sub-process, while exec call will replace the current process context with a new program. Next, I will discuss these methods so that you can better understand their working principles.

In this article, I will introduce processes in the following sequence. First, I will show the kernel representation of processes and how they are managed in the inner core, then let's take a look at various methods of Process Creation and scheduling (on one or more processors), and finally introduce the destruction of processes.

Process representation

In the Linux kernel, processes are represented by a large structure called task_struct. This structure contains all the data required for this process. In addition, it contains a large amount of other data for statistics and maintenance of the relationship with other processes (Parent and Child ). The complete introduction to task_struct is beyond the scope of this article. Listing 1 provides a small part of task_struct. The Code contains the specific elements to be explored in this article. Task_struct is located in./linux/include/linux/sched. h.


Listing 1. A small part of task_struct

Struct task_struct {

Volatile long state;
Void * stack;
Unsigned int flags;

Int prio, static_prio;

Struct list_head tasks;

Struct mm_struct * mm, * active_mm;

Pid_t pid;
Pid_t tgid;

Struct task_struct * real_parent;

Char comm [TASK_COMM_LEN];

Struct thread_struct thread;

Struct files_struct * files;

...

};

In listing 1, we can see several expected items, such as the execution status, stack, a set of signs, parent processes, execution threads (which can be many), and open files. I will describe it in detail later. Here is a brief introduction. The state variable is a bit indicating the task status. The most common statuses are: TASK_RUNNING indicates that the process is running or is running in the running queue; TASK_INTERRUPTIBLE indicates that the process is sleeping; TASK_UNINTERRUPTIBLE indicates that the process is sleeping but cannot be woken up; TASK_STOPPED indicates that the process is stopped. The complete list of these flags can be found in./linux/include/linux/sched. h.

Flags defines many indicators to indicate whether the process is being created (PF_STARTING) or exited (PF_EXITING), or whether the process is currently allocating memory (PF_MEMALLOC ). The name of the executable program (excluding the path) occupies the comm (command) field.

Each process is given a priority (called static_prio), but the actual priority of the process is dynamically determined based on loading and other factors. The lower the priority value, the higher the actual priority.

The tasks field provides the link list capability. It contains a prev pointer (a forward task) and a next pointer (pointing to the next task ).

The address space of a process is represented by the mm and active_mm fields. Mm represents the memory descriptor of the process, while active_mm represents the memory descriptor of the previous process (an optimization to improve the context switching time ).

Thread_struct is used to identify the storage status of the process. This element depends on the specific architecture on which Linux runs, and has such an example in./linux/include/asm-i386/processor. h. In this structure, you can find the storage (hardware registry, program counters, etc.) after the process performs context switching ).

[1] [2] [3] Next page

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.