C语言 列出主进程的所有父进程



我列出了Main()进程的所有父进程的pid和命令。我应该在main()函数中写些什么以便我能列出根进程?

当前代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

void get_ppid(const pid_t pid, pid_t * ppid) {
char buffer[1024];
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) {
// http://man7.org/linux/man-pages/man5/proc.5.html -> /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);
}
}
char* get_name(const pid_t pid){
char path[1024] = "";
char pids[20];
sprintf(pids, "%d", pid);
strcat(path, "/proc/");
strcat(path, pids);
strcat(path, "/cmdline");
FILE* fp = fopen(path, "r");
if(fp == NULL){
printf("Cannot open the file!");
exit(1);
}
char* pname = malloc(1024);
fscanf(fp, "%s", pname);
return pname;
}
void print_process_info(const pid_t pid, pid_t ppid, char* pname){
printf("%-20d%-20d%-50sn", pid, ppid, pname);
}
void print_header(){
printf("%-20s%-20s%-50sn", "ID", "Parent ID", "Command");
}

int main(int argc, char *argv[])
{
int pid =  getpid();
int ppid;
get_ppid(pid, &ppid);
char* pname = get_name(pid);
print_header();
print_process_info(pid, ppid, pname);
free(pname);
}

我如何通过改变main()函数的内容来处理这个问题?

我得到的输出:

ID            |     Parent ID      |     Command                                              
782663        |      782612        |     ./main

我想要得到的输出是这样的(列出主进程的所有父进程):

-------------------------------------------------------------------
ID          |        Parent ID    |       Command                                              
783001      |         782612      |        ./main                                            
782612      |        609630       |       /bin/bash                                         
609630      |        609333       |       /usr/bin/kate                                     
609333      |        609170       |       /usr/bin/ksmserver                                
609170      |        1            |       /lib/systemd/systemd
|
1           |        0            |       /sbin/init
-----------------------------------------------------------------

正如评论中指出的,在使用pidppid的值后,将当前PID替换为父PID,只要不为零就循环。

我如何通过改变main()函数的内容来处理这个问题?

仅将程序中的main更改为此

int main(int argc, char *argv[])
{
int pid =  getpid();
do {
int ppid;
get_ppid(pid, &ppid);
char *pname = get_name(pid);
print_header();
print_process_info(pid, ppid, pname);
free(pname);
pid = ppid;
} while (pid != 0);
}

产生输出:

ID                  Parent ID           Command                                           
71678               61852               ./a.out                                           
ID                  Parent ID           Command                                           
61852               61829               bash                                              
ID                  Parent ID           Command                                           
61829               952                 /usr/lib64/gnome-terminal/gnome-terminal-server   
ID                  Parent ID           Command                                           
952                 1                   /usr/lib/systemd/systemd                          
ID                  Parent ID           Command                                           
1                   0                   /usr/lib/systemd/systemd

最新更新