2.7~2.9 进程退出、孤儿进程、僵尸进程、wait

简介: 2.7~2.9 进程退出、孤儿进程、僵尸进程、wait

01 进程退出



上面是标准C库的,下面是Linux的系统函数


/*
    #include <stdlib.h>
    void exit(int status);
    #include <unistd.h>
    void _exit(int status);
    status参数:是进程退出时的一个状态信息。父进程回收子进程资源的时候可以获取到。
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
    printf("hello\n");   //  \n可以自动刷新缓冲区,
    printf("world");     //word还在缓冲区里面
    exit(0);    //会输出world    exit可以刷新IO缓冲
    //_exit(0);  //不会输出world
    return 0;
}


02 孤儿进程


  • 父进程运行结束,但子进程还在运行;这样的子进程叫做孤儿进程(Orphan Process)


  • 每当出现一个孤儿进程的时候,内核就把孤儿进程的父进程设置为init,而init进程会循环地wait()它的已经退出的子进程。这样,当一个孤儿进程凄凉地结束了其生命周期的时候,init进程就会代表党和政府处理它的一切善后工作


  • 因此孤儿进程并不会有什么危害


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() {
    // 创建子进程
    pid_t pid = fork();
    // 判断是父进程还是子进程
    if(pid > 0) {   //当前是父进程
        printf("i am parent process, pid : %d, ppid : %d\n", getpid(), getppid());
    } 
    else if(pid == 0) {  // 当前是子进程
        sleep(1);   //睡一秒钟,父进程死了 再执行子进程
        printf("i am child process, pid : %d, ppid : %d\n", getpid(),getppid());  //此时的父进程ppid是init,即1
    }
    // for循环
    for(int i = 0; i < 3; i++) {
        printf("i : %d , pid : %d\n", i , getpid());
    }
    return 0;
}



03 僵尸进程


  • 每个进程结束之后,都会释放自己地址空间中的用户区数据,内核区的PCB没有办法自己释放掉,需要父进程去释放


  • 进程终止时,父进程尚未回收,子进程残留资源(PCB)存放在内核中,变成僵尸进程(Zombie)


  • 僵尸进程不能被kill -9杀死


  • 这样就会导致一个问题,如果父进程不调用wait()或waitpid()的话,那么保留的那段信息就不会释放,其进程号就会一直被占用,但是系统所能使用的进程号是有限的,如果大量的产生僵尸进程,将因为没有可用的进程号而导致系统不能产生新的进程


  • 所以僵尸进程有危害,应当避免


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
//僵尸进程:子进程死了,父进程没有回收资源
int main() {
    // 创建子进程
    pid_t pid = fork();
    // 判断是父进程还是子进程
    if(pid > 0) {
        while(1) {
            printf("i am parent process, pid : %d, ppid : %d\n", getpid(), getppid());
            sleep(1);
        }
    } 
    else if(pid == 0) {
        // 当前是子进程
        printf("i am child process, pid : %d, ppid : %d\n", getpid(),getppid());
    }
    // for循环
    for(int i = 0; i < 3; i++) {
        printf("i : %d , pid : %d\n", i , getpid());
    }
    return 0;
}



04 进程回收



wait


/*
    #include <sys/types.h>
    #include <sys/wait.h>
    pid_t wait(int *wstatus);
        功能:等待任意一个子进程结束,如果任意一个子进程结束了,次函数会回收子进程的资源。
        参数:int *wstatus
            进程退出时的状态信息,传入的是一个int类型的地址,传出参数。
        返回值:
            - 成功:返回被回收的子进程的id
            - 失败:-1 (所有的子进程都结束,调用函数失败)
    调用wait函数的进程会被挂起(阻塞),直到它的一个子进程退出或者收到一个不能被忽略的信号时才被唤醒(相当于继续往下执行)
    如果没有子进程了,函数立刻返回,返回-1;如果子进程都已经结束了,也会立即返回,返回-1.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
    // 有一个父进程,创建5个子进程(兄弟)
    pid_t pid;
    // 创建5个子进程
    for(int i = 0; i < 5; i++) {
        pid = fork();
        if(pid == 0) {   //不让产生孙子进程
            break;
        }
    }
    if(pid > 0) {
        // 父进程
        while(1) {
            printf("parent, pid = %d\n", getpid());
            // int ret = wait(NULL);
            int st;   //进程退出时的状态信息,传入的是一个int类型的地址,传出参数
            int ret = wait(&st);
            if(ret == -1) {
                break;
            }
            if(WIFEXITED(st)) {
                // 是不是正常退出
                printf("退出的状态码:%d\n", WEXITSTATUS(st));
            }
            if(WIFSIGNALED(st)) {
                // 是不是异常终止
                printf("被哪个信号干掉了:%d\n", WTERMSIG(st));
            }
            printf("child die, pid = %d\n", ret);
            sleep(1);
        }
    } 
    else if (pid == 0){  // 子进程
         while(1) {
            printf("child, pid = %d\n",getpid());    
            sleep(1);       
         }
        exit(0);
    }
    return 0; // exit(0)
}


waitpid


/*
    #include <sys/types.h>
    #include <sys/wait.h>
    pid_t waitpid(pid_t pid, int *wstatus, int options);
        功能:回收指定进程号的子进程,可以设置是否阻塞。
        参数:
            - pid:
                pid > 0 : 某个子进程的pid
                pid = 0 : 回收当前进程组的所有子进程    
                pid = -1 : 回收所有的子进程,相当于 wait()  (最常用)
                pid < -1 : 某个进程组的组id的绝对值,回收指定进程组中的子进程
            - options:设置阻塞或者非阻塞
                0 : 阻塞
                WNOHANG : 非阻塞
            - 返回值:
                > 0 : 返回子进程的id
                = 0 : options=WNOHANG, 表示还有子进程或者
                = -1 :错误,或者没有子进程了
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
    // 有一个父进程,创建5个子进程(兄弟)
    pid_t pid;
    // 创建5个子进程
    for(int i = 0; i < 5; i++) {
        pid = fork();
        if(pid == 0) {
            break;
        }
    }
    if(pid > 0) {
        // 父进程
        while(1) {
            printf("parent, pid = %d\n", getpid());
            sleep(1);
            int st;
            // int ret = waitpid(-1, &st, 0);
            int ret = waitpid(-1, &st, WNOHANG);
            if(ret == -1) {
                break;
            } else if(ret == 0) {
                // 说明还有子进程存在
                continue;
            } else if(ret > 0) {
                if(WIFEXITED(st)) {
                    // 是不是正常退出
                    printf("退出的状态码:%d\n", WEXITSTATUS(st));
                }
                if(WIFSIGNALED(st)) {
                    // 是不是异常终止
                    printf("被哪个信号干掉了:%d\n", WTERMSIG(st));
                }
                printf("child die, pid = %d\n", ret);
            }
        }
    } else if (pid == 0){
        // 子进程
         while(1) {
            printf("child, pid = %d\n",getpid());    
            sleep(1);       
         }
        exit(0);
    }
    return 0; 
}


目录
相关文章
|
Linux
【Linux】—— 进程等待 wait&&waitpid
【Linux】—— 进程等待 wait&&waitpid
553 0
【Linux】—— 进程等待 wait&&waitpid
|
Linux
进程等待(wait和wait函数)【Linux】
进程等待(wait和wait函数)【Linux】
628 0
|
Linux
网络编程之信号(处理僵尸进程的终极办法)之初识信号捕捉器
接着我们之前的管道所提出来的问题() 在创建子进程之后,子进程究竟何时终止????调用waitpid函数后还要无休止的等待子进程终止吗???”,这显然会是一个问题。因为父进程往往与子进程一样繁忙,因此我们不能只调用waitpid函数来等待子进程终止。那么我们应该怎么办呢??? 信号闪亮登场!!!!!
306 0
|
Linux C语言
Linux: 僵尸进程究竟是什么?有什么危害?
Linux: 僵尸进程究竟是什么?有什么危害?
623 0
|
Linux C++
Linux c/c++进程之僵尸进程和守护进程
这篇文章介绍了Linux系统中僵尸进程和守护进程的概念、产生原因、解决方法以及如何创建守护进程。
572 0
|
调度 Python
并发编程 , 孤儿进程 , 守护进程
并发编程 , 孤儿进程 , 守护进程
|
安全 Java
多线程(CAS, ABA问题, Runnable & Callable & 僵尸线程 & 孤儿进程)
多线程(CAS, ABA问题, Runnable & Callable & 僵尸线程 & 孤儿进程)
534 1
|
Linux Shell 调度
【linux进程(四)】僵尸进程和孤儿进程概念&进程优先级讲解
【linux进程(四)】僵尸进程和孤儿进程概念&进程优先级讲解
|
Linux 调度
【Linux】详解进程状态之僵尸进程——孤儿进程
【Linux】详解进程状态之僵尸进程——孤儿进程
646 0
【Linux】详解进程状态之僵尸进程——孤儿进程
【进程控制】超详细讲解wait和waitpid的原理(结合代码)
【进程控制】超详细讲解wait和waitpid的原理(结合代码)

热门文章

最新文章