学习守护进程看b站视频时这段代码请问什么左右啊
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
if(fd>2)
close(fd);
全部代码是这个
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#define FNAME "/tmp/out"
int daemonize()
{
int fd;
pid_t pid;
pid = fork();
if(pid<-1)
{
perror("fork");
return -1;
}
if(pid>0)
{
exit(0);
}
fd = open("/dev/null", O_RDWR);
if(fd<0)
{
perror("open");
return -1;
}
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
if(fd>2)
close(fd);
setsid();
chdir("/");
return 0;
}
int main()
{
FILE * fp;
int i;
if(daemonize())
exit(-1);
fp = fopen(FNAME, "w");
if(fp == NULL)
{
perror("Failed to open");
exit(1);
}
for( i = 0; ;i++)
{
fprintf(fp, "%d\n", i);
fflush(fp);
sleep(1);
/* code */
}
exit(0);
}