[email protected] 04]# cat ex04-3-pipe02.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main (void) {
int result=-1;
int fd[2],nbytes;
pid_t pid;
Char string[]= "Hello,pipe";
Char readbuffer[80];
int *write_fd = &fd[1]; can be written
int *read_fd = &fd[0]; Read
result = pipe (FD); Create pipe
if ( -1 = = result) {
printf ("Create pipe Faile");
return-1;
}
Pid=fork (); Bifurcation procedure
if ( -1 = = pid) {
printf ("fork process failed \ n");
return-1;
}
if (0 = = pid) {
Close (*READ_FD);
/* Write characters to the pipe end */
Result=write (*write_fd,string,strlen (string));
}else{
Close (*WRITE_FD);
/* Read PIPE data */
Nbytes=read (*read_fd,readbuffer,sizeof (Readbuffer));
printf ("Received%d data, content:%s \ n", Nbytes,readbuffer);
printf ("sizeof (readbuffer) =%d\n", sizeof (Readbuffer));
}
return 0;
}
--------------Test Results
[Email protected] 04]#./ex04-3-pipe02
10 data received, content: Hello,pipe
sizeof (Readbuffer) = 80