[C.C++] 写一段双进程保护的程序

1136 0
狼毛 2023-8-25 20:21:47 | 显示全部楼层 |阅读模式
  1. //写一段双进程保护的程序
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. int main()
  8. {
  9.     pid_t pid;
  10.     int status;
  11.     pid = fork();
  12.     if(pid < 0)
  13.     {
  14.         perror("fork");
  15.         exit(1);
  16.     }
  17.     else if(pid == 0)
  18.     {
  19.         printf("child process\n");
  20.         sleep(5);
  21.         exit(0);
  22.     }
  23.     else
  24.     {
  25.         printf("parent process\n");
  26.         wait(&status);
  27.         printf("child exit code = %d\n",status>>8);
  28.         exit(0);
  29.     }
  30.     return 0;
  31. }
  32.                

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

本版积分规则

狼毛

精英红客

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

中国红客联盟公众号

联系站长QQ:5520533

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