溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

unix 父子進(jìn)程間通信-無(wú)名管道

發(fā)布時(shí)間:2020-07-05 00:34:39 來(lái)源:網(wǎng)絡(luò) 閱讀:337 作者:xieyihua 欄目:系統(tǒng)運(yùn)維
  1. #include <unistd.h> 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4. #include <unistd.h> 
  5. #include <string.h> 
  6. #include <sys/types.h> 
  7. #include <sys/wait.h> 
  8.  
  9.  
  10.  
  11. #define MAXLINE 100 
  12.  
  13. char buf[MAXLINE]; 
  14.  
  15.  
  16.  
  17. int main(int argc, char** agrv) 
  18.         int pipefd[2]; 
  19.         int ret; 
  20.          
  21.         if(0 != pipe(pipefd))//創(chuàng)建管道 
  22.         { 
  23.                 perror("pipe\n"); 
  24.                 exit(1); 
  25.         } 
  26.         //創(chuàng)建子程序 
  27.         if((ret=fork()) < 0)//創(chuàng)建失敗 
  28.         { 
  29.                 perror("fork\n"); 
  30.                 exit(1);         
  31.         } 
  32.         else if(ret == 0)//子程序 
  33.         { 
  34.                 FILE* fp; 
  35.                  
  36.                 close(pipefd[0]);//關(guān)閉管道可讀端 
  37.                  
  38.                 fp = fopen("./main.c","r"); //打開(kāi)已存在可讀文件 
  39.                 if(NULL == fp) 
  40.                 { 
  41.                         perror("fopen\n"); 
  42.                         exit(1);                 
  43.                 } 
  44.                  
  45.                 while(fgets(buf,MAXLINE,fp) != NULL)//將文件內(nèi)容全部讀入管道 
  46.                 { 
  47.                         int n = strlen(buf); 
  48.                         if(n != write(pipefd[1],buf,n)) 
  49.                         { 
  50.                                 perror("write\n"); 
  51.                                 exit(1);                            
  52.                         } 
  53.                 }//end while 
  54.                  
  55.                  
  56.                 if(ferror(fp))//在讀的過(guò)程文件是否有報(bào)錯(cuò) 
  57.                 { 
  58.                         perror("ferror\n"); 
  59.                         exit(1);                    
  60.                 } 
  61.                  
  62.                 close(pipefd[1]);//關(guān)閉文件可寫(xiě)端 
  63.                 exit(0);//退出子程序 
  64.         } 
  65.         else 
  66.         { 
  67.                 close(pipefd[1]);//關(guān)閉管道可寫(xiě)端 
  68.                  
  69.                 if(-1 == wait(NULL))//等待子程序完全將文件內(nèi)容讀入到管道 
  70.                 { 
  71.                         perror("wait\n"); 
  72.                         exit(1);                  
  73.                 } 
  74.                  
  75.                 if(dup2(pipefd[0],STDIN_FILENO) != STDIN_FILENO)//重新設(shè)置標(biāo)準(zhǔn)輸入為管道可讀端 
  76.                 { 
  77.                         perror("dup2\n"); 
  78.                         exit(1);                           
  79.                 } 
  80.                  
  81.                 if(execl("/bin/more","more",(char*)0) < 0 )//分頁(yè)程序 自動(dòng)調(diào)用標(biāo)準(zhǔn)輸入文件 顯示管道內(nèi)容 
  82.                 { 
  83.                         perror("execl\n"); 
  84.                         exit(1);                   
  85.                 } 
  86.          
  87.         } 
  88.         return 0; 

 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI