免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2185 | 回复: 6
打印 上一主题 下一主题

ps命令不能列出所有的进程? [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-10 16:05 |只看该作者 |倒序浏览
这是下面我的测试代码,开辟了另一个子进程,子进程又开辟另一个子进程。结束前,一共是3个进程,可是运行后,用另一个终端,用ps观察,只能看到2个a.out进程!这是怎么回事呢?我的环境是RH8。

  1. [yangwl:/home/users50/yangwl/test/undone/tfork]$ cat tfork.c
  2. #include <stdio.h>;
  3. #include <stdlib.h>;
  4. #include <sys/types.h>;
  5. #include <sys/wait.h>;
  6. #include <unistd.h>;

  7. int main(void) {
  8.         pid_t   pid;
  9.         int     status;

  10.         pid = fork();
  11.         if (pid == -1) {
  12.                 printf("fork error!\n");
  13.                 exit(1);
  14.         }

  15.         if (pid == 0) {                         // child.
  16.                 pid_t   pid;

  17.                 pid = fork();
  18.                 if (pid == -1) {
  19.                         printf("fork error!\n");
  20.                         exit(1);
  21.                 }

  22.                 if (pid == 0) {                 // child's child.
  23.                         pid_t   cc_pid,
  24.                                 cc_ppid;

  25.                         cc_pid = getpid();
  26.                         cc_ppid = getppid();

  27.                         printf("child's child!\n");
  28.                         printf("%d:pid = %d\n",cc_pid,cc_pid);
  29.                         printf("%d:ppid = %d\n",cc_pid,cc_ppid);

  30.                         sleep(10);
  31.                         exit(0);
  32.                 }
  33.                 else {                          // fork twice, child exit.
  34.                         pid_t   c_pid,
  35.                                 c_ppid;

  36.                         c_pid = getpid();
  37.                         c_ppid = getppid();

  38.                         printf("child!\n");
  39.                         printf("%d:pid = %d\n",c_pid,c_pid);
  40.                         printf("%d:ppid = %d\n",c_pid,c_ppid);

  41.                         if ( wait(&status) != pid ) {
  42.                                 printf("wait error!\n");
  43.                                 exit(1);
  44.                         }
  45.                         printf("\n%d exit!\n",pid);
  46.                         printf("status: %d\n",status);

  47.                         sleep(10);
  48.                         exit(0);
  49.                 }
  50.         }
  51.         else {                  // father.
  52.                 pid_t   f_pid;

  53.                 f_pid = getpid();

  54.                 printf("father!\n");

  55.                 printf("%d:pid = %d\n",f_pid,f_pid);

  56.                 if ( wait(&status) != pid ) {
  57.                         printf("wait error!\n");
  58.                         exit(1);
  59.                 }
  60.                 printf("\n%d exit!\n",pid);
  61.                 printf("status: %d\n",status);

  62.                 sleep(10);

  63.                 printf("\n%d father exit!\n", f_pid);
  64.                 exit(0);

  65.         }
  66. }
  67. [yangwl:/home/users50/yangwl/test/undone/tfork]$ gcc tfork.c ; ./a.out
  68. father!
  69. 25449:pid = 25449
  70. child!
  71. 25450:pid = 25450
  72. 25450:ppid = 25449
  73. child's child!
  74. 25451:pid = 25451
  75. 25451:ppid = 25450

  76. 25451 exit!
  77. status: 0

  78. 25450 exit!
  79. status: 0

  80. 25449 father exit!
  81. [yangwl:/home/users50/yangwl/test/undone/tfork]$
复制代码

论坛徽章:
0
2 [报告]
发表于 2005-01-10 19:03 |只看该作者

ps命令不能列出所有的进程?

ps -aux
这样看。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
3 [报告]
发表于 2005-01-10 21:05 |只看该作者

ps命令不能列出所有的进程?

我在UNIXWARE下能看到3个,RH9下只有2个,ps -aux也是2个

论坛徽章:
0
4 [报告]
发表于 2005-01-10 22:06 |只看该作者

ps命令不能列出所有的进程?

ps axm

论坛徽章:
0
5 [报告]
发表于 2005-01-10 22:25 |只看该作者

ps命令不能列出所有的进程?

用top看看??
top | grep a.out

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2005-01-11 08:57 |只看该作者

ps命令不能列出所有的进程?

[quote]原帖由 "yuxh"]我在UNIXWARE下能看到3个,RH9下只有2个,ps -aux也是2个[/quote 发表:


都试过了,就入yuxh所说的,就显示2个。

论坛徽章:
0
7 [报告]
发表于 2005-01-11 09:48 |只看该作者

ps命令不能列出所有的进程?

原帖由 "aero" 发表:


都试过了,就入yuxh所说的,就显示2个。

那就每个系统都man一下ps,可能各个系统的ps参数是不一样的。
我想用top应该能看到所有的进程吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP