Skip to content

Instantly share code, notes, and snippets.

@dklesev
Forked from fclairamb/get_ppid_and_name.c
Created May 29, 2016 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dklesev/dd25ac29096b168a6c7f39fc1022c83d to your computer and use it in GitHub Desktop.
Save dklesev/dd25ac29096b168a6c7f39fc1022c83d to your computer and use it in GitHub Desktop.
posix: Get parent process id and name
#ifdef SHELL
gcc -Wall -Werror $0 && ./a.out
exit $?
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
/**
* Get a process name from its PID.
* @param pid PID of the process
* @param name Name of the process
*
* Source: http://stackoverflow.com/questions/15545341/process-name-from-its-pid-in-linux
*/
static void get_process_name(const pid_t pid, char * name) {
char procfile[BUFSIZ];
sprintf(procfile, "/proc/%d/cmdline", pid);
FILE* f = fopen(procfile, "r");
if (f) {
size_t size;
size = fread(name, sizeof (char), sizeof (procfile), f);
if (size > 0) {
if ('\n' == name[size - 1])
name[size - 1] = '\0';
}
fclose(f);
}
}
/**
* Get the parent PID from a PID
* @param pid pid
* @param ppid parent process id
*
* Note: init is 1 and it has a parent id of 0.
*/
static void get_process_parent_id(const pid_t pid, pid_t * ppid) {
char buffer[BUFSIZ];
sprintf(buffer, "/proc/%d/stat", pid);
FILE* fp = fopen(buffer, "r");
if (fp) {
size_t size = fread(buffer, sizeof (char), sizeof (buffer), fp);
if (size > 0) {
// See: http://man7.org/linux/man-pages/man5/proc.5.html section /proc/[pid]/stat
strtok(buffer, " "); // (1) pid %d
strtok(NULL, " "); // (2) comm %s
strtok(NULL, " "); // (3) state %c
char * s_ppid = strtok(NULL, " "); // (4) ppid %d
*ppid = atoi(s_ppid);
}
fclose(fp);
}
}
int main(int argc, char *argv[]) {
pid_t pid = getpid();
while (pid != 0) {
char name[BUFSIZ];
get_process_name(pid, name);
printf("%6d - %s\n", pid, name);
get_process_parent_id(pid, & pid);
}
return 0;
}
/* Your should get something like that:
24090 - ./a.out
24084 - bash
2425 - bash
1369 - /usr/bin/xfce4-terminal
13056 - init
12994 - lightdm
1428 - lightdm
1 - /sbin/initsession-child
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment