The well-known pipe communication of the communication between linux processes

#include <stdio.h>
#include"common.h"
int main()
{
    
    
/*
 * 有名管道:
 * 1.不区分读写端;
 * 2.打开操作时不允许以单边打开(只有一边,读或写),会阻塞打不开;
 * 3.管道不会缓存数据,得先先运行读的程序,在运行写的程序
 * 4.有写入保护
 * 5.用于两个陌生的进程之间的通信
*/

/*
        写入信息的程序
*/
        //1.创建有名管道
        mkfifo("/home/gentle/fifo", 0666);
        //2.打开管道
        int fd =open("/home/gentle/fifo",O_RDWR );
        if(fd==-1)
            {
    
    
            perror("打开失败");
            exit(0);
        }
        //3.信息写入管道
        write(fd,"hello",10);
        close(fd);

        return 0;
}

``

```c
#include <stdio.h>
#include"common.h"

/*
读取信息的程序
*/
int main()
{
    
    
    //1.如果管道不存在,就创建管道
    mkfifo("/home/gentle/fifo", 0666);
    //2.打开管道,像打开文件一样
    int fd =open("/home/gentle/fifo",O_RDWR );
    if(fd==-1)
        {
    
    
        perror("打开失败");
        exit(0);
    }

    //3.读取管道中的消息
    char buf[10];
    bzero(buf,sizeof(buf));
    read(fd,buf,sizeof(buf));
    printf("收到的信息为:%s",buf);
    close(fd);


    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_50188452/article/details/110287158