Study Notes for Linux kernel 3rd-process Descriptor

Source: Internet
Author: User

Process Descriptor (process descriptor), as its name implies, is the description of a process. It is used to describe the data structure of a process and can be understood as the attributes of a process. For example, the Process status and PID are encapsulated in the data structure of the Process descriptor. The data structure is defined as task_struct.

 

Process status

There are 7 states for processes in Linux. The State field in the task_struct structure of the process specifies the state of the process.

Runable status (task_running)

Stoppedwait (task_interruptible)

Uninterrupted waiting (task_uninterruptible)

Paused (task_stopped)

Trace status (task_traced): The process is paused or monitored by the debugger.

Exit_zombie: the process is terminated, but the parent process has not called the wait system call.

Task _ dead: the parent process initiates a wait system call and the process is deleted by the system.

 

Identifies a process

Two methods to identify a process: Process descriptor address and PID. The PID value is stored in the PID field of the task_struct structure.

The execution context that can be independently scheduled has its own process descriptor. Therefore, the Lightweight Process (lwp) also has its own task_struct structure.

Linux assigns different PIDs to each process and lwp (similarly, in windows, PID and TID are also allocated to each process and thread, and PID and TID are not the same, note, here, lwp in Linux is similar to a thread in Windows ).

Linux also has the thread group concept. All threads in a thread group use the PID of the thread group's leading thread, that is, the pid of the first lwp in the group. The PID of this thread group is stored in the tpid field of the task_struct structure. The tpid value of the thread group's leading thread is the same as that of the PID.

 

Obtain the process descriptor address.

In Linux, two data structures are compact together: the kernel stack of the Process, thread_info (thread descriptor ). Generally, the two data structures are 8192 bytes in size and placed on two consecutive pages. The first address is 2.A multiple of 13. 8 KB is sufficient for the kernel stack and thread_info (you can also set it when compiling the kernel so that the two data structures occupy one page ). This 8 KB initial storage thread_info structure, the kernel stack grows from the end down. In the thread_info structure, there is a pointer to the process descriptor task, which can be used to find the task_struct structure address. In the task_struct structure, there is also a thread_info pointer pointing to the thread_info structure.

Because thread_info and kernel stack are tightly stored together, you can find the thread_info structure address from the kernel stack, and then find the task_struct structure pointer through the task pointer in the thread_info structure. For 8 KB, get the value in ESP, and then compare the value with 0xffffe000, which is about to be reset by 13 BITs, the address of thread_info is obtained, and then the address of task_struct is obtained.

 

Process linked list

In Linux, multiple processes are organized into a cyclic double-stranded table. The process chain table header is the init_task descriptor, that is, the descriptor of the 0 process or Swapper process. Use the tasks field in the task_struct structure to connect multiple processes to the linked list structure.

In earlier Linux versions, all task_running processes were put in a running queue. In this way, the overhead of sorting the linked list by priority was relatively large, early scheduling programs had to traverse the entire linked list to select the best process.

In Linux 2.6, the running queue is different. Multiple processes can be linked in the system, that is, the running queue contains multiple processes that can be run. Each list of processes that can be run has a priority value ranging from 0 ~ 139. Assuming that a process has a priority of K, the run_list field in the task_struct structure of the process connects it to the list of runable processes with a priority of K. In addition, in a multi-processor system, each CPU has its own running queue. The prio_array_t Data Structure manages these processes.

 

Inter-process relationship

There is a parent-child relationship between processes. If a process creates multiple sub-processes, there is a sibling relationship between these sub-processes. In Linux, process 0 and process 1 are created by the kernel, and process 1 (init) is the ancestor of all other processes.

In the task_struct structure of the process Descriptor Table, the following fields indicate the relationship between processes:

Real_parent: the descriptor of the process that creates PROCESS p. If the parent process of process p does not exist, it points to the descriptor of process 1.

Parent: The current parent process pointing to P, which is often the same as real_parent. When the Q process sends a tracing debugging ptrace () System Call to P, this field points to the Q process descriptor.

Children: A linked list header. All elements in the linked list are subprocesses created by PROCESS p.

Sibling: pointer to the next element or the previous element of the sibling process linked list.

 

In addition, there are other relationships between processes: logon session, process group, thread group, and tracing debugging.

In the task_struct structure, the following fields represent these relationships (assuming the current process is p ):

Group_leader: descriptor pointer of the lead process in the process group where p is located

Signal-> pgrp: PID of the lead process in the process group where p is located

Tgid: PID of the lead process in the thread group where p is located

Signal-> session: PID of the lead process in the logon session where p is located

Ptrace_children: A linked list header. All elements in the linked list are subprocesses of P tracked by the debugger.

Ptrace_list: When P is debugged, it points to the first and next elements of the parent process linked list of the debug tracing process.

 

PID export process Descriptor

In some cases, you need to obtain the response process descriptor pointer from the PID, such as kill () System Call. Because it is inefficient to scan the process linked list sequentially and check the PID field of the Process descriptor, Four hash tables are introduced:

Pidtype_pid

Pidtype_tgid

Pidtype_pgid

Pidtype_sid

These four hash tables dynamically allocate space during kernel initialization. Their addresses are stored in the pid_hash array, and their length depends on the ram capacity. Pid_hashfn can be used to convert a PID into a table index.

To prevent conflicts caused by hash operations, linux adopts the zipper method, that is, a hash table with a linked list is introduced for processing.

 

Process Organization

The linked list of the running queue organizes all processes in the task_running state. For processes in other states, perform the following operations in Linux:

  • For task_stopped, exit_zombie, and exit_dead processes, Linux does not set up a dedicated linked list for them because the access is simple.
  • Processes in the task_interruptible and task_uninterruptible statuses are divided into many categories, each of which corresponds to a specific event. In this status, the Process status cannot provide enough information to quickly obtain the process. Therefore, it is necessary to introduce additional process linked lists. These linked lists are called "Waiting queues ".

Waiting for queues has many functions, such as interrupt processing, process synchronization, and timing.

A waiting queue is implemented by a double-stranded table. Each waiting queue has a queue header. This is a data structure of wait_queue_head_t. The data structure contains a lock variable of the spinlock_t type, which is a spin lock used to ensure that the queue is mutually exclusive for access and operations.

The element type in the waiting queue is wait_queue_t. The data structure contains a task field, which is a pointer to a process descriptor. There is a func field, which is a function pointer, it indicates how the process is awakened (that is, the function is called when it is awakened). There is also a flags field that determines that the process is a mutex process (flags = 1) or non-mutex process (flags = 0 ).

Here we will explain the mutex process and the non-mutex process. Non-mutex processes are always awakened by the kernel when an event occurs. The mutex process is selectively awakened by the kernel when an event occurs, such as a process that accesses the critical section.

 

Process resource restrictions

Each process has a set of related resource restrictions, indicating the number of system resources that the process can use. Avoid excessive use of system resources (CPU and disk space) by processes ).

Restrictions on Process resources are stored in the signal-> rlim field of the Process descriptor. This field is an array of rlimit structures. Each element in the array corresponds to a resource.

You can use the getrlimit () and setrlimit () systems to increase the current resource limit.

If the resource limit is rlimit_infinity (0 xffffffff), there is no corresponding resource limit.

 

Summary

Description of some fields in the process Descriptor (task_struct). Assume that the process is P.

  • State: P Process status, which is changed using set_task_state and set_current_state macros, or directly assigned values.
  • Thread_info: pointer to the thread_info structure.
  • Run_list: assume that the P state is task_running, the priority is k, and run_list connects P to the list of runable processes with the priority of K.
  • Tasks: connect P to the process linked list.
  • Ptrace_children: The linked list header. All elements in the linked list are the sub-processes of P tracked by the debugger.
  • Ptrace_list: When P is debugged, all elements in the linked list are subprocesses of P tracked by the debugger.
  • PID: p process ID (PID ).
  • Tgid: PID of the lead process in the thread group where p is located.
  • Real_parent: The process descriptor pointer of the real parent process of P.
  • Parent: The process descriptor pointer of the parent process of P. When debugged, It is the descriptor pointer of the debugger process.
  • Children: subprocess linked list of P.
  • Sibling: connect P to P's sibling process linked list.
  • Group_leader: descriptor pointer of the lead process in the thread group where p is located.

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.