4.18 作业

实现AB进程对话。

A进程发送一句话后,B进程接收到打印。然后B进程发送一句话,A进程接收后打印

重复上述步骤。直到AB接收或者发送完quit后,结束AB进程

提示用两根管道

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
    if(mkfifo("./myfifo",0775)<0)
    {
        if(errno != 17)
        {
            perror("mkfifo aaaaa");
            return -1;
        }
    }
    printf("mkfifo success __%d__\n",__LINE__);


    if(mkfifo("./myfifo123",0775)<0)
    {
        if(errno != 17)
        {
            perror("mkfifo aaaaa");
            return -1;
        }
    }
    printf("mkfifo success __%d__\n",__LINE__);


    int fd = open("./myfifo",O_RDONLY);
    if(fd<0)
    {
        perror("open bbbbb");
        return -1;
    }
    printf("open readonly success fd=%d __%d__\n",fd,__LINE__);


    int fw = open("./myfifo123",O_WRONLY);
    if(fd<0)
    {
        perror("open bbbbb");
        return -1;
    }
    printf("open readonly success fd=%d __%d__\n",fd,__LINE__);

    char buf[128]="";
    ssize_t res = 0;
    while(1)
    {
        bzero(buf,sizeof(buf));
        res = read(fd,buf,sizeof(buf));
        if(res<0)
        {
            perror("read");
            return -1;
        }
        else if(0==res)                                                                       
        {
            printf("写端关闭,且管道中没有数据\n");
            break;
        }
        printf("对端说:%s\n",buf);

//      fgets(buf,sizeof(buf),stdin);
        scanf("%s",buf);
        getchar();
        buf[strlen(buf)]='\0';
        if(write(fw,buf,sizeof(buf))<0)
            {
                perror("write");
                return -1;
            }
            printf("写入成功\n");
            if(strcmp(buf,"quit")==0)
            {
                break;
            }
        }
    close(fd);
    close(fw);
    return 0;
}

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

int main(int argc, const char *argv[])
{
    if(mkfifo("./myfifo",0775)<0)
    {
        if(errno != 17)
        {
            perror("mkfifo aaaaa");
            return -1;
        }
    }
    printf("mkfifo success __%d__\n",__LINE__);


    if(mkfifo("./myfifo123",0775)<0)
    {
        if(errno != 17)
        {
            perror("mkfifo aaaaa");
            return -1;
        }
    }
    printf("mkfifo success __%d__\n",__LINE__);



    int fd = open("./myfifo",O_WRONLY);
    if(fd<0)
    {
        perror("open bbbbb");
        return -1;
    }
    printf("open readonly success fd=%d __%d__\n",fd,__LINE__);

    int fw = open("./myfifo123",O_RDONLY);
    if(fd<0)
    {
        perror("open bbbbb");
        return -1;
    }
    printf("open readonly success fd=%d __%d__\n",fd,__LINE__);


    char buf[128]="";
    ssize_t res = 0;
    while(1)
    {

        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        if(write(fd,buf,sizeof(buf))<0)
        {
            perror("write");
            return -1;
        }
        printf("写入成功\n");

        bzero(buf,sizeof(buf));                                                                                         
        res = read(fw,buf,sizeof(buf));
        if(res<0)
        {
            perror("read");
            return -1;
        }
        else if(0==res)
        {
            printf("写端关闭,没有数据\n");
            break;
        }
        printf("123说:%s\n",buf);
        if(strcmp(buf,"quit")==0)
        {
            break;
        }




        }
    close(fd);
    close(fw);
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值