Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it necessary to close standard input/output/error when writing a daemon?

Tags:

linux

unix

daemon

Why is it necessary to close standard input/output/error when writing a (unix) daemon?

like image 446
d-_-b Avatar asked Dec 27 '22 03:12

d-_-b


1 Answers

Not only stdin, stdout and stderr should be closed, but all open files.

From "Advanced Programming in the UNIX Environment", W. Richard Stevens, Addison-Weseley, 18th Printing, 1999, page 417.

Unneeded file descriptors should be closed. This prevents the daemon from holding open any descriptors that is may have inherited from its parent (which could be a shell or some other process).

Mr. Stevens proposal is to get the maximum file descriptor and close all files up to that value.

The cited chapter is about 'Daemon Processes'. Please note that closing file descriptors is only one point from five when writing daemons.

like image 109
Andreas Florath Avatar answered Apr 06 '23 18:04

Andreas Florath