根据注解完成文件读写所有填空。管道分为无名管道和有名管道,无名管道用 pipe 创建,有名管道用 mkfifo 创建,管道实际上是文件,一端用于读,一端用于写。 #include
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[]) { int fd[2]; // 创建无名管道 fd if( 填空 1 <0) printf("unable to create noname pipe\n"); else printf("create noname pipe success and the pipe ID are %d and %d \n",fd[0],fd[1]); char * pathname="mkfifofile"; unlink(pathname); // 创建有名管道,其存取权限为最高权限 if( 填空 2 <0) printf("unable to create pipe with name\n"); else printf("create pipe with name success\n"); int filefd; int len; char * buf[1024]; // 创建进程 if( 填空 3 ==0) { // 子进程将 pathname 的内容写入无名管道 len= 填空 4 ; printf("write info into the child of noname pipe is %s and the string length is %d\n",pathname,len); close(fd[0]); close(fd[1]); // 打开有名管道 filefd= 填空 5 ; if(filefd<0) printf("open pipe failed\n"); else { printf("open pipe success\n"); // 子进程将 pathname 的内容写入有名管道 filefd len=write(filefd,pathname,strlen(pathname)); printf("write info into the child of pipe with name is %s and the string length is %d\n",pathname,len); } close(filefd); } else { // 父进程读取无名管道内容到 buf 中 len= 填空 6 ; buf[len]='\0'; printf("read info from the child of noname pipe is %s and the string length is %d\n",buf,len); close(fd[0]); close(fd[1]); filefd=open(pathname,O_RDONLY); if(filefd<0) printf("open pipe failed\n"); else { printf("open pipe success\n"); // 父进程读取有名管道内容到 buf 中 len= 填空 7 ; buf[len]='\0'; printf("read info from the child of pipe with name is %s and the string length is %d\n",buf,len); } // 关闭有名管道 filefd 填空 8 ; } }