On 8/14/06, Brett W. McCoy <[EMAIL PROTECTED]> wrote:
> On 8/14/06, Bryan Irvine <[EMAIL PROTECTED]> wrote:
>
> > I'm trying to write a simple daemon. My first real C program so go
> > easy on me. ;)
> >
> > I'm trying to get the daemon to not start a second copy (if someone
> > were to try). What's the best way to do something like that? Check
> > the pid file?
>
> I assume you are using Unix or similar (Linux, OSX), since you don't specify.
yes, I'm testing it on Madriva, and OpenBSD (i386 and sparc64).
> Simplest thing, for a simple daemon, is to create a file containing
> the pid, lock it and remove the file when the process exits. This does
> create problems if the daemon exits abnormally (like a segfault) and
> doesn't clean up after itself. There are more advanced things you can
> do, perhaps using a semphore or mutex, but I'd go the easier route
> first.
I have the daemon creating a file containing the pid. I wasn't sure
how to go about having the daemon check for the existence of the file
beforehand. I saw in some older thread about using access() but then
someone else in that thread said that access() isn't in ISO.
presently that part of the code look like so:
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
if ((chdir("/var/run/")) < 0) {
/* failed */
exit(EXIT_FAILURE);
}
pidFile = fopen ("mirror.pid", "wt");
fprintf (pidFile, "%i", pid);
fclose (pidFile);
exit(EXIT_SUCCESS);
}
To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/