国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
管道通信

什么是管道?

 

      管道是單向的、先進先出的,它把一個進程的輸出和另一個進程的輸入連接在一起。一個進程(寫進程)在管道的尾部寫入數(shù)據(jù),另一個進程(讀進程)從管道的頭部讀出數(shù)據(jù)。

管道的分類

 

  管道包括無名管道名管道兩種,前者用于父進程和子進程間的通信,后者可用于運行于同一系統(tǒng)中的任意兩個進程間的通信。

無名管道的創(chuàng)建

  無名管道由pipe( )函數(shù)創(chuàng)建:

   int pipe(int filedis[2]);

   當一個管道被創(chuàng)建時,它會創(chuàng)建兩個文件描述符:filedis[0]用于讀管道,filedis[1]用于寫管道。

        管道通信示意圖如圖1所示:

                                                               

                                                                                               圖1   管道通信示意圖

 

管道關(guān)閉

 

    關(guān)閉管道只是將兩個文件描述符關(guān)閉即可,可以使用普通的close函數(shù)逐個關(guān)閉。

無名管道讀寫

 

    管道用于不同進程間通信。通常先創(chuàng)建一個管道,再通過fork函數(shù)創(chuàng)建一個子進程,該子進程會繼承父進程創(chuàng)建的管道。注意事項:必須在系統(tǒng)調(diào)用fork()前調(diào)用pipe(),否則子進程將不會繼承文件描述符。否則,會創(chuàng)建兩個管道,因為父子進程共享同一段代碼段,都會各自調(diào)用pipe(),即建立兩個管道,出現(xiàn)異常錯誤。無名管道讀寫過程如圖2所示:

                                                                             

                                                                                                 圖2 無名管道讀寫示意圖

無名管道實例:

 

  1. /********************************************************** 
  2. *實驗要求:   使用pipe創(chuàng)建無名管道并實現(xiàn)父子進程之間的通訊。 
  3. *功能描述:   在父進程中通過無名管道的寫端寫入數(shù)據(jù),通過子進程從管道讀端讀出相*           應(yīng)的數(shù)據(jù)。 
  4. *日    期:   2010-9-17 
  5. *作    者:   國嵌 
  6. **********************************************************/  
  7. #include <unistd.h>  
  8. #include <sys/types.h>  
  9. #include <errno.h>  
  10. #include <stdio.h>  
  11. #include <string.h>  
  12. #include <stdlib.h>  
  13.   
  14. /* 
  15.  * 程序入口 
  16.  * */  
  17. int main()  
  18. {  
  19.     int pipe_fd[2];  
  20.     pid_t pid;  
  21.     char buf_r[100];  
  22.     char* p_wbuf;  
  23.     int r_num;  
  24.       
  25.     memset(buf_r,0,sizeof(buf_r));  
  26.       
  27.     /*創(chuàng)建管道*/  
  28.     if(pipe(pipe_fd)<0)  
  29.     {  
  30.         printf("pipe create error\n");  
  31.         return -1;  
  32.     }  
  33.       
  34.     /*創(chuàng)建子進程*/  
  35.     if((pid=fork())==0)  //子進程執(zhí)行序列  
  36.     {  
  37.         printf("\n");  
  38.         close(pipe_fd[1]);//子進程先關(guān)閉了管道的寫端  
  39.         sleep(2); /*讓父進程先運行,這樣父進程先寫子進程才有內(nèi)容讀*/  
  40.         if((r_num=read(pipe_fd[0],buf_r,100))>0)  
  41.         {  
  42.             printf("%d numbers read from the pipe is %s\n",r_num,buf_r);  
  43.         }     
  44.         close(pipe_fd[0]);  
  45.         exit(0);  
  46.     }  
  47.     else if(pid>0) //父進程執(zhí)行序列  
  48.     {  
  49.         close(pipe_fd[0]); //父進程先關(guān)閉了管道的讀端  
  50.         if(write(pipe_fd[1],"Hello",5)!=-1)  
  51.             printf("parent write1 Hello!\n");  
  52.         if(write(pipe_fd[1]," Pipe",5)!=-1)  
  53.             printf("parent write2 Pipe!\n");  
  54.         close(pipe_fd[1]);  
  55.         waitpid(pid,NULL,0); /*等待子進程結(jié)束*/  
  56.         exit(0);  
  57.     }  
  58.     return 0;  
  59. }  


命名管道

 

命名管道和無名管道基本相同,但也有不同點:無名管道只能有父進程使用;但是通過命名管道,不相關(guān)的進程也能交換數(shù)據(jù)。

命名管道的創(chuàng)建

#include <sys/types.h>

#include <sys/stat.h>

int mkfifo(const char *pathname,mode_t mode)

pathname:FIFO文件名

mode:屬性(與文件操作相同)
一旦創(chuàng)建了一個FIFO,就可以用open打開它,一般的文件訪問函數(shù)(close,read,write)都可以用于FIFO

當打開FIFO時,非阻塞標志(O_NONBLOCK)

將對以后的讀寫產(chǎn)生如下影響:

1 沒有使用 O_NONBLOCK:訪問要求無法滿足時進程阻塞。如讀取空的FIFO時,或者FIFO已滿時。

2 使用O_NONBLOCK:訪問要求無法滿足時不阻塞,立即出錯返回,error是ENXIO。

FIFO讀進程:

  1. /********************************************************** 
  2. *實驗要求:   使用mkfifo創(chuàng)建有名管道并實現(xiàn)兩個進程之間的通訊。 
  3. *功能描述:   創(chuàng)建一個進程,并從已經(jīng)建立好的有名管道中,讀出事先寫入的 
  4. *           數(shù)據(jù)。 
  5. *日    期:   2010-9-17 
  6. *作    者:   國嵌 
  7. **********************************************************/  
  8. #include <sys/types.h>  
  9. #include <sys/stat.h>  
  10. #include <errno.h>  
  11. #include <fcntl.h>  
  12. #include <stdio.h>  
  13. #include <stdlib.h>  
  14. #include <string.h>  
  15. #define FIFO "/tmp/myfifo"  
  16.   
  17. /* 
  18.  * 程序入口 
  19.  * */  
  20. int main(int argc,char** argv)  
  21. {  
  22.     char buf_r[100];  
  23.     int  fd;  
  24.     int  nread;  
  25.   
  26.     printf("Preparing for reading bytes...\n");  
  27.     memset(buf_r,0,sizeof(buf_r));  
  28.       
  29.     /* 打開管道 */  
  30.     fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);  
  31.     if(fd==-1)  
  32.     {  
  33.         perror("open");  
  34.         exit(1);      
  35.     }  
  36.     while(1)  
  37.     {  
  38.         memset(buf_r,0,sizeof(buf_r));  
  39.           
  40.         if((nread=read(fd,buf_r,100))==-1)  
  41.         {  
  42.             if(errno==EAGAIN)  
  43.                 printf("no data yet\n");  
  44.         }  
  45.         printf("read %s from FIFO\n",buf_r);  
  46.         sleep(1);  
  47.     }  
  48.     /*后面三句話是不會被運行到的,但不會影響程序運行的效果當程序在上面的死循環(huán)中執(zhí)行時收到信號后會馬上結(jié)束運行而沒有執(zhí)行后面的三句話。這些會在后面的信號處理中講到,現(xiàn)在不理解沒有關(guān)系,這個問題留給大家學(xué)習了信號處理之后來解決。*/  
  49.     close(fd); //關(guān)閉管道  
  50.     pause(); /*暫停,等待信號*/  
  51.     unlink(FIFO); //刪除文件  
  52. }  


FIFO寫進程:

  1. /********************************************************** 
  2. *實驗要求:   使用mkfifo創(chuàng)建有名管道并實現(xiàn)兩個進程之間的通訊。 
  3. *功能描述:   創(chuàng)建一個進程,并在其中創(chuàng)建一個有名管道,并向其寫入數(shù)據(jù)。 
  4. *日    期:   2010-9-17 
  5. *作    者:   國嵌 
  6. **********************************************************/  
  7. #include <sys/types.h>  
  8. #include <sys/stat.h>  
  9. #include <errno.h>  
  10. #include <fcntl.h>  
  11. #include <stdio.h>  
  12. #include <stdlib.h>  
  13. #include <string.h>  
  14. #define FIFO_SERVER "/tmp/myfifo"  
  15.   
  16. /* 
  17.  * 程序入口 
  18.  * */  
  19. int main(int argc,char** argv)  
  20. {  
  21.     int fd;  
  22.     char w_buf[100];  
  23.     int nwrite;  
  24.       
  25.     /*創(chuàng)建有名管道*/  
  26.     if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0)&&(errno!=EEXIST))  
  27.     {  
  28.         printf("cannot create fifoserver\n");  
  29.     }  
  30.   
  31.     /*打開管道*/  
  32.     fd=open(FIFO_SERVER,O_WRONLY |O_NONBLOCK,0);  
  33.     if(fd==-1)  
  34.     {  
  35.         perror("open");  
  36.         exit(1);  
  37.     }  
  38.       
  39.     /*入?yún)z測*/  
  40.     if(argc==1)  
  41.     {  
  42.         printf("Please send something\n");  
  43.         exit(-1);  
  44.     }  
  45.     strcpy(w_buf,argv[1]);  
  46.       
  47.     /* 向管道寫入數(shù)據(jù) */  
  48.     if((nwrite=write(fd,w_buf,100))==-1)  
  49.     {  
  50.         if(errno==EAGAIN)  
  51.             printf("The FIFO has not been read yet.Please try later\n");  
  52.     }  
  53.     else   
  54.     {  
  55.         printf("write %s to the FIFO\n",w_buf);  
  56.     }  
  57.     close(fd); //關(guān)閉管道  
  58.     return 0;  
  59. }  


運行讀進程結(jié)果如下:

打開另外一個終端,運行寫進程結(jié)果如下:

同時讀進程結(jié)果發(fā)生變化如下:

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
有名管道和無名管道
Linux進程間通信
Linux系統(tǒng)管道和有名管道的通信機制
MPlayer從模式說明
C語言進程間通信(二)
進程通信—管道
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服