#include <stdlib.h> #include <stdlib.h> int main(){ int i; pid_t pid = fork(); printf("xxxxxxxxx\n"); for(i = 0; i < 5; i++){ pid = fork(); //循环创建子进程 if(pid == -1){ perror("fork error"); }else if(pid == 0){ break; //子进程直接退出 } //主进程 循环创建 } if(i < 5){
sleep(i); printf("i'm %d child, pid = %d\n", i+1,getpid()); //打印自己退出 }else{
sleep(i);
printf("i'm %d parent\n");
} return 0; }