/*************************************************** 子进程每隔1秒打印一次apple,父进程每隔2秒打印一次hello。 ****************************************************/ #include #include #include int main() { // 创建子进程 pid_t pid = fork(); // 错误处理 if (pid < 0) { printf("创建子进程失败\n"); return -1; } // 子进程 else if (pid == 0) { while (1) { printf("apple\n"); sleep(1); // 每隔1秒打印一次apple } } // 父进程 else { while (1) { printf("hello\n"); sleep(2); // 每隔2秒打印一次hello } } return 0; // 程序结束 }