您的位置:首页 > 运维架构 > Linux

linux程序后台调度代码写法(没有终端)

2014-04-26 23:34 197 查看
/* 该段代码要在进程的开写,不然close那里可能出异常 */
int initprocess( const char * workdir)

{

int i;

pid_t pid;

if ( (pid = fork()) < 0)

return (-1);

else if (pid)

_exit(0); /* parent terminates */

/* child 1 continues... */

if (setsid() < 0) /* become session leader */

return (-1);

if( signal( SIGHUP, SIG_IGN ) != SIG_ERR )

{

}

if( signal( SIGPIPE, SIG_IGN ) != SIG_ERR )

{

}

if( signal( SIGALRM, SIG_IGN ) != SIG_ERR )

{

}

if( signal( SIGCHLD, SIG_IGN ) != SIG_ERR )

{

}

if ( (pid = fork()) < 0)

return (-1);

else if (pid)

_exit(0); /* child 1 terminates */

/* child 2 continues... */

if( NULL != workdir ) chdir( workdir ); /* change working directory */

/* close off file descriptors */

for (i = 0; i < 64; i++)

close(i);

/* redirect stdin, stdout, and stderr to /dev/null */

open("/dev/null", O_RDONLY);

open("/dev/null", O_RDWR);

open("/dev/null", O_RDWR);

return (0); /* success */

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐