关于linux底下的daemon()函数怎么用,daemon(0,0)函数是什么意思?

xiaole921 2008-03-12 01:43:59
关于linux底下的daemon()函数怎么用,daemon(0,0)函数是什么意思?
...全文
4935 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Novey 2008-08-04
  • 打赏
  • 举报
回复
DAEMON(3) Linux Programmer<A1><AF>s Manual DAEMO
N(3)

NAME
daemon - run in the background

SYNOPSIS
#include <unistd.h>

int daemon(int nochdir, int noclose);

DESCRIPTION
The daemon() function is for programs wishing to detach themselves from the controlling terminal and run in the background
as system daemons.

Unless the argument nochdir is non-zero, daemon() changes the current working directory to the root ("/").

Unless the argument noclose is non-zero, daemon() will redirect standard input, standard output and standard error to
/dev/null.

RETURN VALUE
(This function forks, and if the fork() succeeds, the parent does _exit(0), so that further errors are seen by the child
only.) On success zero will be returned. If an error occurs, daemon() returns -1 and sets the global variable errno to
any of the errors specified for the library functions fork(2) and setsid(2).

SEE ALSO
fork(2), setsid(2)

NOTES
The glibc implementation can also return -1 when /dev/null exists but is not a character device with the expected major
and minor numbers. In this case errno need not be set.

HISTORY
The daemon() function first appeared in BSD4.4.

BSD MANPAGE

自己看咯,man还是很有用的。
cceczjxy 2008-03-13
  • 打赏
  • 举报
回复

/dev/null是一个字符设备,当时不知道是一个什么样的字符设备,请指教!
------------------------------------------------------------
你就认为是个垃圾箱就可以了,永远填不满,不会有数据产生。就是一个黑洞。
xiaole921 2008-03-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 littlefirebug 的回复:]
#include <unistd.h>

int daemon(int nochdir,int noclose)

在创建精灵进程的时候,往往需要将精灵进程的工作目录修改为"/"根目录
并且将标准输入,输出和错误输出重定向到/dev/null

daemon的作用就是当参数nochdir为0时,将根目录修改为工作目录
noclose为0时,做输入,输出以及错误输出重定向到/dev/null

执行成功返回0
错误返回-1
[/Quote]
/dev/null是一个字符设备,当时不知道是一个什么样的字符设备,请指教!
xiaole921 2008-03-13
  • 打赏
  • 举报
回复
daemon()函数,还有其他的用法吗?谁有比较详细的说明文档,共享以下!
lbaby 2008-03-12
  • 打赏
  • 举报
回复
可以自己写


#include "apue.h"

#include <syslog.h>

#include <fcntl.h>

#include <sys/resource.h>



void

daemonize(const char *cmd)

{

int i, fd0, fd1, fd2;

pid_t pid;

struct rlimit rl;

struct sigaction sa;

/*

* Clear file creation mask.

*/

umask(0);



/*

* Get maximum number of file descriptors.

*/

if (getrlimit(RLIMIT_NOFILE, &rl) < 0)

err_quit("%s: can't get file limit", cmd);



/*

* Become a session leader to lose controlling TTY.

*/

if ((pid = fork()) < 0)

err_quit("%s: can't fork", cmd);

else if (pid != 0) /* parent */

exit(0);

setsid();



/*

* Ensure future opens won't allocate controlling TTYs.

*/

sa.sa_handler = SIG_IGN;

sigemptyset(&sa.sa_mask);

sa.sa_flags = 0;

if (sigaction(SIGHUP, &sa, NULL) < 0)

err_quit("%s: can't ignore SIGHUP");

if ((pid = fork()) < 0)

err_quit("%s: can't fork", cmd);

else if (pid != 0) /* parent */

exit(0);



/*

* Change the current working directory to the root so

* we won't prevent file systems from being unmounted.

*/

if (chdir("/") < 0)

err_quit("%s: can't change directory to /");



/*

* Close all open file descriptors.

*/

if (rl.rlim_max == RLIM_INFINITY)

rl.rlim_max = 1024;

for (i = 0; i < rl.rlim_max; i++)

close(i);



/*

* Attach file descriptors 0, 1, and 2 to /dev/null.

*/

fd0 = open("/dev/null", O_RDWR);

fd1 = dup(0);

fd2 = dup(0);



/*

* Initialize the log file.

*/

openlog(cmd, LOG_CONS, LOG_DAEMON);

if (fd0 != 0 || fd1 != 1 || fd2 != 2) {

syslog(LOG_ERR, "unexpected file descriptors %d %d %d",

fd0, fd1, fd2);

exit(1);

}

}



//来自apue 2e 的
littlefirebug 2008-03-12
  • 打赏
  • 举报
回复
#include <unistd.h>

int daemon(int nochdir,int noclose)

在创建精灵进程的时候,往往需要将精灵进程的工作目录修改为"/"根目录
并且将标准输入,输出和错误输出重定向到/dev/null

daemon的作用就是当参数nochdir为0时,将根目录修改为工作目录
noclose为0时,做输入,输出以及错误输出重定向到/dev/null

执行成功返回0
错误返回-1

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧