[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fork and vfork and daemon processes


I'm a little new to Linux programming so bare with me.

I read somewhere that fork is partially implemented... it works but you
have to be careful in your code.
What I'm trying to do is turn my regular application that accesses both com
ports to function correctly
as a daemon process.   I've tried a number of different daemon types of
detach routines but for some
reason when run from inittab the processes cause the whole ETRAX board to
behave in an odd way.
It's as though the ETRAX board is resetting during boot or something.  Has
anyone had similar difficulties?
If you have, how did you overcome the problems? was it due to fork() ?

By the way, I don't think it would be an issue but just in case, I'm using

Here is the code I'm using, taken from the UNIX programming FAQ

/* closeall() -- close all FDs >= a specified value */

void closeall(int fd)
    //int fdlimit = sysconf(_SC_OPEN_MAX);  // appears to be no sysconf
    int fdlimit = 3;

    while (fd < fdlimit)

/* daemon() - detach process from user and disappear into the background
 * returns -1 on failure, but you can't do much except exit in that case
 * since we may already have forked. This is based on the BSD version,
 * so the caller is responsible for things like the umask, etc.
int daemon(int nochdir, int noclose)
    switch (fork())
        case 0:  break;
        case -1: return -1;
        default: _exit(0);          /* exit the original process */

    if (setsid() < 0)               /* shoudn't fail */
      return -1;

    /* dyke out this switch if you want to acquire a control tty in */
    /* the future -- not normally advisable for daemons */

    switch (fork())
        case 0:  break;
        case -1: return -1;
        default: _exit(0);

    if (!nochdir)

    if (!noclose)
        dup(0); dup(0);

    return 0;

CAUTION:  The information contained in this mail message is
confidential and may be legally privileged. No confidentiality
or privilege is waived or lost by any mistransmission. If the
reader of this message is not the intended recipient, you are
hereby notified that any use, dissemination, or reproduction of
this message is prohibited. If you have received this message in
error please notify the sender immediately by email and destroy
the original message. Thank you.