pipe

来源:互联网 发布:2016年淘宝99大促时间 编辑:程序博客网 时间:2024/06/09 14:11

fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃[非标准]

  fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上

#include <stdio.h>
#include<unistd.h>
#include <errno.h>
#define MAXSIZE 128
int main()
{
    int filedis[2];//对应管道的头部和尾部,单向,写的时候不能读
int f[2];
   int pid;
    int ret=0;
    int retsize;
char test[]="hello world ";
char message[MAXSIZE];//="hahaha";
extern int errno;
    char readbuffer[MAXSIZE];
char result[MAXSIZE];
     if((ret=pipe(filedis))!=-1); //与子进程通讯需在fork之前调用 
       {
       printf("%d,%s\n",ret,strerror(errno));
       }
if(pipe(f)!=-1)
{
   printf("%s\n",strerror(errno));
}
     if((pid=fork())==0)//fork反回了2次,一次在父进程,一次在子进程
       {  //由子进程中调用
        sleep(1);//等待父进程写入
          close(filedis[1]);

 

 

#include <stdio.h>
#include <unistd.h>
 
int main ()
{
//popen 创建一个管道并启动另外一个进程,
//在调用进程和所执行的指令之间创建一个通道
//返回的指针FILE* 用于输入或者输出
   FILE* fs = popen ("sort", "w");
   if(fs==NULL){
    perror("call popen function fail");
    return 1;
   }
  
   fprintf (fs, "This is a simple program.\n");
   fprintf (fs, "Hello, Linux world.\n");
   fprintf (fs, "I like Linux.\n");
   fprintf (fs, "Only a test program.\n");

   pclose(fs);
   return 0;
}

 ///读写管道

FILE* stream_in;
 FILE* stream_out;
 int nLen;

 char buffer[BUFFER_SIZE];

 stream_in=popen(argv[1],"r");
 if(stream_in==NULL){
  perror("call popen function fail");
  return 1;
 }

 stream_out=popen(argv[2],"w");
 if(stream_out==NULL){
  perror("call popen function fail");
  return 1;
 }

 fflush(stream_out);

 nLen=read(fileno(stream_in),buffer,PIPE_BUF);
 while(nLen>0){
  write(fileno(stream_out),buffer,nLen);
  nLen=read(fileno(stream_in),buffer,PIPE_BUF); 
 }

 pclose(stream_in);
 pclose(stream_out);


         
          if((retsize=read(filedis[0],readbuffer,MAXSIZE))!=-1)//从管道中读,其中的数据读过一次就没了
           {
           printf("%d\n",retsize);
            strcpy(message,readbuffer);
           printf("%s\n",readbuffer);
           }
           close(filedis[0]);
 close(f[0]);
           if(write(f[1],message,strlen(message))!=-1)
           {
            printf("write back\n");
    }
            exit(0);
       }
 else if(pid>0)
         {
              //父进程
              close(filedis[0]);
              if((ret=write(filedis[1],test,sizeof(test)))!=-1)
               {
               printf("parent write buffer\n");
               }
           
            // sleep(3);
          wait(NULL);
             close(f[1]);
             if(read(f[0],result,MAXSIZE)!=-1)
              {
               printf("read from son %s\n",result);
  }
       }
       else {
          printf("fork error");
          }
       // close(filedis[0]);
       // close(filedis[1]);
         //
        //  exit(0);
          return 0;
}
使用管道实现父子进程间的双向通信示例

#include <stdio.h>
#include <unistd.h>

int main(int argc,char* argv[])
{
 int f_des1[2];
 int f_des2[2];

 int pid;
 char msg[BUFSIZ];
 char p_msg[BUFSIZ];
 
 if(argc!=2){
  printf("Usage: %s message\n",argv[0]);
  return 1;
 }

 if(pipe(f_des1)==-1){
  perror("cannot create the IPC pipe");
  return 1;
 }


 if(pipe(f_des2)==-1){
  perror("cannot create the IPC pipe");
  return 1;
 }

 pid=fork();
 if(pid==-1){
  perror("cannot create new process");
  return 1;
 }else if(pid==0){
  close(f_des1[1]);
  close(f_des2[0]);
  
  //read data from the parent process
  if(read(f_des1[0],msg,BUFSIZ)==-1){
   perror("child process cannot read data from pipe");
   return 1;
  }else
   printf("in child process, receive message: %s\n",msg);
  
  //write data to the parent process
                if(write(f_des2[1],msg,strlen(msg))==-1){
                        perror("child  process cannot write data to pipe");
                        return 1;
                }else
                        printf("in child  process, send message back: %s\n",argv[1]);

  _exit(0);
 }else {
  close(f_des1[0]);
  close(f_des2[1]);
  
  //write data to the child process 
  if(write(f_des1[1],argv[1],strlen(argv[1]))==-1){
   perror("parent process cannot write data to pipe");
   return 1;
  }else
   printf("in parent process, send message: %s\n",argv[1]);
  
  //read data from the cild process
                if(read(f_des2[0],p_msg,BUFSIZ)==-1){
                        perror("parent process cannot read data from pipe");
                        return 1;
                }else
                        printf("in parent process, receive message: %s\n",p_msg);

         
  wait(NULL);
  _exit(0);
 }

 return 0;
}