Exit and wait together to completely clear the resources of the child process

Source: Internet
Author: User

#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<stdlib.h>#include<errno.h>intMain () {pid_t p1,p2,pr; inti;  for(i=0; i<=2; i++)  {   if((P1=fork ()) = =0) {printf ("parent process%d Child process%d\n", Getppid (), Getpid ()); Exit (0); }  Else{PR=Wait (NULL);//If successful, wait returns the process ID of the child process being collected,//if the calling process does not have child processes, the call fails, at which time wait returns-1 while errno is set to Echild.   if(pr>0) printf ("I catched A child process with PID of%d\n", PR); Elseprintf ("Error:%s\n.\n", Strerror (errno)); } } return 0;} The output results are as follows: Parent process4595 child process4596i catched A child process with PID of4596parent process4595 Child process4597i catched A child process with PID of4597parent process4595 Child process4598i catched A child process with PID of4598when the exit comment is shown, the output is as follows: Parent process4642 child process4643parent process4643 child process4644parent process4644 Child PR ocess4645i catched a child process with PID of4645I catched A child process with PID of4644parent process4643 Child process4646i catched A child process with PID of4646I catched A child process with PID of4643parent process4642 Child process4647parent process4647 child process4648i catched A child process with PID of4648I catched A child process with PID of4647parent process4642 Child process4649i catched A child process with PID of4649//The result of the re-execution sideparent process4657 Child process4658parent process4658 child process4659parent process4659 child process4660i Catch Ed a child process with PID of4660I catched A child process with PID of4659parent process4658 Child process4661i catched A child process with PID of4661I catched A child process with PID of4658parent process4657 Child process4662parent process4662 child process4663i catched A child process with PID of 4663I catched A child process with PID of4662parent process4657 Child process4664i catched A child process with PID of4664from the above output can be obtained: Exit can be used to release the resources of the process, it must be added, when commented out, it may be because the child process resources are not cleaned up in time, so that wait blocked, can not clean up the child process in a timely manner! Summary: When the process issues exit the call, the kernel frees the resources that the process occupies, frees up the memory space occupied by the process context, preserves the process table entries, and sets the key that records the status of the process in the process table item to a zombie state. The kernel calls exit from within the kernel when the process receives a non-capturing signal, which causes the process to exit. The parent process passes wait, and the process table entry is freed. 

Exit and wait together to completely clear the resources of the child process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.