无名管道pipe

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

int main()
{
 pid_t pid;
 int pipefd[2];
 pipe(pipefd);
 pid = fork();
 if(pid == 0){//子进程
  char buf_2[20];
  bzero(buf_2, 20);
  sleep(2);
  read(pipefd[0], buf_2, 17);//从管道的读端读出数据
  printf("%s",buf_2);
  close(pipefd[0]);
  close(pipefd[1]);
  exit(0);
 }
 else{//父进程
  char buf[] = "test for the pipe";
  write(pipefd[1], buf, 17);//从管道的写端写入数据
  close(pipefd[0]);
  close(pipefd[1]);
 }
 return 0;
}

posted on 2012-04-07 20:43  小风儿_xf  阅读(193)  评论(0编辑  收藏  举报

导航