简体   繁体   中英

Is mine process still a daemon?

I'm relatively new to programming and my English might be little rusty so please be gentle :).

I'm trying to create a group of daemons that will belong to same group without group leader. I think I did it but I'm not sure if my method still meets the requirements (if my process is still a daemon)

This part is in 0.c

pid_t pid=fork();
if (pid==0)
{
    execl ("./1", "1", str, (char *)0);
    exit(EXIT_SUCCESS);
    sleep(1);
}

This part of code is in 1.c

pid = fork();
if (pid < 0)
    exit(EXIT_FAILURE);
if (pid > 0)
    exit(EXIT_SUCCESS);
gid = setsid();
if (gid < 0)
    exit(EXIT_FAILURE);
gid = setsid();
if (gid < 0)
    exit(EXIT_FAILURE);
for(i=0; i<n; i++)
{
pid_t pid=fork();
    if (pid==0)
    {
        execl ("./2", "2", str, str1, (char *)0);
        exit(EXIT_SUCCESS);
        sleep(1);
    }
}

and this in 2

pid_t pid;
pid = fork();
if (pid < 0)
    exit(EXIT_FAILURE);
if (pid > 0)
    exit(EXIT_SUCCESS);
umask(0);
chdir("./");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

Now let's say I have n processes 2 are they all daemons?

EDIT: I see now that those probably aren't daemons. Can someone explain to me then how to make daemons that will meet my requirements?

Normally daemons need to disassociate from their controlling terminal, or at least ignore SIGHUP , and you don't appear to be doing this.

\n

So no, I do not think your code is achieving daemon status.

After further investigation, I found that setsid() disassociates the controlling terminal, among other actions. So please disregard this.

Anything with the PPID of 1 is, for the most part, likely a daemon. You can check the likelihood that your process is a daemon then by executing: (command line)

$ ps -xj

There is much more here discussing ways to test for daemon.

And here discussing creating a daemon in general. (using C)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM