11/13作业

1.write

#include<my_head.h>
//写程 序
int main(int argc, const char *argv[])
{
    //创建有名管道1
    if(mkfifo("./myfifo", 0664) != 0)
    {
        perror("mkfifo error");
        return -1;
    }

    printf("管道创建成功\n");
    
    //创建有名管道2
    if(mkfifo("./myfifo2", 0664) != 0)
    {
        perror("mkfifo2 error");
        return -1;
    }

    printf("管道2创建成功\n");
    pid_t pid=-1;
    pid=fork();
    if(pid>0)
    {
    
    //父进程
    //打开管道文件
    int fd = -1;
    if((fd= open("./myfifo", O_WRONLY)) == -1)
    {
        perror("open error");
        return -1;
    }

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

        //将数据写入到管道中
        write(fd, buf, strlen(buf));

        if(strcmp(buf, "quit") == 0)
        {
            break;
        }
    }
#include<my_head.h>
//写程 序
int main(int argc, const char *argv[])
{
    //创建有名管道1
    if(mkfifo("./myfifo", 0664) != 0)
    {
        perror("mkfifo error");
        return -1;
    }

    printf("管道创建成功\n");
    
    //创建有名管道2
    if(mkfifo("./myfifo2", 0664) != 0)
    {
        perror("mkfifo2 error");
        return -1;
    }

    printf("管道2创建成功\n");
    pid_t pid=-1;
    pid=fork();
    if(pid>0)
    {
    
    //父进程
    //打开管道文件
    int fd = -1;
    if((fd= open("./myfifo", O_WRONLY)) == -1)
    {
        perror("open error");
        return -1;
    }

    //关闭文件描述符
    close(fd);
    }else if(pid==0)
    { 
    //子进程
    sleep(3);
    int fd2 = -1;
    if((fd2 = open("./myfifo2", O_RDONLY)) == -1)
    {
        perror("open2 error");
        return -1;
    }

    char buf2[128] ="";
    while(1)
    {
        bzero(buf2, sizeof(buf2));

        //从管道文件中读取数据
        read(fd2, buf2, sizeof(buf2));

        printf("读取的管道2数据为:%s\n", buf2);

        if(strcmp(buf2, "quit") == 0)
        {
            break;
        }
    }
    close(fd2);
    }else
    {
       perror("fork error");
       return -1 ;
     }
     wait(NULL);
    
    return 0;
}

2.read

#include<my_head.h>
//读程序
int main(int argc, const char *argv[])
{
    pid_t pid=-1;
    pid=fork();
    if(pid>0)
    {
    
    //父进程
    //打开管道文件
    int fd = -1;
    if((fd = open("./myfifo", O_WRONLY)) == -1)
    {
        perror("open error");
        return -1;
    }

    char buf[128] ="";
    while(1)
    {
     bzero(buf, sizeof(buf));

        //从管道文件中读取数据
        read(fd, buf, sizeof(buf));

        printf("读取的管道1数据为:%s\n", buf);

        if(strcmp(buf, "quit") == 0)
        {
            break;
        }
    }
    //关闭文件描述符
    close(fd);
   
    }else if(pid==0)
    { 
    //子进程
    int fd2 = -1;
    if((fd2 = open("./myfifo2", O_RDONLY)) == -1)
    {
        perror("open error");
        return -1;
    }

    char buf2[128] ="";
       while(1)
    {
        printf("请输入>>");
        fgets(buf2, sizeof(buf2), stdin);
        buf2[strlen(buf2)-1] = '\0';

        //将数据写入到管道中
        write(fd2, buf2, strlen(buf2));

        if(strcmp(buf2, "quit") == 0)
        {
            break;
        }
    }   
   }else
    {
       perror("fork error");
       return -1 ;
     }

     wait(NULL);
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值