操作系统第3次实验报告:管道

  • 毛琳淇
  • 201821121007
  • 计算1811

1. 编写程序

在服务器上用Vim编写程序:创建一个命名管道,创建两个进程分别对管道进行读fifo_read.c和写fifo_write.c。给出源代码。

fifo_read.c

 #include<stdio.h>

 #include<string.h>

 #include<unistd.h>

 #include<sys/types.h>

 #include<sys/stst.h>

 #include<fcntl.h>

  int main(){

      int fd,ret;

      ret=mkfifo("myfifo",0777);

      if(ret!=o){

          perror("mkfifo");

      }

      fd=open("myfifo",O_RDONLY);

      if(fd<0){

          perror("openfifo error");

      }

      while(1){

          char infm[100]={0};

          read(fd,infm,sizeof(infm));

          printf("read%s/n",infm);

      }

      close(fd);

      return 0;

  }

fifo_write.c

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main( ){
 int fd,ret;

 ret=mkfifo("myfifo",0777);

 if(ret!=0){

      perror("mkfifo");

}

 fd=open("myfifo",O_WRONLY);

 if(fd<0){

       perror("openfifo error");

   }

 while(1){

      char send[200];

      scanf("%s",send);

     }

  close(fd);

  return 0;

}

2. 分析运行结果

输入端

 

 

 

判断管道是否建立成功,再输入内容。

输出端

 

 

判断管道是否建立成功,再输出输入端的内容。

管道模式:

O_RDONLY读管道

O_WRONLY写管道

O_RDWR读写管道

O_NONBLOCK非阻塞

3. 通过该实验产生新的疑问及解答

一开始启动在启动fifo_write.c前要先启动fifo_read.c后才能成功运行,否则读管道则会阻塞直到写管道输入内容。

posted @ 2020-04-16 23:50  my7in7i  阅读(202)  评论(0编辑  收藏  举报