From mboxrd@z Thu Jan 1 00:00:00 1970 From: harryxiyou@gmail.com (harryxiyou) Date: Fri, 13 Apr 2012 23:00:19 +0800 Subject: [RFC]Something wrong with my module In-Reply-To: <20120412143355.GB1874@debian.debian> References: <20120412130325.GA1874@debian.debian> <20120412143355.GB1874@debian.debian> Message-ID: To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org On Thu, Apr 12, 2012 at 10:33 PM, Jonathan Neusch?fer 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 >> 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