/proc help
Dave Hylands
dhylands at gmail.com
Thu Feb 9 16:19:16 EST 2012
Hi Surenkumar,
On Thu, Feb 9, 2012 at 12:31 PM, Surenkumar Nihalani <suren at gatech.edu> wrote:
> Hi guys,
>
> I want to find the number of threads owned by the caller process who tried
> to read from my proc file, how do I find the stuct responsible for it?
> Also, I want to know the thread id of my caller.
> I am writing a kernel module. Please help.
current->pid will give you the process ID. In the kernel, the process
ID is unique per thread.
current->tgid will give you the "thread group ID" which is also
sometime called the PID in user space. This is unique per process but
not unique per thread.
So part of this boils down to deciding what your terminology means. I
think of a process as a memory space, and thread as a unit of
execution, where each process has one or more threads associated with
it. For historical reasons, the kernel uses pid, where I would
normally think of it as being a thread ID, but it is what it is, and
is unlikely to change.
So if you create a process with multiple threads, each thread will
have a uniqe pid, and the tgid associated with each thread will match
the pid of the first thread which started the process. You can see
this reflected in the /proc filesystem as well. /proc/TTT/task/PPP/*
TTT is the tgid, and PPP is the pid.
If you want to know how many different threads/processes have accessed
your proc file, then you'll need to keep track of that yourself inside
the proc entry callback. So you can create whatever data structure
you'd like. IIRC the proc entry callback executes in the context of
the thread/process which actually issues the read command.
--
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com
More information about the Kernelnewbies
mailing list