linux-C管道

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void read_data(int pipes[]){//从管道读数据
  char c;
  int rc;
  close(pipes[1]);//读进程不用写管道pipes[1]为写管道
  while ((rc=read(pipes[0],&c,1))>0){
     printf("%c",c);
  }
  close(pipes[0]);
  exit(0);
}

void write_data(int pipes[]){//写管道数据
  char c;
  int rc;
  close(pipes[0]);//写进程不用读管道pipes[0]为读管道
  scanf("%c",&c);
  while (c!='x'){//读到x后退出
     rc=write(pipes[1],&c,1);
     scanf("%c",&c);
     if (rc==-1){  //错误
        perror("parent write error");
        close(pipes[1]);
        exit(1);
     }
  }
  close(pipes[1]);
  exit(0);
}

int main(int argc,char *argv[]){
  int pipes[2];
  pid_t pid;
  int rc;

  rc=pipe(pipes);
  if (rc==-1){  
     perror("pipe error");
     exit(1);
  }
  pid=fork();
  if (pid==0) read_data(pipes);
  else write_data(pipes);
  return 0;
}

 knoppix@Microknoppix:/mnt-system/deepfuture$ gcc -o test11 test11.c
test11.c: In function 'read_data':
test11.c:13: warning: incompatible implicit declaration of built-in function 'exit'
test11.c: In function 'write_data':
test11.c:27: warning: incompatible implicit declaration of built-in function 'exit'
test11.c:31: warning: incompatible implicit declaration of built-in function 'exit'
test11.c: In function 'main':
test11.c:42: warning: incompatible implicit declaration of built-in function 'exit'
knoppix@Microknoppix:/mnt-system/deepfuture$ ./test11
q
q
r
r
u
u
k
k
x

你可能感兴趣的:(C++,c,linux,gcc,C#)