#include "head.h"
int main(int argc, char const *argv[])
{
pid_t pid[9] = {0};
int i = 0;
for (i = 0; i < 9; i++)
{
pid[i] = fork();
if (-1 == pid[i])
{
perror("fail to fork\n");
return -1;
}
if (0 == pid[i])
{
printf("parent PPID %d child PPID %d\n", getppid(), getpid());
exit(0); //为了防止子进程重新创建子进程
}
}
sleep(1); //为了等子进程运行完毕再打印父进程
printf("parent PPID %d child PPID ", getpid());
for (i = 0; i < 9; i++)
{
printf(" %d", pid[i]);
}
printf("\n"); //为了将父进程 后面跟九个子进程
while(1) //阻止父进程结束
{
}
/* code */
return 0;
}
效果完美