When learning a process, you may encounter a linux Process management problem. Here we will introduce the solution to the linux Process management problem, and share it with you.
Maximum number of processes: in Linux, although the processes are dynamically allocated, 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 represented by a dynamically assigned task_struct. One exception is the init process itself, which always exists and is represented by a statically assigned task_struct. This example can be found in./linux/arch/i386/kernel/init_task.c.
In Linux, there are two methods for allocating all linux Process Management. 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 with no header or tail. However, because init_task always exists, it can be used as an anchor to continue the previous 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. The following list shows a simple program that iterates the task list and provides a small amount of information about each task, such as name, pid, and parent name ). Note: Here, this module uses printk to send the result. To view the specific results, you can use the cat utility or real-time tail-f/var/log/messages) to view the/var/log/messages file. The next_task function is a macro in sched. h. It simplifies the iteration of the task list and returns the task_struct reference of the next task ).
List: procsview. c, a simple kernel module that sends task information)
# Include
# Include
# Include
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 the Makefile shown in the list to compile this module. During compilation, you can use insmod procsview. ko to insert a module object or use rmmod procsview to delete it.
List: Makefile used to build the kernel module
Obj-m + = procsview. o
KDIR: =/lib/modules/$ (shell uname-r)/build
PWD: = $ (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 a idle task called swapper) and init task pid 1 ).
Nov 12 22:19:51 mtj-desktop kernel: [8503.873310] *** swapper [0] parent swapper
Nov 12 22:19:51 mtj-desktop kernel: [8503.904182] *** init [1] parent swapper
Nov 12 22:19:51 mtj-desktop kernel: [8503.904215] *** kthreadd [2] parent swapper
Nov 12 22:19:51 mtj-desktop kernel: [8503.904233] *** migration/0 [3] parent kthreadd
...
Note: You can also identify the currently running task. Linux maintains a symbol called current, which indicates that the currently running process type is task_struct ). If the following line of code is inserted at the end of init_module:
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 is insmod because the init_module function runs in the context where the insmod command is executed. The current symbol actually refers to a function get_current) and can be found in an arch-related header such as./linux/include/asm-i386/current. h ).
- Linux shell: Development History of unix/linux shell
- One of Linux shell programming learning: What is shell?
- How to Implement SCSI hard disk hot swapping and online identification in Linux
- Easy understanding of CUPS software for Linux Printing
- Install a linux virtual machine under win32