IO day8作业

1.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 pthread_t tid1;
  6 pthread_t tid2;
  7 struct aaa
  8 {
  9     int fd1;
 10     int fd2;
 11 };
 12 char buf1[128] = "";
 13 char buf2[128] = "";
 14 void *zxc1(void *p)
 15 {
 16     int fd2 = *(int *)p;
 17     while(1)
 18     {
 19         bzero(buf1,sizeof(buf1));
 20         printf("请输入>>>\n");
 21         scanf("%s",buf1);
 22         if(write(fd2,buf1,sizeof(buf1)) < 0)
 23         {                                                                                                                                                                                                                                                                                                     
 24             ERR_MSG("write");
 25             return NULL;
 26         }
 27         if(strcmp(buf1,"quit") == 0)
 28         {
 29             exit(0);
 30         }
 31     }
 32 }
 33 
 34 void *zxc2(void* p)
 35 {
 36     int fd1 = *(int *)p;
 37     while(1)
 38     {
 39         bzero(buf2,sizeof(buf2));
 40         if(read(fd1,buf2,sizeof(buf2)) < 0)
 41         {
 42             ERR_MSG("read");
 43             return NULL;
 44         }
 45         if(strcmp(buf2,"quit") == 0)
 46         {
 47             exit(0);
 48         }
 49         printf("%s\n",buf2);
 50     }
 51 }
 52 int main(int argc, const char *argv[])
 53 {
 54     if(mkfifo("./fifo1",0664) < 0)
 55     {
 56         if(errno != 17)
 57         {
 58             ERR_MSG("fifo");
 59             return -1;
 60         }
 61     }
 62     if(mkfifo("./fifo2",0664) < 0)
 63     {
 64         if(errno != 17)
 65         {
 66             ERR_MSG("fifo");
 67             return -1;
 68         }
 69     }
 70     int fd1 = open("./fifo1",O_RDONLY);
 71     if(fd1 < 0)
 72     {
 73         ERR_MSG("open");
 74         return -1;
 75     }
 76     int fd2 = open("./fifo2",O_WRONLY);
 77     if(fd2 < 0)
 78     {
 79         ERR_MSG("open");
 80         return -1;
 81     }
 82   
 83     if(pthread_create(&tid1,NULL,zxc1,(void*)&fd2) != 0)
 84     {
 85      printf("子线程1创建失败\n");
 86      return -1;
 87     }
 88 
 89     if(pthread_create(&tid2,NULL,zxc2,(void*)&fd1) != 0)
 90     {
 91      printf("子线程2创建失败\n");
 92      return -1;
 93     }
 94 
 95     pthread_join(tid2,NULL);
 96     pthread_join(tid1,NULL);
 97 
 98     close(fd1);
 99     close(fd2);
100     return 0;
101 }
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                            
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 pthread_t tid1;
  6 pthread_t tid2;
  7 struct aaa
  8 {
  9     int fd1;
 10     int fd2;
 11 };
 12 char buf1[128] = "";
 13 char buf2[128] = "";
 14 void *zxc1(void *p)
 15 {
 16     int fd1 = *(int *)p;
 17     while(1)
 18     {
 19         bzero(buf1,sizeof(buf1));
 20         printf("请输入>>>\n");
 21         scanf("%s",buf1);
 22         if(write(fd1,buf1,sizeof(buf1)) < 0)
 23         {
 24             ERR_MSG("write");
 25             return NULL;
 26         }
 27         if(strcmp(buf1,"quit") == 0)
 28         {
 29             exit(0);
 30         }
 31     }
 32 }
 33 
 34 void *zxc2(void* p)
 35 {
 36     int fd2 = *(int *)p;
 37     while(1)
 38     {
 39         bzero(buf2,sizeof(buf2));
 40         if(read(fd2,buf2,sizeof(buf2)) < 0)
 41         {
 42             ERR_MSG("read");
 43             return NULL;
 44         }
 45         if(strcmp(buf2,"quit") == 0)
 46         {
 47             exit(0);                                                                                                                                                                                                                                                                                          
 48         }
 49         printf("%s\n",buf2);
 50     }
 51 }
 52 int main(int argc, const char *argv[])
 53 {
 54     if(mkfifo("./fifo1",0664) < 0)
 55     {
 56         if(errno != 17)
 57         {
 58             ERR_MSG("fifo");
 59             return -1;
 60         }
 61     }
 62     if(mkfifo("./fifo2",0664) < 0)
 63     {
 64         if(errno != 17)
 65         {
 66             ERR_MSG("fifo");
 67             return -1;
 68         }
 69     }
 70     int fd1 = open("./fifo1",O_WRONLY);
 71     if(fd1 < 0)
 72     {
 73         ERR_MSG("open");
 74         return -1;
 75     }
 76     int fd2 = open("./fifo2",O_RDONLY);
 77     if(fd2 < 0)
 78     {
 79         ERR_MSG("open");
 80         return -1;
 81     }
 82 
 83     if(pthread_create(&tid1,NULL,zxc1,(void*)&fd1) != 0)
 84     {
 85      printf("子线程1创建失败\n");
 86      return -1;
 87     }
 88 
 89     if(pthread_create(&tid2,NULL,zxc2,(void*)&fd2) != 0)
 90     {
 91      printf("子线程2创建失败\n");
 92      return -1;
 93     }
 94 
 95     pthread_join(tid2,NULL);
 96     pthread_join(tid1,NULL);
 97 
 98     close(fd1);
 99     close(fd2);
100     return 0;
101 }
~                                                                                          

2.xmind

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值