示例#1
0
int cleanup_terminated_children(void)
{
    pid_t pid;
    int status;
    int count = 0;

    while (1)
    {
        pid = waitpid(-1, &status, WNOHANG);

        if (pid == 0)             /* returns 0 if no child process to wait for */
        {
            break;
        }

        if (pid == -1)            /* returns -1 if there was an error */
        {
            /* errno will have been set by waitpid() */
            if (errno == ECHILD)  /* no children */
            {
                break;
            }
            if (errno == EINTR)   /* waitpid() was interrupted by a signal */
            {
                continue;    /* try again */
            }
            else
            {
                printf("unexpected error in cleanup_terminated_children(): %s\n",
                       strerror(errno));
                break;
            }
        }
        //print_wait_status(pid, status);      /* supply this yourself */
        printf("process %d, completed normally, status %d\n", pid, status);
        update_process_table(pid, status);
        remove_process_table(pid);
        count++;
    }

    return count;
}
示例#2
0
文件: pr7.4.c 项目: Sagarp91/Proj7
// Function to check for any terminated child processes, and remove
// them from the process table
int cleanup_terminated_children()
{
    pid_t pid;
    int status;
    int count = 0;

    while (1)
    {
        pid = waitpid(-1, &status, WNOHANG);

        if (pid == 0)                             /* returns 0 if no child process to wait for */
            { break; }

        if (pid == -1)                        /* returns -1 if there was an error */
        {
                                                /* errno will have been set by waitpid() */
            if (errno == ECHILD)                  /* no children */
                { break; }
                if (errno == EINTR)               /* waitpid() was interrupted by a signal */
            {                                     /* try again */
                continue;
            }
            else
            {
                printf("unexpected error in cleanup_terminated_children(): %s\n",
                    strerror(errno));
                break;
            }
        }

        if(pr7_debug)
            printf("pid is %d, status is %d\n", (int)pid, status);
        update_process_table(process_table, pid, status);
        remove_process_table(process_table, pid);
        count++;
    }

    return count;
}