⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fork_test.c

📁 1、 了解系统调用fork()、execl()、exit()、getpid()和waitpid()的功能和实现过程 2、 编写一段程序实现以下功能: a) 使用系统调用fork()创建两个子进程
💻 C
字号:
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
main()
{ 
   pid_t pid;
   int i =2;
   printf("The main process is printing:id is %d\n",getpid());
   switch (pid = fork())
   {
    case -1:
     perror("the fork failed!\n");
     break;
    case 0: //pid为0,子进程。
     printf("The child'getpid is %d\n",getpid());
     printf("The child is calling an exec.the child'pid is %d\n",pid);
     execl("/bin/ps", "/bin/ps", "-a" , NULL);
     _exit(0);

    default://pid大于0,为父进程得到的子进程号
     printf("The parent is waiting for child to exit.the id is %d.\n",getpid());
     waitpid(pid, NULL, 0);
     printf("child has exited.\n");
     printf("Parent had exited.\n");

   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -