void setdaemon(const char * pid_file)
{
pid_t pid;
if(!pid_file)
{
return;
}
if((pid = fork()) < 0)
{
exit(-1);
}
if(pid)
{
exit(0);
}
setsid();
if ((pid = fork()) < 0)
{
exit(-1);
}
if (pid)
{
exit(0);
}
write_pid(pid_file);
}
void test()
{
setdaemon("daemon.pid");
while(1)
{
print("daemon process!\n");
sleep(1);
}
}
void main()
{
pid_t pid = -1;
int exitcode = 0;
pid = fork();
if(pid == 0)
{
test();
}
pid = wait(&exitcode);
exit(0);
}
Linux创建后台进程
最新推荐文章于 2021-12-08 07:57:44 发布