Linux学习(16):有名管道实现简单聊天

文章讨论了在两个进程中通过fifo管道进行读写操作时遇到的问题,即创建管道后不会立即输出等待状态,需要另一个进程连接到管道;写完一行后会进入读模式,导致不能连续写入。为了解决这些问题,提出了使用父子进程的方法,父进程负责写入,子进程负责读取,从而实现了自由的通信过程。
摘要由CSDN通过智能技术生成
  1. 实现思路

chatA对fifo1进行只写操作,对fifo2进行只读操作

顺序:写fifo1 ---->阻塞等待读取fifo2的数据

chatB对fifo1进行只读操作,对fifo2进行只写操作

顺序:阻塞等待读fifo1的数据---->写fifo2

  1. 代码

//头文件
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
//chatA
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

int main() {
    // 1.判断有名管道文件是否存在
    int ret = access("fifo1", F_OK);
    if(ret == -1) {
        // 文件不存在
        printf("管道不存在,创建对应的有名管道\n");
        ret = mkfifo("fifo1", 0664);
        if(ret == -1) {
            perror("mkfifo");
            exit(0);
        }
    }

    ret = access("fifo2", F_OK);
    if(ret == -1) {
        // 文件不存在
        printf("管道不存在,创建对应的有名管道\n");
        ret = mkfifo("fifo2", 0664);
        if(ret == -1) {
            perror("mkfifo");
            exit(0);
        }
    }

    // 2.以只写的方式打开管道fifo1
    int fdw = open("fifo1", O_WRONLY);
    if(fdw == -1) {
        perror("open");
        exit(0);
    }
    printf("打开管道fifo1成功,等待写入...\n");
    // 3.以只读的方式打开管道fifo2
    int fdr = open("fifo2", O_RDONLY);
    if(fdr == -1) {
        perror("open");
        exit(0);
    }
    printf("打开管道fifo2成功,等待读取...\n");

    char buf[128];

    // 4.循环的写读数据
    while(1) {
        memset(buf, 0, 128);
        // 获取标准输入的数据
        fgets(buf, 128, stdin);
        // 写数据
        ret = write(fdw, buf, strlen(buf));
        if(ret == -1) {
            perror("write");
            exit(0);
        }

        // 5.读管道数据
        memset(buf, 0, 128);
        ret = read(fdr, buf, 128);
        if(ret <= 0) {
            perror("read");
            break;
        }
        printf("buf: %s\n", buf);
    }

    // 6.关闭文件描述符
    close(fdr);
    close(fdw);

    return 0;
}
//chatB
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

int main() {

    // 1.判断有名管道文件是否存在
    int ret = access("fifo1", F_OK);
    if(ret == -1) {
        // 文件不存在
        printf("管道不存在,创建对应的有名管道\n");
        ret = mkfifo("fifo1", 0664);
        if(ret == -1) {
            perror("mkfifo");
            exit(0);
        }
    }

    ret = access("fifo2", F_OK);
    if(ret == -1) {
        // 文件不存在
        printf("管道不存在,创建对应的有名管道\n");
        ret = mkfifo("fifo2", 0664);
        if(ret == -1) {
            perror("mkfifo");
            exit(0);
        }
    }

    // 2.以只读的方式打开管道fifo1
    int fdr = open("fifo1", O_RDONLY);
    if(fdr == -1) {
        perror("open");
        exit(0);
    }
    printf("打开管道fifo1成功,等待读取...\n");
    // 3.以只写的方式打开管道fifo2
    int fdw = open("fifo2", O_WRONLY);
    if(fdw == -1) {
        perror("open");
        exit(0);
    }
    printf("打开管道fifo2成功,等待写入...\n");

    char buf[128];

    // 4.循环的读写数据
    while(1) {
        // 5.读管道数据
        memset(buf, 0, 128);
        ret = read(fdr, buf, 128);
        if(ret <= 0) {
            perror("read");
            break;
        }
        printf("buf: %s\n", buf);

        memset(buf, 0, 128);
        // 获取标准输入的数据
        fgets(buf, 128, stdin);
        // 写数据
        ret = write(fdw, buf, strlen(buf));
        if(ret == -1) {
            perror("write");
            exit(0);
        }
    }

    // 6.关闭文件描述符
    close(fdr);
    close(fdw);

    return 0;
}
  1. fgets函数

char *fgets(chat *str,int n,FILE* stream)
    参数:str指向一个字符数组的指针,用于存储读入的字符串
        n为要读取的最大字符数(包括空字符),通常比目标缓冲区大1以存放'\0'
        stream为要从中读取数据的流(文件名或stdin)
  1. 问题

(1)创建好管道后,并不会第一时间输出等待读取或者等待写入,需要等另一端文件连接到管道的另一端,这是由于只写的一端需要等待只读的一端就位才会开始写,没有读取端时会阻塞

(2)我们写完一句回车后,就会进入到读模式,就不能再写了,写的内容会保存在stdin中,在下一轮直接被读到(下一轮就无法输入了,读取到stdin中保存的内容写完后又切换到了读模式,写的内容又会保存在stdin中)

(3)换言之,一次只能写一句,多写了就一直错误下去

  1. 解决方案

采用父子进程,父负责写,子负责收

//chatA
int main(){
    //判断管道是否存在并创建它
    int ret = access("fifo1",F_OK);
    if(ret==-1){
        printf("管道不存在,创建管道\n");
        ret = mkfifo("fifo1",0664);
        if(ret==-1){
            perror("mkfifo");
            exit(0);
        }
    }
    ret = access("fifo2",F_OK);
    if(ret==-1){
        printf("管道不存在,创建管道\n");
        ret = mkfifo("fifo2",0664);
        if(ret==-1){
            perror("mkfifo");
            exit(0);
        }
    }
    //创建管道后再创建子进程,让子进程负责读,父进程负责写
    pid_t pid = fork();
    if(pid>0){
        //父进程
        //以只写的方式打开fifo1
        int fdw = open("fifo1",O_WRONLY);
        if(fdw==-1){
            perror("open");
            exit(0);
        }
        printf("打开管道fifo1成功,等待写入...\n");
        char buf[128];
        while (1){
            memset(buf,0,128);
            fgets(buf,128,stdin);
            //写数据
            ret = write(fdw,buf,strlen(buf));
            if(ret==-1){
                perror("write");
            }
        }
        close(fdw);


    }else if(pid==0){
        //子进程
            //以只读的方式打开fifo2
        int fdr= open("fifo2",O_RDONLY);
        if(fdr==-1){
            perror("open");
            exit(0);
        }
        printf("打开管道fifo2成功,等待读取...\n");
        char buf[128];
        while(1){
            memset(buf,0,128);
            ret = read(fdr,buf,sizeof(buf));
            if(ret<=0){
                perror("对方断开连接");
                break;
            }
            printf("buf:%s\n",buf);
        }
        close(fdr);
    }

    return 0;
}
//chatB
int main(){
    //判断管道是否存在并创建它
    int ret = access("fifo1",F_OK);
    if(ret==-1){
        printf("管道不存在,创建管道\n");
        ret = mkfifo("fifo1",0664);
        if(ret==-1){
            perror("mkfifo");
            exit(0);
        }
    }
    ret = access("fifo2",F_OK);
    if(ret==-1){
        printf("管道不存在,创建管道\n");
        ret = mkfifo("fifo2",0664);
        if(ret==-1){
            perror("mkfifo");
            exit(0);
        }
    }
    //创建管道后再创建子进程,让子进程负责读,父进程负责写
    pid_t pid = fork();
    if(pid>0){
        //父进程
        //以只写的方式打开fifo2
        int fdw = open("fifo2",O_WRONLY);
        if(fdw==-1){
            perror("open");
            exit(0);
        }
        printf("打开管道fifo2成功,等待写入...\n");
        char buf[128];
        while (1){
            memset(buf,0,128);
            fgets(buf,128,stdin);
            //写数据
            ret = write(fdw,buf,strlen(buf));
            if(ret==-1){
                perror("write");
            }
        }
        close(fdw);


    }else if(pid==0){
        //子进程
            //以只读的方式打开fifo1
        int fdr= open("fifo1",O_RDONLY);
        if(fdr==-1){
            perror("open");
            exit(0);
        }
        printf("打开管道fifo1成功,等待读取...\n");
        char buf[128];
        while(1){
             memset(buf,0,128);
            ret = read(fdr,buf,sizeof(buf));
            if(ret<=0){
                perror("对方断开连接");
                break;
            }
            printf("buf:%s\n",buf);
        }
        close(fdr);
    }

    return 0;
}

这样可以实现自由地通信

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值