7月 032009
 

下面是memcached中初始化一个daemo的代码,如果我们自己要写daemo的话,这个可以参考:

1. fork时使用了switch结构,而不是if … else

2. 提供了一个参数,决定是否重定向标准输入、标准输出、标准错误

#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int daemon(int nochdirint noclose)
{
    
int fd
;

    switch (fork()) {
    case –
1
:
        return (-
1
);
    case 
0
:
        break;
    default:
        
_exit(EXIT_SUCCESS
);
    }

    if (setsid() == –1)
        return (-
1
);

    if (nochdir == 0)
        (
void)chdir("/"
);

    if (noclose == && (fd open("/dev/null"O_RDWR0)) != –1) {
        (
void)dup2(fdSTDIN_FILENO
);
        (
void)dup2(fdSTDOUT_FILENO
);
        (
void)dup2(fdSTDERR_FILENO
);
        if (
fd STDERR_FILENO
)
            (
void)close(fd
);
    }
    return (
0
);
}

 Posted by at 上午 11:13

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据