#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAXFILE 65535
#include <iostream>
using namespace std;
int main()
{
pid_t pid;
int i;
pid = fork();
if (pid < 0)//fork() call failed
{
printf("error in fork\n");
exit(1);
}
else if (pid > 0)
{
exit(0);
}
setsid();// must be called
chdir("/");//change current working directory to root
umask(0);
//close all unnecessary file
for(i = 0; i < MAXFILE; i++)
{
close(i);
}
//do your program logic here
for(;;)
sleep(10);
}
Linux Daemon learning
最新推荐文章于 2022-06-24 23:14:37 发布