Fork wait in background process - large delay

hi all,

We are trying to run a process in the background and in the process we call fork ;and wait for the child process to finish .We find that the died = wait(&status); happens after 10 seconds randomly and sometimes completes in time (within 1 sec)

This behavior is seen only when the program is run as a background process. With foreground the functionality is always within 1 sec.

Can anyone please suggest the reason for this random scheduling in Linux - Operating system is CentOS

    switch ( pid = fork())
    {
        case -1:
            DbgMsg( DBG_ERR, "Cannot fork for compile!\n");
            single--;
            return -1;
        case 0:
            execvp( file, argv);
            DbgMsg(DBG_ERR | DBG_COMPILER, "Should never happen\n");
        default:
            {
waitloop:
            died = wait(&status);
            if (died == pid)
                break;
            if (died == -1) {
#if 0 /* on systems that don't map errno to __error() */
                extern int errno;
#endif

                DbgMsg(DBG_WARNING | DBG_COMPILER, "Compilation error (%s)\n", strerror(errno));
                switch (errno) {
                    case ECHILD:
                    case EINTR:
                        break;
                    default:
                        DbgMsg( DBG_WARNING, "Unexpected compilation error (%d)\n", errno);
                        goto waitloop;
                }
            }
            DbgMsg( DBG_WARNING, "Wait returned for other child %x\n", died);
            goto waitloop;
            }
    }

Thanks in advance for any help
Thanks
Vishnu

Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.