【问题标题】:Process id of parent process not showing up in ps-a commandps-a 命令中未显示父进程的进程 ID
【发布时间】:2019-09-28 06:48:40
【问题描述】:

我在 linux 中用 c 编写代码,我必须显示当前正在运行的进程的 pid 以及父进程的 pid。我正在使用 ps -a 命令进行交叉检查,但它没有出现在那里。任何帮助将不胜感激

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

 void main()
 {
  sleep(4);
  printf("The process id of the current process running is  %d\n",getpid());                              
  sleep(4);
  printf("The process id of the parent process is %d\n", getppid());
 }

【问题讨论】:

  • 你怎么打电话给ps -a?它应该工作。尝试./program 并在另一个控制台中运行pidof ./program
  • @DavidRanieri 我正在使用 2 个终端。我在一个终端中执行文件并在另一个终端中运行 ps -a 命令
  • @DavidRanieri 它只显示当前进程的进程ID,而不是父进程的pid

标签: linux system-calls


【解决方案1】:

ps -a command没有给你命令的PID,你需要grep

ps -a | grep command

如果你不想使用grep,你可以使用

pidof command

但如果你还想要你需要的 PPID:

ps -ef | grep command

【讨论】:

    猜你喜欢
    • 2013-09-08
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2015-12-31
    • 2013-08-11
    相关资源
    最近更新 更多