On Thu, Apr 12, 2012 at 10:33 PM, Jonathan Neuschäfer <j.neuschaefer@gmx.net> wrote: Hi Jonathan
On Thu, Apr 12, 2012 at 09:52:02PM +0800, harryxiyou wrote:
On Thu, Apr 12, 2012 at 9:03 PM, Jonathan Neuschäfer <j.neuschaefer@gmx.net> wrote:
Hi Jonathan,
[...]
I give the pid 8, state 8, and comm "jiawei" in my module. But it can not print correctly. Maybe kernel can tell my bogus one,right?
This has to do with the way accessing struct fields works in C: For each struct each field name is translated by the compiler into an offset which is used to compute the address of a field given the struct's address. When you access the pid field of a struct task_struct the offset will be at least around 20 * sizeof(int), which is an invalid offset to your struct pcb, where the offsets are (most of the time): pid: 0 state: sizeof(int) flag: 2 * sizeof(int) comm: 3 * sizeof(int) tasks: 3 * sizeof(int) + sizeof(char *) (You get (an approximation of) the offset of a field by adding the size of the previous field (the compiler also adds some padding - see Documentation/unaligned-memory-access.txt in the kernel tree and http://en.wikipedia.org/wiki/Data_padding#Data_structure_padding))
It sounds well. I will test it, which delare a structure named 'pcb' but including all the fileds as task_struct structure. Thanks Harry Wei