狂自私

导航

用waitpid函数回收进程

waitpid函数:作用同wait,但可指定pid进程清理,可以不阻塞。

原型:pid_t waitpid(pid_t pid, int *status, in options);

返回值:成功时返回清理掉的子进程ID,失败返回-1;当第三个参数被设置为WNOHANG,且子进程还在运行时,返回0;

参数说明:

pid

> 0 回收指定ID的子进程  。

-1 回收任意子进程(相当于wait)

0 回收和当前调用waitpid一个组的所有子进程。

< -1 回收指定进程组内的任意子进程。

status

存储进程死亡的信息。

options

设置为阻塞或者不阻塞状态。

WNOHANG:不阻塞

0:阻塞

现在有一个题目:父进程fork 3 个子进程,三个子进程一个调用ps命令, 一个调用自定义程序1(正常),一个调用自定义程序2(会出段错误)。父进程使用waitpid对其子进程进行回收。

很简单,就是一些函数的调用。

代码:

#include <cstdio>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

 

int main()

{

    int i = 0;

    int status; //存储进程死亡信息

    pid_t pid; //存储wait函数返回值

 

    for (; i != 3; i++)

    {

        if (!fork())

        {

            break;

        }

    }

    if (i < 3)

    {

        if (0 == i)

        {

            int e_ret = execlp("ps", "ps", "aux", NULL);

            if (-1 == e_ret)

            {

                perror("execlp ps error ");

                exit(1);

            }

        }

        else if (1 == i)

        {

            int e_ret = execl(" / home / lovedan / projects / test / a", "a", NULL);

            if (-1 == e_ret)

            {

                perror("execlp a error ");

                exit(1);

            }

        }

        else if (2 == i)

        {

            int e_ret = execl(" / home / lovedan / projects / test / b", "b", NULL);

            if (-1 == e_ret)

            {

                perror("execlp b error ");

                exit(1);

            }

        }

    }

    else if (i == 3)

    {

        //while (-1 != (pid = waitpid(-1, &status, WNOHANG)))//这里设置为不阻塞状态

        while (-1 != (pid = wait(&status))) //上面那句代码并没有问题。不阻塞的话,那输出真的是群魔乱舞。

        {

            if (0 == pid)

            {

                printf("The child process is running and does not recycle.\n");

            }

            else if (pid > 0)

            {

                printf("The recovery sub - process is successful, and his ID is %d .\n", pid);

                if (WIFEXITED(status))

                {

                    printf("The subprocess exits normally, and the return value is %d .\n", WEXITSTATUS(status));

                }

                else if (WIFSIGNALED(status))

                {

                    printf("The subprocess exits with an exception because it exits with a signal of %d .\n", WTERMSIG(status));

                }

            }

        }

        if (-1 == pid)

        {

            printf("No child processes can be recycled.\n");

        }

    }

    else

    {

        printf("i value error.i = %d", i);

        exit(1);

    }

 

    // printf("hello from fork_3_ps_normal_SegmentationFault!\n");

    return 0;

}

程序中的a和b是另外的两个小程序,根据题目意思来写的,很简单,代码就不拿上来了。

来看看不阻塞和阻塞状态下的输出:首先是不阻塞的;

The child process is running and does not recycle.

The child process is running and does not recycle.

The child process is running and does not recycle.

The child process is running and does not recycle.

I am a.

The recovery sub-process is successful, and his ID is 449 .

The subprocess exits with an exception because it exits with a signal of 11 .

The recovery sub-process is successful, and his ID is 448 .

The subprocess exits normally, and the return value is 216 .

The child process is running and does not recycle.

The child process is running and does not recycle.

The child process is running and does not recycle.

The child process is running and does not recycle.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

The child process is running and does not recycle.

The child process is running and does not recycle.

root         1  0.0  0.0  10432   580 ?        Ss   08:29   0:00 /init

The child process is running and does not recycle.

The child process is running and does not recycle.

The child process is running and does not recycle.

The child process is running and does not recycle.

lovedan      2  0.0  0.0  25788  3704 tty1     Ss   08:29   0:00 -bash

The child process is running and does not recycle.

root        44  0.0  0.0  80408   912 ?        Ss   08:30   0:00 /usr/sbin/sshd

The child process is running and does not recycle.

lovedan    446  0.0  0.0  36172   516 tty1     S    09:06   0:00 ./fork_3_ps_normal_SegmentatThe child process is running and does not recycle.

lovedan    447  0.0  0.0  51704  1832 tty1     R    09:06   0:00 ps aux

The child process is running and does not recycle.

The recovery sub-process is successful, and his ID is 447 .

The subprocess exits normally, and the return value is 0 .

No child processes can be recycled.

完全是群魔乱舞。

来看看阻塞状态下的:

I am a.

The recovery sub-process is successful, and his ID is 727 .

The subprocess exits normally, and the return value is 216 .

The recovery sub-process is successful, and his ID is 728 .

The subprocess exits with an exception because it exits with a signal of 11 .

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.0  0.0  10432   580 ?        Ss   08:29   0:00 /init

lovedan      2  0.0  0.0  25788  3708 tty1     Ss   08:29   0:00 -bash

root        44  0.0  0.0  80408   912 ?        Ss   08:30   0:00 /usr/sbin/sshd

lovedan    725  0.0  0.0  36172   512 tty1     S    09:10   0:00 ./fork_3_ps_normal_SegmentationFault.out

lovedan    726  0.0  0.0  51704  1832 tty1     R    09:10   0:00 ps aux

The recovery sub-process is successful, and his ID is 726 .

The subprocess exits normally, and the return value is 0 .

No child processes can be recycled.

这就比较好了。满足。

posted on 2018-04-05 20:41  狂自私  阅读(679)  评论(0编辑  收藏  举报