How to get process name from pid??

Submitted by deepzambre
on October 18, 2006 - 4:37am

hello all,
Can anybody tell me how to obtain process name if we have pid , in a kernel module??
I have tried the way of reading /proc/pid/status, But is there any other way to do so,, is it possible to read proces name from kernel data structures( like task_struct) ??

And i also want to know that ,
is it possible to open a file in kernel mode (kernel module) if so then which functions should be used to open file and read its contents??
Please reply..

Thanks in advance,,

It is not clear to me what yo

Anonymous (not verified)
on
October 18, 2006 - 6:35am

It is not clear to me what you want.

A process ID is unique. A process "name" not (in fact there is
no such thing as the "process name"). You can
have 10 people issuing a "ls" command at the same time. All
these will have different PIDs but the same process "name".

What you want is probably what the name of the command was
(as entered by the user on the shell command line).
But that one is easy:

int main( int argc, char *argv[])
{
   printf( "My name is: %s\n", argv[0])

   return 0;
}

Greetings
AC

Sorry, "but that one is easy"

Anonymous (not verified)
on
October 18, 2006 - 6:37am

Sorry, "but that one is easy" in user space.

I do not know if it is possible inside a kernel module.

AC

task_struct defined in includ

mator
on
October 18, 2006 - 7:56am

task_struct defined in include/linux/sched.h have char comm[TASK_COMM_LEN] as in 2.6.18 kernel, hope this helps.

ps: but what for do you need process name anyway in your driver ?
ps2: /proc/$pid/ files can be somesort usefull

THANKS

deepzambre
on
October 19, 2006 - 3:08am

Thanks mator ,,
it worked with comm[TASK_COMM_LEN] this field. thanks a lot for your help..

how u use comm[TASK_COMM_LEN]?

jesstan (not verified)
on
November 27, 2006 - 11:08am

i'm using linux 2.6.8
also hav to print out the process name, and i oredi get the pid,
how u use "comm[TASK_COMM_LEN]" to get process name?

printk("%c",comm[TASK_COMM_LEN]);

can't work...in sched.c

You need to print the first c

gat3way (not verified)
on
November 28, 2006 - 12:52am

You need to print the first character of the task name,errr?

Why don't you use %s instead? :)

Because he's getting into ker

Anonymous (not verified)
on
November 28, 2006 - 3:44am

Because he's getting into kernel programming before learning C?

On systems with procfs

Anonymous (not verified)
on
October 19, 2006 - 7:59am

On systems with procfs you can always check content of /proc/$PID/cmdline

I know no other way. Linux keeps only that info and procfs is only way I know to access that info.

File IO from kernel space

Anonymous (not verified)
on
October 19, 2006 - 8:11am

> is it possible to open a file in kernel mode (kernel module)
> if so then which functions should be used to open file and
> read its contents??

No, it is generally impossible. Under Linux you do not really read file, you map it into cache and cause page fault to load its content into memory/cache. Kernel doesn't allow kernel itself to produce page faults.

One of the possible ways is thru helper application. (Do not have copy of kernel at hand - can't recall function name.) Module can ask kernel to spawn a process. Then the application would read file you need and with e.g. ioctls() would transfer its content into kernel space.

> Kernel doesn't allow kernel

prabir
on
November 27, 2006 - 11:30am

> Kernel doesn't allow kernel itself to produce page faults.

Maybe digressing from the topic but why is that so???

time...

Anonymous (not verified)
on
January 6, 2007 - 12:11pm

cause OS must work far quicker. It doesn't have that much time to spare.

FILE I/O From User Space

coop (not verified)
on
August 24, 2007 - 6:53am

It is _not_ impossible, just stupid. You can
use filp_open(), filp_close, and kernel_read().
Writing is more difficult but still possible.
But the original questioner obviously is
pretty clueless and is just asking for trouble
doing this. There are a large variety of better ways to do whatever he is trying to do.

I hardly ever venture into

Brian (not verified)
on
November 30, 2007 - 4:39pm

I hardly ever venture into the kernel discussion boards because of posts like this one:

But the original questioner obviously is
pretty clueless and is just asking for trouble
doing this. There are a large variety of better ways to do whatever he is trying to do.

How is this even remotely helpful to the community? I understand it must get irritating hearing newbie questions all day long, but there is no need for taking cheap shots at interested, motivated individuals. I agree that the enthusiast who started this thread is doing something sub-optimal, but, hey, we all have to make mistakes to learn.

So for heavens sake, once you've pointed out that the guy is making a bad choice in his approach, try pointing him in the right direction.

Get process name (cmdline) from pid?

TragicWarrior
on
May 1, 2007 - 2:54pm

Is there anyway to do this in user space in GNU/Linux apart from reading /proc/pid/cmdline? HP-UX has pstat, but I can't find anything comparable.

find_task_by_pid(pid)

Glikis (not verified)
on
May 8, 2007 - 6:25am

ok
you have the pid

pid_t var;

what you can do is

Use find_task_by_pid(pid) which returns a pointer to the task_struct. The comm field of the task_struct gives the name of the process.

struct task_struct *my;

my = find_task_by_id(pid)

printf("%s",my->comm); //print f or whater you want to do with it

getting process name from process id

Anonymous (not verified)
on
August 23, 2007 - 10:11pm

in userspace iam not able to get process name using find_task_by_id(pid). can any one point me how to get the process name from process id in user space.

man ps

Anonymous (not verified)
on
December 3, 2007 - 9:36am
  • With the ps command: ps ch -o %c $$
  • from the proc file system: tr \\0 \\n </proc/$$/cmdline

After you have read "man ps", you might also read "man tr" "man proc" and "man bash". You can also avoid waiting for replies to queries on this site by reading "man man", and trying "man -k xrated".

observe how i get the name

Anonymous (not verified)
on
December 5, 2007 - 11:22am

observe how i get the name of a program given its PID

[bash]$ ps -aux
...
user 23453 0.0 0.0 2356 780 tty2 S 12:18 0:00 gnome-pty-helper
...

[bash]$ cat /proc/23453/cmdline
gnome-pty-helper

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.