作业社区
探索学习新天地,共享知识资源!
慕运维8597106 的学生作业:
#include #include #include #include #include #include int main() { pid_t cpid = fork(); if (cpid == -1) { perror("[ERROR fork]"); exit(EXIT_FAILURE); } else if (cpid == 0) { printf("child process A running:%ld\n", time(NULL)); sleep(2); exit(EXIT_SUCCESS); } else if (cpid > 0) { pid_t cpid2 = fork(); if (cpid2 == -1) { perror("[ERROR fork]"); exit(EXIT_FAILURE); } else if (cpid2 == 0) { printf("child process B running:%ld\n", time(NULL)); sleep(5); exit(EXIT_SUCCESS); } else if (cpid2 > 0) { int status1, status2 = 0; while ((waitpid(cpid, &status1, WNOHANG)) == 0 || (waitpid(cpid2, &status2, WNOHANG) == 0)) { } } } printf("All child processes have finished:%ld\n", time(NULL)); return 0; } 【图片】
+114