关联课程:https://blog.csdn.net/search_129_hr/category_11784317.html
本次作业采用c语言于Linux环境下使用老师提供的代码完成,参考代码如下:
#include#include #include int main() { pid_t tempPid, tempW; tempPid = fork(); if (tempPid == -1){ perror("fork error"); exit(1); } else if (tempPid == 0){ sleep(3); printf("Child process:pid=%dn", getpid()); exit(0); } else { do{ tempW = waitpid(tempPid, NULL, WNOHANG); if (tempW == 0){ printf("No child exitedn"); sleep(1); }//of if } while (tempW == 0); if (tempW == tempPid){ printf("Catch a Child process:pid=%dn", w); }else{ printf("waitpid errorn"); }//of if }//of if return 0; }//of main
本次作业运用c语言中waitpid函数,主要作用为回收本进程产生的指定子进程。在本段代码中,此函数被父进程循环调用以监控子进程的运行状态。在发现指定的子进程执行完毕后,函数负责回收该子进程防止其转化为僵尸进程且产生危害。
运行过程:



