创建

int mkfifo(const char *pathname, mode_t mode);

:有名管道需要用open函数打开以后使用,如果以读方式打开,会阻塞到有写方打开管道,同样以写方式打开会阻塞到以读方式打开;在该程序中一定会先执行子进程,因为父进程读管道时,管道中没有数据,也会阻塞read到有write写入到管道中

举例

int wfd;
int rfd;
char buf[100] = {0};

mkfifo("./fifo", 0777);     //创建管道

if(fork() == 0)
{   
    wfd = open("./fifo", O_WRONLY);
    dup2(1, wfd); //标准输出到管道输入

    while(1)
    {   
        system("ls");
        sleep(1);
    }   
}   

rfd = open("./fifo", O_RDONLY);
while(1)
{   
    read(rfd, buf, 100);
    puts(buf);
}