6月15日作业

 #include <stdio.h>                                                                 
 #include <errno.h>                                                                 
 #include <unistd.h>                                                                
 #include <string.h>                                                                
 #include <sys/types.h>                                                             
 #include <sys/stat.h>                                                              
 #include <fcntl.h>                                                                 
                                                                                    
                                                                                    
 int main(int argc, const char *argv[])                                             
 {                                                                                  
     char str[120] = "";                                                            
     char buf[120] ="";                                                             
                                                                                    
     //打开管道                                                                     
     int fp = open("./myfifo", O_WRONLY);                                           
     if(fp < 0)                                                                     
     {                                                                              
         perror("mkfifo");                                                          
         return -1;                                                                 
     }                                                                              
     printf("open1打开成功  fp = %d\n",fp);                                         
                                                                                    
     int dp = open("./yourfifo", O_RDONLY);                                         
     if(dp < 0)                                                                     
     {                                                                              
         perror("open2");                                                           
         return -1;                                                                 
     }                                                                              
     printf("open2 打开成功\n");                                                    
                                                                                    
                                                                                    
     while(1)                                                                       
     {                                                                              
         printf("一写>>>");                                                         
         scanf("%s",str);                                                           
         int wr = write(fp, str, strlen(str));                                      
         if(wr < 0)                                                                 
         {                                                                          
             perror("write");                                                       
             return -1;                                                             
         }                                                                          
         if(strcmp(str,"quit") == 0)                                                
         {                                                                          
             printf("open1 写道关闭");//*                                           
             break;                                                                 
         }                                                                          
                                                                                    
                                                                                    
                                                                                    
         int rd = read(dp, str, sizeof(str));                                       
         if(rd < 0)                                                                 
         {                                                                          
             perror("read");                                                        
             return -1;                                                             
         }                                                                          
         else if(rd == 0)                                                           
         {                                                                          
             printf("管道写道关闭\n");                                              
             break;                                                                 
         }                                                                          
         printf("一号读>>>%s\n",str);                                               
         if(strcmp(str,"quit") == 0)                                                
             break;                                                                 
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
         /*                                                                         
            close(fp);                                                              
            printf("fp关闭\n");                                                     
            */                                                                      
     }                                                                              
                                                                                    
                                                                                    
     close(fp);                                                                     
     close(dp);                                                                     
     printf("fo 以关闭\n");                                                         
                                                                                    
     return 0;                                                                      
 }                                                                                  
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
 #include <stdio.h>                                                                                     
 #include <errno.h>                                                                                     
 #include <unistd.h>                                                                                    
 #include <string.h>                                                                                    
 #include <sys/types.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("myfifo 创建完成 \n");                                                                      
                                                                                                        
     if(mkfifo("./yourfifo", 0777) < 0)                                                                 
     {                                                                                                  
         if(errno != 17)                                                                                
         {                                                                                              
             perror("yourfifo");                                                                        
             return -1;                                                                                 
         }                                                                                              
     }                                                                                                  
     printf("yourfifo is success\n");                                                                   
                                                                                                        
                                                                                                        
     char str[120] = "";                                                                                
     char buf[120] = "";                                                                                
                                                                                                        
     //打开管道                                                                                         
     int fo = open("./myfifo", O_RDONLY);                                                               
     if(fo < 0)                                                                                         
     {                                                                                                  
         perror("open1");                                                                               
         return -1;                                                                                     
     }                                                                                                  
     printf("open1管道打开成功 fo = %d\n",fo);                                                          
                                                                                                        
     int dp = open("./yourfifo", O_WRONLY);                                                             
     if(dp < 0)                                                                                         
     {                                                                                                  
         perror("open2");                                                                               
         return -1;                                                                                     
     }                                                                                                  
     printf("open2管道打开成功 dp = %d\n",dp);                                                          
                                                                                                        
                                                                                                        
     //循环传输信息                                                                                     
                                                                                                        
     while(1)                                                                                           
     {                                                                                                  
         //读                                                                                           
         int rd = read(fo, str, sizeof(str));                                                           
         if(rd < 0)                                                                                     
         {                                                                                              
             perror("read");                                                                            
             return -1;                                                                                 
         }                                                                                              
         else if(rd == 0)                                                                               
         {                                                                                              
             printf("管道写道关闭\n");                                                                  
             break;                                                                                     
         }                                                                                              
                                                                                                        
         printf("二号读>>>%s\n",str);                                                                   
         if(strcmp(str,"quit") == 0)                                                                    
             break;                                                                                     
                                                                                                        
         //写                                                                                           
         printf("二号写>>>");                                                                           
         scanf("%s",buf);                                                                               
         int wr = write(dp, buf, strlen(buf));                                                          
         if(wr < 0)                                                                                     
         {                                                                                              
             perror("open2");                                                                           
             return -1;                                                                                 
         }                                                                                              
         if(strcmp(buf,"quit") == 0)                                                                    
             break;                                                                                     
     }                                                                                                  
                                                                                                        
     close(fo);                                                                                         
     close(dp);                                                                                         
     printf("fo 关闭\n");                                                                               
     return 0;                                                                                          
 }                                                                                                      
                                                                                                        
                                                                                                        
                                                                                                        
                                                                                                        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值