什么叫守护进程(),并调用fork(),setsid(),叉()等之间的区别? [英] What's the difference between calling daemon() and calling fork(), setsid(), fork(), etc.?
问题描述
我一直在寻找创造Unix的守护进程,似乎有两种方法。在啰嗦一句,这似乎拿出搜索时是调用叉()
, setsid()
, 叉()
再次 CHDIR()
一个安全的地方,设置的umask()
,最后的close()
标准输入
,标准输出
和标准错误
。
I've been looking at creating Unix dæmons, and there seem to be two methods. The long-winded one, which seems to come up when searching is to call fork()
, setsid()
, fork()
again, chdir()
to somewhere safe, set umask()
and, finally, close()
stdin
, stdout
and stderr
.
运行男人守护
,然而这样带来了()在守护信息
功能,这似乎尽一切同样的东西如上。是否有两种方法之间的差异或守护进程()
只是一个方便的功能,做同样的事情啰嗦的方法?要么是一个人好,尤其是对一个新手C程序员?
Running man daemon
, however, brings up information on a daemon()
function, which seems to do all the same stuff as above. Are there any differences between the two approaches or is daemon()
just a convenience function that does the same thing as the long-winded method? Is either one better, especially for a novice C programmer?
推荐答案
的后台
函数没有在POSIX中定义,所以它的实现(如果有的话)可以表现不同的不同的平台。
The daemon
function is not defined in POSIX, so its implementation (if any) could behave differently on different platforms.
在Linux中使用的glibc,后台
只做一岔路口,可选 CHDIR
S(但仅限于 /
,你不能指定路径),不碰的umask
,并且不关闭 STD *
描述符(它选择性地将这些重新开放的/ dev / null的
虽然)。 (源)
On Linux with glibc, daemon
only does one fork, optionally chdir
s (but only to /
, you can't specify a path), does not touch umask
, and does not close the std*
descriptors (it optionally reopens them to /dev/null
though). (source)
因此,它取决于平台,以及至少一个实现不低于你做什么。如果你需要的是你在做什么,坚持与(或坚持一个平台,其中后台
函数正是这么做的)。
So it depends on the platform, and at least one implementation does less than what you do. If you need all of what you're doing, stick with that (or stick to a platform where the daemon
function does exactly that).
这篇关于什么叫守护进程(),并调用fork(),setsid(),叉()等之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!