views:

109

answers:

1

I've written a daemon that writes the word "Beat" to a file, followed up the current date and time at 15 second intervals. However, each time I check the output file, the daemon appears to be outputting twice like this:

Beat: Fri Jan 1 18:09:01 2010

Beat: Fri Jan 1 18:09:01 2010

where it should only have on entry. the entire code is located at http://pastebin.com/m27a81981 (I didn't want to paste it here as the entire thing is a bit long.). The function for writing to the file is

get_time();
ofstream outputFile("heart.txt", ios::app);
beat = "\nBeat: " + gtime + "\n";
outputFile << beat;
outputFile.close();

Thanks in advance.

+5  A: 

It's because you fork() at the beginning, creating two running instances of the daemon...

Aviad P.
How do I end the first instance associated with a terminal?
Galileo
Check the return value of fork() - it will return true for one and false for the other (i think). So: `if(fork()) return;` or `if(!fork()) return;` my linux is rusty.
Aviad P.
Check for return value of the "fork()"
Hamish Grubijan
Got to love these forks ...
Hamish Grubijan
Actually, it returns 0 to the child process, and the pid of the child process to the parent. See: http://www.opengroup.org/onlinepubs/000095399/functions/fork.html
Adam Luchjenbroers