免费注册 查看新帖 |

Chinaunix

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

[C] 为什么WIFCONTINUED不在作业继续的时候没有正确的返回呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-01-16 17:50 |只看该作者 |倒序浏览
本帖最后由 DIYBYPERL 于 2014-01-16 17:58 编辑

代码如下,结果见后
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <stdarg.h>
  8. #include <errno.h>
  9. #include <sys/sysmacros.h>
  10. #include <sys/utsname.h>
  11. #include <pwd.h>
  12. #include <grp.h>
  13. #include <time.h>
  14. #include <sys/time.h>


  15. #define MAXLINE 1024
  16. #define FILE_MODE 0777

  17. #define BUFFSIZE 4096
  18. static int err_sys(char *fmt, ...);
  19. void pr_exit(int status);


  20. int main(int argc, char **argv)
  21. {
  22.     int iret;
  23.     pid_t pid;
  24.     int status = 0;


  25.     if((pid = fork()) < 0)
  26.         err_sys("fork error");
  27.     else if(pid == 0)
  28.     {
  29.         int cnt = -1;
  30.         while(cnt ++ < 120)
  31.         {
  32.             printf("CHLD:%2d.\n", cnt);
  33.             sleep(1);
  34.         }
  35.         _exit(0);
  36.     }
  37.     else
  38.     {
  39.         int cnt = -1;
  40.         while(waitpid(pid, &status, WCONTINUED | WUNTRACED ) == pid)
  41.         {
  42.             pr_exit(status);
  43.         }
  44.         printf("PARENT OVER\n");

  45.     }


  46.     exit(0);

  47. }



  48. /****************************************************************
  49. *以下是错误处理函数
  50. ***************************************************************/
  51. static int err_sys(char *msg)
  52. {
  53.         printf("%s\n", msg);
  54.     exit(1);
  55. }



  56. /****************************************************************
  57. *以下获取子进程退出状态函数
  58. ***************************************************************/
  59. void pr_exit(int status)
  60. {
  61.     if(WIFEXITED(status))
  62.     {
  63.         printf("normal terminatioin, exit status = %d\n", WEXITSTATUS(status));
  64.     }
  65.     else if(WIFSIGNALED(status))
  66.     {
  67.         printf("abnormal termination, signal number = %d%s\n",
  68.                 WTERMSIG(status),
  69. #ifdef WCOREDUMP
  70.                 WCOREDUMP(status) ? "(core file generated)" : "");
  71. #else
  72.               "");
  73. #endif
  74.     }
  75.     else if(WIFSTOPPED(status))
  76.     {
  77.         printf("child stopped, signal number = %d\n", WSTOPSIG(status));
  78.     }
  79.     else if(WIFCONTINUED(status))
  80.     {
  81.         printf("child continue\n");
  82.     }
  83.     else
  84.     {

  85.         printf("unkown exit status\n");
  86.     }

  87.     fflush(stdout);

  88. }











复制代码
代码是在AIX6.1下测试的。直接用“kill -18 <子进程ID>” 给子进程SIGTSTP信号,用“kill -19 <子进程ID>” 给子进程SIGCONT信号。发送SIGTSTP信号,父进程都能waitpid到子进程相应的状态,但发送继续信号时收不到子进程相应的状态。但在随后再次发送SIGTSTP信号时,父进程却同时收到子进程的暂停状态和之前的继续状态, 这是是个什么意思呢?????在SIGTSTP时,收到子进程的暂停和继续状态, 在SIGCONT却收不到继续状态????
请大神们指点

以下是运行结果,
  1. CHLD: 0.
  2. CHLD: 1.
  3. CHLD: 2.
  4. CHLD: 3.
  5. CHLD: 4.
  6. CHLD: 5.
  7. CHLD: 6.
  8. CHLD: 7.
  9. CHLD: 8.
  10. CHLD: 9.
  11. CHLD:10.
  12. CHLD:11.
  13. CHLD:12.
  14. child stopped, signal number = 18   子进程收到SIGTSTP信号,父进程也打印了相应的内容

  15. CHLD:13.                            子进程收到SIGCONT信号继续运行,父进程却没有waitpid地到相应的状
  16. CHLD:14.
  17. CHLD:15.
  18. CHLD:16.
  19. CHLD:17.
  20. CHLD:18.
  21. CHLD:19.
  22. CHLD:20.
  23. CHLD:21.
  24. child stopped, signal number = 18 子进程第二次收到SIGTSTP信号,父进程同时收到了子进程暂停的状态和之前的继续的状态!!
  25. child continue                    子进程第二次收到SIGTSTP信号,父进程同时收到了子进程暂停的状态和之前的继续的状态!!
  26. CHLD:22.
  27. CHLD:23.
  28. CHLD:24.
  29. CHLD:25.
  30. CHLD:26.
  31. CHLD:27.
  32. CHLD:28.
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-01-17 09:36 |只看该作者
大神出现!!

论坛徽章:
0
3 [报告]
发表于 2014-01-17 12:16 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP