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

  • 姓名:白晓
  • 学号:201821121035
  • 班级:计算1812

1. 编写程序

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

fifo.read.c

 

 1    #include<stdio.h>
 2    #include<stdlib.h>
 3    #include<unistd.h>
 4    #include<fcntl.h>
 5    #include<string.h>
 6    #include<sys/types.h>
 7    #include<sys/stat.h>
 8    #include<errno.h>
 9    int main(){
10       printf("read program\n");
11       int rfd=open("fifo",O_RDONLY);
12       int ret=mkfifo("fifo",0666);
13       if(rfd==-1){
14           printf("Open file error!\n");
15           exit(1);
16       }
17       printf("Open file success!\n");
18       if((ret<0)&&(errno!=EEXIST)){
19           printf("make fifo error\n");
20           exit(1);
21       }
22       while(1){
23           char buff[1024];
24           ret=read(rfd,buff,1024);
25           if(ret>0){
26               printf("read:%s\n",buff);
27           }
28       }
29       close(rfd);
30       return 0;
31   }

fifo_write.c

 1    #include<stdio.h>
 2    #include<stdlib.h>
 3    #include<unistd.h>
 4    #include<fcntl.h>
 5    #include<sys/types.h>
 6    #include<sys/stat.h>
 7    #include<errno.h>
 8    #include<string.h>
 9    int main(){
10       printf("read program\n");
11       int rfd=open("fifo",O_WRONLY);
12       int ret=mkfifo("fifo",0666);
13       if(rfd==-1){
14           printf("Open file error!\n");
15           exit(1);
16       }
17       printf("Open file success!\n");
18       if((ret<0)&&(errno!=EEXIST)){
19           printf("make fifo error\n");
20           exit(1);
21       }
22       while(1){
23           char buff[1024];
24           printf("write:");
25           scanf("%s",buff);
26           ret=write(rfd,buff,strlen(buff));
27           }
28       close(rfd);
29       return 0;
30 }

2. 分析运行结果

给出运行结果,并分析。

打开两个cmd,一个运行fifo_read.c,一个运行fifo_write.c。

O_RDONLY:读管道

O_WRONLY:写管道

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

运行程序时出现如上图所示,应使用两个cmd分别运行两个文件,并图示为管道阻塞。

posted @ 2020-04-17 21:04  木澈  阅读(148)  评论(0编辑  收藏  举报