作業系統第3次實驗報告:管道

  • 2020 年 4 月 18 日
  • 筆記
  • 姓名 :萬大明
  • 學號 :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\n");  //打開管道文件失敗           
         exit(-1);                                               
     }                                                           
     while(1){                                                   
         printf("Please write content : ");                      
         scanf("%s",buffer);     //用戶輸入                                    
         int fkwrite=write(fd,buffer,(strlen(buffer)+1));     //將用戶輸入的值寫入管道   
         printf("Successfully write : %s\n",buffer);             
     }                                                           
     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!\n");                                                        
         exit(-1);                                                                 
     }                                                                              
     while(1){                                                                      
         if(read(fd,buffer,500) > 0)                                                
             printf("Successfully read : %s\n",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?    實時通訊?