用管道实现AB进程对话

A进程发送一句话后,B进程接,收到打印。然后B进程发送一句话,A进程接收后打印
重复上述步骤。直到AB接收或者发送完quit后,结束AB进程
 

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

int main(int argc, const char *argv[])
{
    if (mkfifo("./FIFO", 0664) < 0)
    {
        if (errno != 17)
        {
            perror("mkfifo");
            return -1;
        }
    }

    printf("mkfifo1 success\n");

    if (mkfifo("./FIFO2", 0664) < 0)
    {
        if (errno != 17)
        {
            perror("mkfifo");
            return -1;
        }
    }

    printf("mkfifo success\n");

    pid_t cpid = fork();
    if (cpid > 0)
    {
		int fd = open("./FIFO", O_RDWR);
	
        if (fd < 0)
        {
            perror("open");
            return -1;
        }
        printf("open success\n");

        char buf[128] = "";
        ssize_t res = 0;
        while (1)
        {
            printf("请A输入消息>>>");
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf) - 1] = '\0';

            if (write(fd, buf, strlen(buf) + 1) < 0)
            {
                perror("write");
                return -1;
            }
            printf("A发送成功\n");

            res = read(fd, buf, sizeof(buf));
            if (res < 0)
            {
                perror("read");
                return -1;
            }
            else if (res == 0)
            {
                printf("B写端关闭!");
                break;
            }
            printf("A收到消息: %s\n", buf);

            if (strcmp(buf, "quit") == 0)
                break;
        }
        close(fd);
    }
    else if (cpid == 0)
    {
        int fd2 = open("./FIFO2", O_RDWR);
        if (fd2 < 0)
        {
            perror("open");
            return -1;
        }
        printf("open success\n");

        char buf2[128] = "";
        ssize_t res2 = 0;
        while (1)
        {
            res2 = read(fd2, buf2, sizeof(buf2));
            if (res2 < 0)
            {
                perror("read");
                return -1;
            }
            else if (res2 == 0)
            {
                printf("A写端关闭!");
                break;
            }
            printf("B收到消息: %s\n", buf2);

            if (strcmp(buf2, "quit") == 0)
                break;

            printf("请B输入回复>>>");
            fgets(buf2, sizeof(buf2), stdin);
            buf2[strlen(buf2) - 1] = '\0';

            if (write(fd2, buf2, strlen(buf2) + 1) < 0)
            {
                perror("write");
                return -1;
            }
            printf("B发送成功\n");
        }
        close(fd2);
    }
    else
    {
        perror("fork");
        return -1;
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0_V_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值