#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<signal.h>
int main( void )
{
daemon_init();
fprintf(stderr, "main进程[%d]\n", getpid() );
while( 1 )
{
}
exit(0);
}
int daemon_init()
{
pid_t pid;
if( getppid() == 1 )
{
umask( 0 );
return 0;
}
if( (pid = fork()) < 0 )
{
fprintf(stderr, "fork err!\n");
return -1;
}
else if( pid > 0 )
{
fprintf( stderr, "结束进程[%d]\n", getpid() );
exit( 0 );
}
fprintf( stderr, "孤儿进程[%d]\n", getpid() );
signal( SIGHUP, SIG_IGN );
signal( SIGQUIT, SIG_IGN );
signal( SIGCHLD, SIG_IGN );
setsid();
umask( 0 );
return 0;
}
进程控制之孤儿进程
时间: 2024-10-24 13:45:33