how to get process name from c program?
11,709
Solution 1
You can have a look at the /proc/$pid/cmdline
file (open it like a normal file and read the zero-byte-delimited command line from it).
That's the way the Unix tool ps
does it on Linux.
Solution 2
one simple way to know the current executable name is argv[0]
, which tells you the name of the current executable in C.
Solution 3
More solutions can be found here: How to get current process name in linux?
I implemented the solution using the program_invocation_name
variable.
Comments
-
Ravi Chandra almost 2 years
In C program in linux, we can use
getpid()
andgetppid()
system call to get the pid and ppid of a process,Similarly is there any system call to get name of a process/current process?
-
chtenb over 10 yearsI would fetch the process list (using
ps
) and match on the pid.
-