我正在编写一个 deamonizing 程序。它工作正常,但不会在提到的文件中产生任何输出。我为去魔化编写的程序是否正确?另外,这个程序以相反的顺序产生输出。有人能解释为什么吗?
输出:
Closing File descriptors
Child Created.Exiting Parent
程序 :
int main(void)
{
pid_t pid, sid;
int i=0;
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0)
{
cout<<"Child Created.Exiting Parent\n";
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0)
{
exit(EXIT_FAILURE);
}
if ((chdir("/home/csgrad/suryakum/checking")) < 0)
{
cout<<"Directory not changed\n";
exit(EXIT_FAILURE);
}
cout<<"Closing File descriptors\n";
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
while (1)
{
i++;
ofstream outputFile("program3data.txt");
outputFile<< "Run "<<i<<"\n";
sleep(30); /* wait 30 seconds */
}
exit(EXIT_SUCCESS);
}