#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
#define MAXFILE 65535
int main(void)
{
pid_t child1,child2;
int i;
child1 = fork();
/*创建子进程1*/
if( child1 == -1 ){
perror("child1fork");
exit(1);
}
else if( child1 > 0 )
exit( 0 );
/*打开日志服务*/
openlog("exc2_info",LOG_PID, LOG_DAEMON);
/*以下几步是编写守护进程的常规步骤*/
setsid();
chdir( "/" );
umask( 0 );
for( i = 0 ; i < MAXFILE ;i++ )
{
close( i );
}
/*创建子进程2*/
child2 = fork();
if( child2 == -1 ){
perror("child2fork");
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);
}
}
}
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
#define MAXFILE 65535
int main(void)
{
pid_t child1,child2;
int i;
child1 = fork();
/*创建子进程1*/
if( child1 == -1 ){
perror("child1fork");
exit(1);
}
else if( child1 > 0 )
exit( 0 );
/*打开日志服务*/
openlog("exc2_info",LOG_PID, LOG_DAEMON);
/*以下几步是编写守护进程的常规步骤*/
setsid();
chdir( "/" );
umask( 0 );
for( i = 0 ; i < MAXFILE ;i++ )
{
close( i );
}
/*创建子进程2*/
child2 = fork();
if( child2 == -1 ){
perror("child2fork");
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);
}
}
}