退出程序会自动关闭管道吗?

use*_*376 2 c kill pipe process waitpid

假设我在子进程和父进程之间创建了一个管道并且子进程正常结束,子进程的管道会自动关闭吗?

另外,如果子进程也有子进程并且子进程以分段错误结束,它也会杀死我的孙子进程吗?我的意思是从进程表中删除它(我不需要等待它)。

编辑:例如,对于以下代码,我在子进程中生成分段错误并尝试在父进程中等待它。运行程序后,waitpid返回-1,但是当我检查WIFEXITED(status)时,子进程程序似乎正常退出。我得到了一个

杀死子进程失败:没有这样的进程

错误尝试杀死我的孙子进程。我想知道这是不是因为分段错误会自动关闭子进程和孙进程?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>


int main( void ) {

    pid_t childpid;
    int ends[ 2 ];
    pipe( ends );
    if ( ( childpid = fork() ) == -1 ) {
        perror( "fork failed" );
        exit( 0 );
    }
    if( childpid == 0 ) {
        pid_t cpid;
        if ( ( cpid = fork() ) == -1 ) {
            perror( "fork failed" );
            exit( 0 );
        }
        if ( cpid == 0 ){
            while(1);
        }   
        else{
            printf("cpid is : %d\n",cpid);
            char msg[32];
            sprintf( msg, "%d", cpid );
            printf("cpid con is : %s\n", msg);
            if( write( ends[ 1 ], msg, 32 ) == -1 ) {
                perror( "Write failed" );
                exit( 0 );
            }
            char *s = NULL;
            *s = 15;
            while(1);
        }
    }
    else{
        printf("childpid is : %d\n",childpid);
        char msg[ 32 ];
        int cpid;
        if( read( ends[0], msg,32 ) == -1 ) {
            perror("read failed");
            exit( 0 ); 
        }
        cpid = atoi( msg );
        int status;
        while(1) {
            if ( waitpid( childpid, &status, WNOHANG ) == -1 ) {
                //printf( "%d\n", WIFEXITED(status) );
                if ( kill( cpid, 9 ) == -1 ) {
                    perror( "Killing child process failed" );
                    exit( 0 );
                }
                /*if ( kill( cpid, 9 ) == -1 ) {
                    perror( "Killing child process failed" );
                    exit( 0 );
                }*/

            }
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

PSk*_*cik 7

操作系统将关闭与已死亡或退出的进程关联的所有文件描述符。如果这关闭了指向管道读取端的最后一个文件描述符,则写入写入端将开始生成 SIGPIPE(fds 是对它们后面的 vnode 实体的引用计数引用)。

如果父母去世,其孩子将重新成为父母initinit将等待它。(wait无论如何,祖父母不能对孙子孙女)。