How to parse the Linux kernel to obtain the current process pointer-Linux general technology-Linux programming and kernel information. The following is a detailed description.
1. Memory Data Representation:
In teaching materials or reading, we often need to intuitively use diagrams to show the distribution of data in the memory. How is the data organized in the memory? Different machines have different representations. We use the most common Intel X86 series computers as an example to illustrate this problem.
(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // linux.ccidnet.com/col/attachment/2006/11/913107.jpg'); ">
Memory: the memory is low. The memory is at the bottom, and the unit of memory is 16 bits. For Computers Based on intel i386 architecture, the system uses the small-end byte sequence to store data. The so-called small-end byte sequence refers to the low-order Byte Low-address, high-order byte high address (memory address increase direction). The large-end byte order is opposite. The given system uses the byte order called the host byte order. The CPU also reads data in the form of a small-end byte order, as shown in, if the variable num is a 16-bit short Integer type, the num read from the memory is 0x1234; If num is a 32-bit int type, the CPU reads num = 0x56781234 from the memory, where the num address is 0x12345678, that is, & num = 0x12345678.
2. Obtain the Process Task Structure pointer from the Linux Kernel
After understanding the system memory data representation, let's take a look at how the Linux kernel gets the task structure pointer of the current process. The following code uses the source code of Linux Kernel 2.4.0.
In include \ asm-i386 \ current. h
# Ifndef _ i1__current_h
# Define _ i1__current_h
Struct task_struct;
Static inline struct task_struct * get_current (void)
{
Struct task_struct * current;
_ Asm _ ("andl % esp, % 0;": "= r" (current): "0 "(~ 8191UL ));
Return current;
}
# Define current get_current ()
# Endif /*! (_ I1__current_h )*/