zoukankan      html  css  js  c++  java
  • 操作系统第3次实验报告:管道

    • 姓名 :万大明
    • 学号 :201821121058
    • 班级 :计算1812

    1. 编写程序

    fifo_write.c

    #include <fcntl.h>                                               
    #include <unistd.h>                                              
    #include <stdio.h>                                               
    #include <stdlib.h>                                              
    #include <errno.h>                                               
    #include <signal.h>                                              
    #include <string.h>                                              
    #include <sys/types.h>                                           
    #include <sys/stat.h>                                           
    int main(){                                                     
         int fifo=mkfifo("fifo",0662);                              
         if((fifo == -1)){                                           
             if((errno!=EEXIST)){          //排除文件已存在这个报错                          
                 perror("mkfifo");         //依参数fifo建立特殊的FIFO文件                          
                 exit(-1);                                           
             }                                                       
         }                                                           
         char buffer[500];                                           
         int fd = open("fifo",O_WRONLY); //打开管道文件fifo          
         if(fd == -1){                                               
             printf("Open failure
    ");  //打开管道文件失败           
             exit(-1);                                               
         }                                                           
         while(1){                                                   
             printf("Please write content : ");                      
             scanf("%s",buffer);     //用户输入                                    
             int fkwrite=write(fd,buffer,(strlen(buffer)+1));     //将用户输入的值写入管道   
             printf("Successful!");             
         }                                                           
         close(fd);                                                  
         return 0;                                                   
     }  
    

     fifo_read.c

    #include <fcntl.h>                                                                  
    #include <unistd.h>                                                                
    #include <stdio.h>                                                                  
    #include <stdlib.h>                                                                 
    #include <errno.h>                                                                  
    #include <signal.h>                                                                 
    #include <string.h>                                                                 
    #include <sys/types.h>                                                              
    #include <sys/stat.h>                                                              
    int main(){                                                                        
         int fifo=mkfifo("fifo",0662);                                                  
         if((fifo == -1)){                                                              
             if((errno!=EEXIST)){               //排除文件已存在这个报错                                        
                 perror("mkfifo");                                                      
                 exit(-1);                                                              
             }                                                                          
         }                                                                              
         char buffer[500];                                                              
         int fd=open("fifo",O_RDONLY);          //以读的方式打开管道                                        
         if(fd == -1){                                                                  
             printf("Error!
    ");                                                        
             exit(-1);                                                                 
         }                                                                              
         while(1){                                                                      
             if(read(fd,buffer,500) > 0)                                                
                 printf("Successfully read : %s
    ",buffer);    //读出写入的内容                         
         }                                                                              
         return 0;                                                                                                                                                         
     }  
    

    2. 分析运行结果

    执行"./fifo_write"的结果

     执行"./fifo_read"的结果:

    分析:

    • 工作特点为:单向(write->read),先进先出,同步进行
    • O_RDONLY:读管道
             O_WRONLY:写管道
    • 在进行以上操作时,应打开两个端口,先将读的一端打开,再打开

             写的一端,否则读端将会堵塞。

    • 由写入端写入内容,写入成功会提示"successful",由读出端读出。

    3. 通过该实验产生新的疑问及解答

    • 疑惑:O_RDONLY, O_WRONLY, O_RDWR等有什么作用,什么情况下堵塞或不堵塞。

              解答:调用open打开有名管道的进程可能会被阻塞。但如果同时用读写方式(O_RDWR)打

                      开,则一定不会导致阻塞;如果以只读方式(O_RDONLY)打开,则调用open函数的

                      进程将会被阻塞直到有写方式打开管道;同样以写方式(O_WRONLY)打开也会阻塞

                      直到有读方式打开管道。

    • 疑惑:当读出端不打开时,写入端输入后一直无反应。

                                             

              解答:管道缓冲区一有空闲区域,写进程就会试图向管道写入数据。如果读进程不读走管

                        道缓冲区中的数据,那么写操作将一直被阻塞等待。

    4. 加分项

    利用管道编程解决一些实际的问题。

    • QQ?    实时通信? 
  • 相关阅读:
    Mybatis和Hibernate
    SpringMVC运行原理
    HTML中class属性里有多个属性值
    json对象(增加数据)
    Ajax请求
    <url-pattern>里的/和/*
    mysql忘记密码
    Eclipse无法正常连接mysql8.0.20
    a+1和&a+1
    可重入函数与不可重入函数
  • 原文地址:https://www.cnblogs.com/jiuyouir/p/12723806.html
Copyright © 2011-2022 走看看