Get process name given PID
#snippets
Description
Given a process id get the process name in a POSIX system environment.
C
void print_name_pid(pid_t pid)
{
char name[PATH_MAX];
int c, i = 0;
FILE *f;
sprintf(name, "/proc/%ld/cmdline", (long) pid);
f = fopen(name, "r");
if(!f) {
printf("Not able to open cmdline file\n");
return;
}
while((c = getc(f)) != EOF && c != 0)
name[i++] = c;
name[i] = '\0';
printf("%s(%ld)\n", name, (long)pid);
fclose(f);
}