pid_t child1, child2;}
int i;
child1 = fork();//创建子进程1
if(child1 == 1)
{
perror("child1 fork.\n");}
exit(1);
else if(child1 > 0)
{
exit(0);//父进程退出}
openlog("daemon_proc_info", LOG_PID, LOG_DAEMON);//打开日志服务
setsid();//子进程中创建新会话
chdir("/");//改变当前目录为根目录
umask(0);//重设文件权限码
for(i = 0; i < getdtablesize(); i ++)
{
close(i);//关闭文件描述符}
child2 = fork();//创建子进程2
if(child2 == 1)
{
perror("child2 fork.\n");}
exit(1);
else if(child2 == 0)//子进程2中
{
syslog(LOG_INFO, "child2 will sleep for 10s.\n");//向日志中写入字符串}
sleep(10);
syslog(LOG_INFO, "child2 is going to exit!\n");
exit(0);
else//父进程(子进程1)
{
waitpid(child2, NULL, 0);//阻塞等待}
syslog(LOG_INFO, "child1 noticed that child2 has exited.\n");
closelog();//关闭日志服务
while(1)
{
sleep(10);}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |