Linux: Difference between forking twice and daemon(ise)

前端 未结 2 1035
清歌不尽
清歌不尽 2021-01-20 13:44

I was trying to write a basic multiprocessing tcp-server, which forks a process for every new accept().

I don\'t need the parent process to wait on the child process

2条回答
  •  情歌与酒
    2021-01-20 14:05

    There is a subtle difference.

    Forking twice: Intermediate child process can't become a zombie provided it has exited and has been waited for by Parent. Grandchild can't become a zombie either as it's parent (intermediate child process) has exited, so grandchild is an orphan. The orphan(grandchild) gets inherited by init and if it exits now, it is the responsibility of the system to clean it up. In this way, the parent process is releived of the responsibility of waiting to collect the exit status signal from child and also the parent can be busy doing some other work. This also enables the child to run for long time so that a shorttime parent need not wait for that amount of time.

    Daemon: This is for programs wishing to detach themselves from the controlling terminal and run in the background as system daemons. Has no controlling terminal.

    The decision of approach depends on the requirement/scenario in hand.

提交回复
热议问题