#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
int fd[2];
int nbytes;
pid_t childpid;
char string[] = "hello,Freeking/n";
char readbuf[40];
pipe( fd );
if( ( childpid = fork()) == -1 )
{
perror( "fork error!" );
exit(1);
}
if( childpid == 0 )
{
//子程序關閉管道的讀句柄
close( fd[0] );
//通過寫句柄向管道寫入資訊
write( fd[1], string, strlen(string) );
_exit(0);
}
else
{
//父程序關閉管道的讀句柄
close( fd[1] );
//通過讀句柄向管道讀取資訊
nbytes = read(fd[0], readbuf, sizeof(string) );
readbuf[nbytes] = '/0';
printf( "Receive string: %s", readbuf );
}
return 0;
}