
作业社区
探索学习新天地,共享知识资源!
蜡笔小方哎 的学生作业:
#include #include #include #include #include int main() { pid_t pid_1 = fork(); if(pid_1 == -1) { perror("fork() error\n"); exit(-1); } else if(pid_1 == 0) { printf("child process A is created, pid is %d\n", getpid()); sleep(2); printf("child process A exit\n"); exit(0); } else { pid_t pid_2 = fork(); if(pid_2 == -1) { perror("fork() error\n"); exit(-1); } else if(pid_2 == 0) { printf("child process B is created, pid is %d\n", getpid()); sleep(5); printf("child process B exit\n"); exit(0); } else { while(waitpid(pid_1, NULL, WNOHANG) == 0) { printf("waiting for child process A exit...\n"); sleep(1); } while(waitpid(pid_2, NULL, WNOHANG) == 0) { printf("waitding for child process B exit...\n"); sleep(1); } } } return 0; }