unix父子进程间通信-无名管道

  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  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))//创建管道 
  22.         { 
  23.                 perror("pipe\n"); 
  24.                 exit(1); 
  25.         } 
  26.         //创建子程序 
  27.         if((ret=fork()) < 0)//创建失败 
  28.         { 
  29.                 perror("fork\n"); 
  30.                 exit(1);         
  31.         } 
  32.         else if(ret == 0)//子程序 
  33.         { 
  34.                 FILE* fp; 
  35.                  
  36.                 close(pipefd[0]);//关闭管道可读端 
  37.                  
  38.                 fp = fopen("./main.c","r"); //打开已存在可读文件 
  39.                 if(NULL == fp) 
  40.                 { 
  41.                         perror("fopen\n"); 
  42.                         exit(1);                 
  43.                 } 
  44.                  
  45.                 while(fgets(buf,MAXLINE,fp) != NULL)//将文件内容全部读入管道 
  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))//在读的过程文件是否有报错 
  57.                 { 
  58.                         perror("ferror\n"); 
  59.                         exit(1);                    
  60.                 } 
  61.                  
  62.                 close(pipefd[1]);//关闭文件可写端 
  63.                 exit(0);//退出子程序 
  64.         } 
  65.         else 
  66.         { 
  67.                 close(pipefd[1]);//关闭管道可写端 
  68.                  
  69.                 if(-1 == wait(NULL))//等待子程序完全将文件内容读入到管道 
  70.                 { 
  71.                         perror("wait\n"); 
  72.                         exit(1);                  
  73.                 } 
  74.                  
  75.                 if(dup2(pipefd[0],STDIN_FILENO) != STDIN_FILENO)//重新设置标准输入为管道可读端 
  76.                 { 
  77.                         perror("dup2\n"); 
  78.                         exit(1);                           
  79.                 } 
  80.                  
  81.                 if(execl("/bin/more","more",(char*)0) < 0 )//分页程序 自动调用标准输入文件 显示管道内容 
  82.                 { 
  83.                         perror("execl\n"); 
  84.                         exit(1);                   
  85.                 } 
  86.          
  87.         } 
  88.         return 0; 

 

创新互联专注于八宿企业网站建设,响应式网站建设,商城网站制作。八宿网站建设公司,为八宿等地区提供建站服务。全流程专业公司,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务


本文题目:unix父子进程间通信-无名管道
文章位置:http://pwwzsj.com/article/ghhpeh.html