[C.C++] 写一段简单的任务门测试程序

1242 0
狼毛 2023-8-25 19:10:03 | 显示全部楼层 |阅读模式
//写一段简单的任务门测试程序

  1. // 写一段简单的任务门测试程序
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <errno.h>

  10. int main(int argc, char *argv[])
  11. {
  12.     int i;
  13.     int status;
  14.     int n = 5;
  15.     pid_t pid;
  16.     for (i = 0; i < n; i++)
  17.     {
  18.         pid = fork();
  19.         if (pid == 0)
  20.         {
  21.             break;
  22.         }
  23.         else if (pid < 0)
  24.         {
  25.             perror("fork");
  26.             exit(1);
  27.         }
  28.         else
  29.         {
  30.             continue;
  31.         }
  32.     }
  33.     if (i == n)
  34.     {

  35.         while (wait(&status) > 0)
  36.         {
  37.             if (WIFEXITED(status))
  38.             {
  39.                 printf("child %d exit with code %d\n", WEXITSTATUS(status));
  40.                 continue;
  41.             }
  42.             else if (WIFSIGNALED(status))
  43.             {
  44.                 printf("child %d exit with signal %d\n", WTERMSIG(status));
  45.                 continue;
  46.             }
  47.             else if (WIFSTOPPED(status))
  48.             {
  49.                 printf("child %d exit with signal %d\n", WSTOPSIG(status));
  50.                 continue;
  51.             }
  52.             else
  53.             {
  54.                 printf("unknow status\n");
  55.                 continue;
  56.             }
  57.         }
  58.         if (errno != ECHILD)
  59.         {
  60.             perror("wait");
  61.             exit(1);
  62.         }
  63.         else
  64.         {
  65.             printf("all child exit\n");
  66.             return 0;
  67.         }
  68.     }
  69.     else
  70.     {
  71.         sleep(i);
  72.         printf("child %d exit\n", i);
  73.         return 0;
  74.     }
  75.     return 0;
  76. }
复制代码




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

狼毛

精英红客

关注
  • 206
    主题
  • 2
    粉丝
  • 1
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行