免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1664 | 回复: 2
打印 上一主题 下一主题

[C] 求代码分析 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-27 16:09 |只看该作者 |倒序浏览
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <math.h>

#define MYPATHSTR       "/"
#define NULLDEV        "/dev/null"       // path of the null device
#define true 1
#define false 0
typedef  char  bool;

bool ttdaemonize(void){
  fflush(stdout);
  fflush(stderr);
  switch(fork()){
  case -1: return false;
  case 0: break;
  default: _exit(0);
  }
  if(setsid() == -1) return false;
  switch(fork()){
  case -1: return false;
  case 0: break;
  default: _exit(0);
  }
  umask(0);
  if(chdir(MYPATHSTR) == -1) return false;
  close(0);
  close(1);
  close(2);
  int fd = open(NULLDEV, O_RDWR, 0);
  if(fd != -1){
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
    if(fd > 2) close(fd);
  }
  return true;
}

int main(void)
{
        if (ttdaemonize())
        {
        }
}

哪位大神给分析下这段代码啊,守护进程为什么要调用两次fork等等的

论坛徽章:
0
2 [报告]
发表于 2014-08-27 18:06 |只看该作者
apue有说的,这是为了避免僵尸进程的产生。

论坛徽章:
0
3 [报告]
发表于 2014-08-28 09:57 |只看该作者
ok 非常感谢回复 2# Fixend


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP