注意:从管道读数据是一次性操作,数据一旦被读,它就从管道中被抛弃,释放空间以便写更多的数据
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main(void)
{
intfd[2],nbytes; pid_tchildpid;
charstring[]="Hello,world!\n";
charreadbuffer[80]; pipe(fd);
if((childpid=fork())==-1) { perror("fork"); exit(1); }
if(childpid==0) { /*Child process closes up in put side of pipe*/ close(fd[0]); /*Send"string"through the out put side of pipe*/
write(fd[1],string,strlen(string));
exit(0); }
else {
/*Parent process closes up out put side of pipe*/ close(fd[1]);
/*Readinastringfromthepipe*/ nbytes=read(fd[0],readbuffer,sizeof(readbuffer));
printf("Receivedstring:%s",readbuffer); }
return(0);
}