Jarno
Jarno

Reputation: 153

C fork not exiting

I am fairly new to C and I wrote the code below to exec some shell script (nsdc.sh) every 10 seconds. For some reason it is calling the shell script many times (in a loop). It seems the _exit is not being called. Can anyone explain this behavior to me?

#include "dynzone.h"

void
dynzone_run(struct nsd *nsd)
{
    while(1) {
        pid_t pid = fork();

        if(pid == -1) {
            log_msg(LOG_ERR, "fork zone reload failed");
        } else if(pid == 0) {
            /* CHILD */
            log_msg(LOG_NOTICE, "exec reload");

            if(system("/home/edns/jarno/v1/nsdc.sh reload") == -1) {
                printf("reload error: %s\n", strerror(errno));
            }

            _exit(1);
        }

        sleep(10);
    }
}

void
dynzone_spawn(struct nsd *nsd)
{
    pid_t pid;

    pid = fork();

    if(pid == -1) {
        log_msg(LOG_ERR, "fork dynzone failed");
    } else if(pid == 0) {
        /* CHILD */
        log_msg(LOG_NOTICE, "spawned dynzone");

        dynzone_run(nsd);

        /* ENOTREACH */
        exit(0);
    }

    /* PARENT */
    return;
}

Thanks in advance!

Upvotes: 0

Views: 116

Answers (1)

Frunsi
Frunsi

Reputation: 7157

So, after usual deduction and trusting in all aspects of your description, there could be only one possible path of execution, which leads to your observed result:

Your "nsdc.sh" script will never exit at all.

Right?

Upvotes: 1

Related Questions