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.

Share:
11,709
Ravi Chandra
Author by

Ravi Chandra

Works at Nvidia for display drivers

Updated on June 15, 2022

Comments

  • Ravi Chandra
    Ravi Chandra almost 2 years

    In C program in linux, we can use getpid() and getppid() 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
      chtenb over 10 years
      I would fetch the process list (using ps) and match on the pid.