Linux Process Management profiling (creation, management, scheduling, and destruction)

Source: Internet
Author: User

Linux is a dynamic system that can adapt to changing computing needs. The demand for Linux computing is as follows:Process. 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 (this will lead to the creation of a new process) or callforkOrexecSystem Call.forkA sub-process is createdexecThe new program replaces the context of the current process. 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.

1. process representation

In the Linux kernel, a process is calledtask_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 ). Pairtask_structThe complete introduction of is out of the scope of this article, and listing 1 showstask_struct. The Code contains the specific elements to be explored in this article.task_structLocated 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.stateA variable is a bit indicating the task status. The most common statuses are:TASK_RUNNINGIndicates that the process is running or is running in the running queue;TASK_INTERRUPTIBLEIndicates that the process is sleeping,TASK_UNINTERRUPTIBLEIndicates that the process is sleeping but cannot be woken up;TASK_STOPPEDIndicates that the process is stopped. The complete list of these flags can be found in./linux/include/linux/sched. h.

flagsMany indicators are defined to indicate whether the process is being created (PF_STARTING) Or exit (PF_EXITING), Or whether the process is currently allocating memory (PF_MEMALLOC). The name of the executable program (excluding the path) occupiescomm(Command) field.

Each process is given a priority (calledstatic_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.

tasksFields provide the link list capability. It containsprevPointer (refers to the previous Task) andnextPointer (pointing to the next task ).

The address space of a process ismmAndactive_mmField representation.mmRepresents the memory descriptor of the process, andactive_mmIt is the memory descriptor of the previous process (an optimization to improve the context switching time ).

thread_structIs 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 ).

2. Process Management
In Linux, the maximum number of processes is dynamically allocated, but you still need to consider the maximum number of processes. The maximum number of processes in the inner kernel is represented by a symbol called max_threads, which can be found in./linux/kernel/fork. c. You can change this value from the user space through the/proc/sys/kernel/threads-max proc file system.


Now let's take a look at how to manage processes in Linux. In many cases, processes are dynamically created and allocated by a dynamictask_struct. One exception isinitProcess itself, which always exists and is statically allocated bytask_struct. This example can be found in./linux/arch/i386/kernel/init_task.c.

There are two ways to allocate all processes in Linux. The first method is to use a hash table, which is obtained by hashing the PID value. The second method is to use a double-chain loop table. The cyclic table is very suitable for iteration of the task list. The list is cyclic and has no header or tail.init_taskIt always exists, so it can be used as an anchor for further iteration. Let's look at an example of traversing the current task set.

The task list cannot be accessed from the user space, but this problem can be easily solved by inserting code into the kernel in the form of modules. As shown in Listing 2, a simple program iterates the task list and provides a small amount of information about each task (name,pidAndparentName ). Note: Here, this module usesprintk. To view the specific results, you can usecatUtility (or real-timetail -f /var/log/messages) View the/var/log/messages file.next_taskA function is a macro in sched. h. It simplifies the iteration of the task list (returnstask_structReference ).

Listing 2. Simple kernel module (procsview. c) that sends task information)

#include <linux/kernel.h>#include <linux/module.h>#include <linux/sched.h>int init_module( void ){  /* Set up the anchor point */  struct task_struct *task = &init_task;  /* Walk through the task list, until we hit the init_task again */  do {    printk( KERN_INFO "*** %s [%d] parent %s\n",task->comm, task->pid, task->parent->comm );  } while ( (task = next_task(task)) != &init_task );  return 0;}void cleanup_module( void ){  return;}


You can use Makefile in listing 3 to compile this module. You can useinsmod procsview.koInsert a module object. You can also usermmod procsviewDelete it.

Listing 3. Makefile used to build the kernel module

obj-m += procsview.oKDIR := /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules


After insertion,/var/log/messages can display the output, as shown below. We can see that there is an idle task (calledswapper) AndinitTask (Pid 1).

Nov 12 22:19:51 mtj-desktop kernel: [8503.873310] *** swapper [0] parent swapperNov 12 22:19:51 mtj-desktop kernel: [8503.904182] *** init [1] parent swapperNov 12 22:19:51 mtj-desktop kernel: [8503.904215] *** kthreadd [2] parent swapperNov 12 22:19:51 mtj-desktop kernel: [8503.904233] *** migration/0 [3] parent kthreadd...

Note: You can also identify the currently running task. Linux maintenance is called currentRepresents the currently running process (type: task_struct). If init_moduleInsert the following line of code at the end:

printk( KERN_INFO, "Current task is %s [%d], current->comm, current->pid );

You will see:

Nov 12 22:48:45 mtj-desktop kernel: [10233.323662] Current task is insmod [6538]

Note that the current task isinsmodThis is becauseinit_moduleThe function is ininsmodCommand Execution context.currentA symbol actually refers to a function (get_current) And can be found in an arch-related header (for example, within./linux/include/asm-i386/current. h ).

3. Process Creation

You may have seen the system call mode when calling a function. In many cases, system calls are named sys _ * and some initial functions are provided to implement calls (such as error checks or user space behaviors ). The actual work is often delegated to another function named do.

Let's take a look at how to create a process from the user space. The underlying mechanism of user space tasks and kernel tasks is the same, because the two will eventually depend on do_forkTo create a new process. When creating a kernel thread, the kernel calls kernel_thread(See./linux/arch/i386/kernel/process. c ). do_fork.

The process for creating a user space is similar. In a user space, a program callsfork, This causessys_fork(See./linux/arch/i386/kernel/process. c ). Function Relationship 1 is shown.

Figure 1. Hierarchy of functions responsible for creating processes

As shown in figure 1do_forkIs the basis for Process Creation. You can find it in./linux/kernel/fork. c.do_forkFunctions (and cooperative functions)copy_process).

do_forkThe function is called first.alloc_pidmap, A New PID will be allocated for this call. Next,do_forkCheck whether the debugger is tracking the parent process. If yesclone_flagsInternal settingsCLONE_PTRACETo prepare for the fork operation. Afterdo_forkThe function also callscopy_processTo pass the flag, stack, registry, parent process, and the latest assigned PID.

The new processcopy_processThe function is created as a copy of the parent process. This function can perform all operations except the startup process. The startup process is processed later.copy_processThe first step is to verifyCLONEFlag to ensure that these marks are consistent. If they are inconsistent, the system returnsEINVALError. Next, ask Linux Security Module (LSM) to check whether a new task can be created for the current task.

Next, calldup_task_structFunction (in./linux/kernel/fork. c), this will assign a newtask_structAnd copy the descriptor of the current process to it. After the new thread stack is set, some status information will be initialized and the control will be returnedcopy_process. Control backcopy_processIn addition to several other restrictions and security checks, some general management will be performed, includingtask_struct. Then, a series of replication functions will be called to copy all aspects of the process, such as copying open file descriptors (copy_files), Copy the Symbol Information (copy_sighandAndcopy_signal), Replication process memory (copy_mm) And the final replication thread (copy_thread).

Then, the new task is assigned to a handler and additional checks are performed on the handlers that are allowed to execute the process (cpus_allowed). After the priority of a new process is inherited from the parent process, a small amount of additional general management is executed, and the control is also returneddo_fork. At this time, the new process exists but is not yet running.do_forkFunction callwake_up_new_taskTo fix this problem. This function (available in. /linux/kernel/sched. c) initialize the general management information of some scheduling programs, place new processes in the running queue, and then wake them up for execution. Finally, oncedo_forkThe PID value is returned to the calling program and the process is completed.

4. Process Scheduling

Processes that exist in Linux can also be scheduled through the Linux scheduler. Although the scheduler is beyond the scope of this article, the Linux scheduler maintains a list for each priority, which savestask_structReference. Task passedscheduleFunction (in./linux/kernel/sched. c), which determines the best process based on the loading and process execution history.

5. Process destruction

Process destruction can be driven by several events-through normal processes, through signals, or throughexitFunction call. No matter how the process exits, the process must end with the kernel functiondo_exit(In./linux/kernel/exit. c. This process is shown in step 2.

Figure 2. Hierarchy of functions for process destruction

do_exitThe purpose is to delete all references to the current process from the operating system (for all resources that are not shared ). You must first setPF_EXITINGIndicates that the process is exiting. Other aspects of the kernel will use it to avoid trying to process the process when the process is deleted. Separating a process from the various resources it receives during its lifecycle is implemented through a series of calls, suchexit_mm(Delete memory pages) andexit_keys(Release thread sessions and process security keys ).do_exitVarious statistics required for function execution to release the process.exit_notifyExecute a series of notifications (for example, telling the parent process that its child process is exiting ). Finally, the Process status is changedPF_DEADAnd also callsscheduleFunction to select a new process to be executed. Note that if the notification to the parent process is required (or the process is being tracked), the task will not completely disappear. You can callrelease_taskTo actually reclaim the part of memory used by the process.

6. Conclusion

Linux is still evolving. process management is one of the areas to be further innovated and optimized. While adhering to the UNIX principle, Linux is also making breakthroughs. The new processor architecture, Symmetric Multi-processing (SMP), and virtualization will lead to new developments in the kernel field. One example is the new O (1) scheduler introduced in Linux 2.6, which provides scalability for systems with a large number of tasks. Another example is the Thread model updated using Native POSIX Thread Library (NPTL). Compared with the previous LinuxThreads model, it provides more effective Thread processing.

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.