父子进程IO读写数据;跟FILE *fp有点类似
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int _child_process_read(int fd)
{int n;int old_offset = 0;char buf[1024];while(1){lseek(fd,old_offset,SEEK_SET);n = read(fd,buf,sizeof(buf) - 1);buf[n] = '\0';if(strncmp(buf,"quit",4) == 0)break;old_offset = lseek(fd,0,SEEK_CUR);printf("Read %d bytes : %s.\n",n,buf);}return 0;
}int child_process_read(int fd,const char *name)
{int n;char buf[1024];close(fd);if((fd = open(name,O_RDONLY)) < 0){fprintf(stderr,"Fail to open %s : %s.\n",name,strerror(errno));return -1;}while(1){while((n = read(fd,buf,sizeof(buf) - 1)) == 0);buf[n] = '\0';if(strncmp(buf,"quit",4) == 0)break;printf("Read %d bytes : %s.\n",n,buf);}return 0;
}int parent_