Daemon and Zombie Processes

Source: Internet
Author: User

Zombie Process: In the case of a child process does not exit, the parent process exits, the child process is taken over by the Init process, but the child process will not exit normally, and will not be withdrawn after the process's resources, so this situation should be avoided, Otherwise, the system resources will be consumed. To avoid this, you can use the Vfork function to derive from a child process, or a function in the parent process to wait for the child process to exit:

Header Files <sys/types.h><sys/wait.h>
int wait (int *status);//suspend execution of the current process until a signal is activated or the child process finishes executing
int waitpid (int pid,int *status,int options);
Wait for the PID to represent the completion of the execution of the child process, PID =-1, equivalent to wait. Wait for any child process

---------------------------------------------------------------------------------------------------------

Daemon: has been running in the background, the parent process is init, and the zombie process is similar, the difference is that the daemon is more of a service process, it is necessary to always provide services so can not quit.

General authoring steps for Daemons:

    1. Detach the control terminal (parent process) to make the parent process init
    2. Prohibit process from reopening control terminal
    3. Close file descriptor (inherits file descriptor from parent process, preferably close first, avoids unnecessary waste of system resources)
    4. Change Working directory (avoid working directory being uninstalled)
    5. Resetting the file creation mask (avoid the permissions and permissions we need when creating the file)
    1. int c = 0;
    2. int pid = fork ();
    3. if (PID > 0) {//exit parent process to make its parent process init
    4. Exit (0);
    5. }
    6. if (PID < 0) {//child process creation failed to exit
    7. Exit (1);
    8. }
    9. for (; c < 101122;c++) {//Close file descriptor
    10. Close (c);
    11. }
    12. ChDir ("/"); Change Working directory
    13. Umask (0); Reset File Creation Mask
    14. while (1) {//Dead loop keeps it running in the background
    15. The work required to prepare the process
    16. }

Daemon and Zombie Processes

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.