Chinaunix首页 | 论坛 | 博客
  • 博客访问: 687099
  • 博文数量: 240
  • 博客积分: 3616
  • 博客等级: 大校
  • 技术积分: 2663
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-21 23:59
文章分类

全部博文(240)

文章存档

2013年(6)

2012年(80)

2011年(119)

2010年(35)

分类: LINUX

2010-10-25 17:57:57

#include
#include
#include
#include
#include
#include
#define MAXFILE 65535
int main(void)
{
 pid_t child1,child2;
 int i;
 child1=fork();//创建子进程1
 if(child1==-1)//出错判断
 {
  perror("child1 fork");
  exit(1); 
 }
 else if(child1>0)//如果运行在父进程就退出
 {
  exit(0);
 }
 openlog("exc2_info",LOG_PID,LOG_DAEMON);//打开日志服务
 setsid();//下面就开始编写守护进程
 chdir("/");
 umask(0);
 for(i=0;i {
  close(i);
 }
 child2=fork();//创建子进程2
 if(child2==-1)
 {
  perror("child2 fork");
  exit(1);
 }
 else if (child2==0)//在日志中写入字符串
 {
  syslog(LOG_INFO,"child2 will sleep for 10s");
  sleep(10);
  syslog(LOG_INFO,"child2 is going to exit !");
  exit(0);
 }
 else
 {
  waitpid(child2,NULL,0);//关闭日志服务
  syslog(LOG_INFO,"child1 noticed that child2 has exited");
  closelog();
  while(1)
  {
   sleep(10); 
  } 
 }
}
阅读(679) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-10-26 18:28:58

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com