pipe创建匿名管道`

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/andrewgithub/article/details/84933705

pipe创建的管道为匿名管道,匿名管道只能在有关系的进程之间使用,例如父进程和子进程,由同一个父进程创建的子进程或称为兄弟进程
在这里插入图片描述
如图所示若管道只有一个,并且管道只能进程单向通讯,因此,在子进程继承父进程的文件描述符之后,父进程关闭fd[0]文件描述符,子进程关闭fd[1文件描述符,这样父进程和子进程之间就能够使用管道进行单向的通讯了

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

//pipe  创建的匿名管道只能在有血缘关系的进程之间进行使用
// 例如  父进程和子进程    同一个父进程创建的两个子进程

//管道的创建需要使用两个文件描述符,文件描述符  fd[0] 用于读取 fd[1]用于pipe的写入

int main(int argc, char *argv[])
{




int pipe_fd[2];
if(pipe(pipe_fd) < 0)
{
    printf("pipe create error \n");
    return -1;
}
else
{
    printf("pipe create success \n");
}



close(pipe_fd[0]);
close(pipe_fd[1]);


    printf("hello world\n");

    return 0;
}








猜你喜欢

转载自blog.csdn.net/andrewgithub/article/details/84933705
今日推荐