在完成 forks() 的打印语句之前在终端中打印出提示

Prompt in terminal prints out before finishing print statements for forks()

我对 fork() 进程和下面给定的代码还很陌生

#include <stdio.h>
#include <unistd.h>
int main()
{
int i;
int n = 4;
for(i=0; i<n; i++)
   fork();
printf("hello\n");
return 0;
}

打印出类似

的内容
cse [prompt] ./Program1
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
cse [prompt] hello
hello
hello
hello
hello
hello

为什么打印完所有的hello,提示又打印出来了?这对我来说真的没有意义。

保存 FORK() 返回的每个 PID。

然后对每个子进程使用函数waitpid(),这样主进程不会退出,直到所有子进程(子进程)都退出。