/********************************************** 创建一个子进程,打印hello后父进程打印world,之后父进程再创建两条进程,一个进程打印end,另一个进程打印... **********************************************/ #include #include #include #include int main() { // 创建子进程 pid_t pid = fork(); // 错误处理 if (pid < 0) { printf("创建子进程失败\n"); return -1; } // 子进程 else if (pid == 0) { printf("hello\n"); } // 父进程 else { wait(NULL); // 等待子进程结束 printf("world\n"); // 创建两个新的子进程 pid_t pid1 = fork(); if (pid1 == 0) { printf("end\n"); } else { pid_t pid2 = fork(); if (pid2 == 0) { printf("...\n"); } } } return 0; // 程序结束 }