AB两个实现信息交互

管道A:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>





int main(int argc, const char *argv[])
{
    if(mkfifo("./myfifo",0777)<0)
    {
        if(errno != 17)
        {
            perror("mkfifo1");
            return -1;
        }
    }
    printf("管道1创建成功!\n");

      if(mkfifo("./myfifo2",0777) <0)
  {
      if(errno !=17 )
      {
          perror("mkfifo");
          return -1;
      }
  }
  printf("管道2创建成功!\n");



    int op = open("./myfifo",O_WRONLY);//打开管道1的写
    int op1 = open("./myfifo1",O_RDONLY);//打开管道2的读

    if(op<0 || op1<0)
    {
        perror("open");
        return -1;
    }
    printf("管道1写打开成功/2读打开成功!\n");

    char arr[128] = "";
    ssize_t res = 0;

    while(1)
    {

        //读端
        bzero(arr,sizeof(arr));
        res = read(op1,arr,sizeof(arr));
        //fgets(arr,sizeof(arr),stdin);
        if(res <0)
        {
            perror("read");
            return -1;
        }else if(res == 0)
        {
            printf("写端关闭!\n");
            break;
        }
        if(strcmp(arr,"quit") == 0)
        {
            break;                                                                 
        }
        printf("%s \n",arr);

        //写端
        printf("罗密欧说:");
        fgets(arr,sizeof(arr),stdin);
        arr[strlen(arr)-1] = '\0';
        if(write(op,arr,sizeof(arr))<0)
        {
            perror("write");
            return -1;
        }else
            printf(" ");

        if(strcmp(arr,"quit") == 0)
        {
            printf("罗密欧退出!\n");
            break;
        }


    }
    close(op);
    close(op1);
    return 0;
}
                                                                              

管道B:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
    if(mkfifo("./myfifo",0777) <0)
    {
        if(errno !=17 )
        {
            perror("mkfifo");
            return -1;
        }
    }
    printf("管道1创建成功!\n");


      if(mkfifo("./myfifo1",0777) <0)
  {
      if(errno !=17 )
      {
          perror("mkfifo");
          return -1;
      }
  }
  printf("管道2创建成功!\n");


        int op = open("./myfifo",O_RDONLY);//打开管道1的读
        int op1 = open("./myfifo1",O_WRONLY);//打开管道2的写
        printf("管道1读打开成功/2写打开成功\n");

        char arr[128] = "";
        ssize_t res = 0;
        //写端
        while(1)
        {
            printf("朱丽叶说:");
            fgets(arr,sizeof(arr),stdin);
            arr[strlen(arr)-1] = '\0';                            
            if(write(op1,arr,strlen(arr))<0)
            {
                perror("write");
                return -1;
            }
            printf(" ");

            if(strcmp(arr,"quit") == 0 )
            {
                printf("朱丽叶退出\n");
                break;
            }

            //读端
            bzero(arr,sizeof(arr));
            res = read(op,arr,sizeof(arr));
            if(res <0)
            {
                perror("read");
                return -1;
            }else if(res == 0)
            {
                printf("写端关闭!\n");
                break;
            }
            if(strcmp(arr,"quit") == 0)
            {
                break;
            }
            printf("%s \n",arr);

        //  close(op);
        //  printf("op以关闭\n");
    }

    if(close(op)<0 || close(op1)<0)
    {
        perror("read");
        perror("write");
        return -1;
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值