当前位置: 首页 > news >正文

Day05 linux高级系统设计 - 管道

复制文件描述符

dup函数

作用:

文件描述符复制

语法:

        #include <unistd.h>

        int dup (int oldfd);

参数:

        所需复制得文件描述符

返回值:

        复制到的文件描述符

功能:

        从文件描述符表中,找一个最小可能的文件描述符(通过返回值返回)作为oldfd复制

示例1:

#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{int newpd = dup(1);write(newpd,"hello world\n",12);return 0;
}

dup2函数(推荐)

函数:

#include <unistd.h>

int dup2(int oldfd,int newfd);

参数:

oldfd:原文件描述符

newfd:指定复制到的文件描述符,如果该文件描述符已存在,那将已存在的关闭

示例:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{int fd = open("huange.txt",O_WRONLY | O_CREAT | O_APPEND,0666);int pd = dup2(fd,1);printf("娶你明年\n");return 0;
}

无名管道

概述

又名管道 (pipe)
无名管道是一种特殊类型的文件,在应用层体现为两个打开的文件描述符 ,1 个描述符写 fd[1], 1 个描述符读 fd[0]
核心:0读1写
特点
1.管道不是普通的文件,不属于某个文件管理系统,存在于内存中
2.半双工:数据在同一时间只能在单向传输
补充:
单工:指数据只能在单向传输数据
双工:指在两台通讯设备之间,允许有双向的数据传输
全双工:允许两台通讯设备之间同时进行双向数据传输。一般的电话,手机就是全双工系统,因为在讲话的同时也能听到对方的声音。
半双工:允许两台通讯设备之间进行双向数据传输,但不能同时进行。因此同一时间只允许一个设备进行传输数据,若另一设备需要传输数据,则需要等待上一个传输数据的设备传输结束后,才可以传输数据。
3.数据只允许从管道的一端写入,从管道的另一端读取
4.写入管道中的数据遵循先入先出的原则        
5.管道所传输的数据是无格式的,这要求管道的读出方和写入放提前商量好一个文件的格式,如多少字节算一则消息
6.管道在内存中对应一个缓冲区。不通的系统所对应的大小不一样。
7.数据从管道中读取是一次性操作,管道中数据一旦被读取,管道就会抛弃该数据,以便于释放空间来写入其它数据
8.管道没有名字,只能在具有公共祖先的进程中使用
补充: 管道可以用于任意两个或更多相关进程之间的通信,只要在创建子进程 的系列调用之前通过一个共同的祖先进程创建管道即可。
        如管道可用于一个进程和其子孙进程之间的通信。第一个进程创建管道,然后创建子进程,接着子进程再创建第一个进程的孙子进程。
        管道通常用于两个兄弟进程之间的通信—— 它们的父进程创建了管道,并创建两个子进程。

pipe函数

作用:用于创建无名管道

语法:

#include <unistd.h>

int fd[] ;

pipe(fd);

参数:

fd为int型数据的首元素地址,其存放了管道的文件描述符反对fd[0],fd[1]

f[0]为读而打开管道,f[1]为写而打开

返回值:

        成功:0

        失败:-1

int fd[2];

pipe(fd);

示例:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
int main(int argc, char const *argv[])
{int fd[2];pipe(fd);int i = 0;for(i = 0;i < 2;i++){int pid = fork();if(pid == 0){break;}}if(i == 0){close (fd[0]);char buf[] = "hello to2";write(fd[1],buf,sizeof(buf));close(fd[1]);_exit(0);}else if(i == 1){close (fd[1]);char buf[60] = {0};read(fd[0],buf,sizeof(buf));close(fd[0]);printf("%s\n",buf);_exit(0);}else if(i == 2){while(1){int pid = waitpid(-1,NULL,WNOHANG);if(pid == 0){break;}}}return 0;
}

读写特点

1.默认用read函数从管道中读取数据时是阻塞的

2.调用write函数写入时,当管道的缓冲区已满时,也会发生阻塞,管道的缓冲区大小时65kb

3.通信过程中,读端口全部关闭后,写进程向管道内写数据时,写进程会(收到SIGPIPE 信号)退出。

4.从管道中读取数据的特点  编程时可通过fcntl函数设置文件的阻塞特性。设置为阻塞:fcntl(fd,FSETFL,O_NONBLOCK);

示例一:缓冲区已满时 write 也会阻塞。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{int fd[2] ;pipe(fd);int pid = fork();if(pid == 0){close(fd[1]);sleep(2);close(fd[0]);_exit(0);}else if(pid > 0){close(fd[0]);//关闭读int count = 0;for(int i = 0;i < 10000;i++){int buf[1024] = {0};printf("buf=%ld\n",sizeof(buf)/sizeof(buf[0]));write(fd[1],buf,1024);count += 1024;printf("i=%d\ncount%d\n",i,count);}  close(fd[1]);wait(NULL);}return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{//证明:调用 write 函数向管道里写数据,当缓冲区已满时 write 也会阻塞int fd[2];pipe(fd);for (int i = 0; i < 1000; i++){char buf[1024] = {0};write(fd[1],buf,1024);printf("i = %d\n",i);}printf("OVER\n");return 0;
}

示例二:通信过程中,写端关闭,读端将解阻塞

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{int fd[2];pipe(fd);int pid = fork();if(pid == 0){close(fd[1]);int buf[100] = {0};printf("开始读取\n");read(fd[0],buf,100);close(fd[0]);printf("读取结束\n");_exit(0);}else if(pid > 0){close(fd[0]);sleep(2);close(fd[1]);sleep(5);_exit(0);}return 0;
}

示例三:通信过程中 读端关闭 写端将收到SIGPIPE信号 退出写端进程

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{int fd[2];pipe(fd);int pid = fork();if (pid == 0){close(fd[1]);while (1){int i = 0;char buf[128] = "";int len = read(fd[0], buf, sizeof(buf) / sizeof(buf[0]));i++;printf("len=%d\n", len);if (i == 5){break;}}}else if (pid > 0){close(fd[0]);while(1){printf("给父进程%u写数据\n",getpid());write(fd[1],"hello pipe",10);sleep(1);}close(fd[1]);wait(NULL);}return 0;
}

综合案例

要求 : 使用代码实现 ps -A | grep bash
示例:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{int pd[2];pipe(pd);int pid = fork();if(pid < 0){printf("输入有误\n");return 0;}else if(pid  == 0){close(pd[1]);dup2(pd[0],0);execl("/bin/grep","/bin/grep","bash",NULL);_exit(-1);}else if(pid > 0){close(pd[0]);dup2(pd[1],1);execl("/bin/ps","ps","-A",NULL);wait(NULL);}return 0;
}

有名管道

概述:

又名命名管道(FIFO)

特点 :
1 、半双工,数据在同一时刻只能在一个方向上流动。
2 、写入 FIFO 中的数据遵循先入先出的规则。
3 FIFO 所传送的数据是无格式的,这要求 FIFO 的读出方与写入方必须事先约定好数据的格式,如多少字节算一个消息等。
4 FIFO 在文件系统中作为一个特殊的文件而存在,但 FIFO 中的内容却存放在内存中。
5 、管道在内存中对应一个缓冲区。不同的系统其大小不一定相同。
6 、从 FIFO 读数据是一次性操作,数据一旦被读,它就从 FIFO 中被抛弃,释放空间以便写更多的数据。
7 、当使用 FIFO 的进程退出后, FIFO 文件将继续保存在文件系统中以便以后使用。
8 FIFO 有名字,不相关的进程可以通过打开命名管道进行通信。

概述

mkfifo函数

作用:创建有名管道

语法:

        #include <sys/types.h>

        #include <sys/stat.h>

int mkfifo(const char *pathname,mode_t mode);

参数:

        pathname:文件名

mode:文件操作模式,一般使用0666(所有用户可读可写)

返回值

        成功:0

        失败:-1 一般失败是因为有与pathname名相同的文件

读写特点

1 open 打开管道 不指定 O_NONBLOCK ( 阻塞 )
1 open 以只读方式打开 FIFO 时,要阻塞到某个进程为写而打开此 FIFO
2 open 以只写方式打开 FIFO 时,要阻塞到某个进程为读而打开此 FIFO
3 open 以只读、只写方式打开 FIFO 时会阻塞,调用 read 函数从 FIFO 里读数据
read 也会阻塞。
4 、通信过程中若写进程先退出了,则调用 read 函数从 FIFO 里读数据时不阻塞;若写进程又重新运行,则调用 read 函数从 FIFO 里读数据时又恢复阻塞。
5 、通信过程中,读进程退出后,写进程向命名管道内写数据时,写进程也会(收到 SIGPIPE 信号)退出。
6 、调用 write 函数向 FIFO 里写数据,当缓冲区已满时 write 也会阻塞。
2 open 打开管道 指定 O_NONBLOCK ( 非阻塞 )
1 、先以只读方式打开:如果没有进程 , 已经为写而打开一个 FIFO, 只读 open 成功,并且 open 不阻塞。
2 、先以只写方 式打开:如果没有进程 , 已经为读而打开一个 FIFO ,只写 open 将出错返回-1
3 read write 读写命名管道中读数据时不阻塞。
4 、通信过程中,读进程退出后, 写进程向命名管道内写数据时,写进程也会(收到SIGPIPE 信号)退出。
3 、 注意: open 函数以可读可写方式打开 FIFO 文件时的特点:
1 open 不阻塞。
2 、调用 read 函数从 FIFO 里读数据时 read 会阻塞。
3 、调用 write 函数向 FIFO 里写数据 , 当缓冲区已满时 write 也会阻塞

综合案例

09_demoA.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char const *argv[])
{int i = 0;for(i = 0;i < 2;i++){int pid = fork();if(pid == 0){break;}}if(i == 0){mkfifo("./zjrtozl",0666);//张晋若发送消息int fd = open("./zjrtozl",O_WRONLY);while(1){char buf[100] = {0};fgets(buf,100,stdin);                                        printf("张晋若:%s\n",buf);write(fd,buf,strlen(buf));buf[strlen(buf)-1] = 0;if(strcmp("886",buf) == 0){break;}}close(fd);_exit(0);}else if(i == 1){mkfifo("./zltozjr",0666);//张立接收消息int fd = open("./zltozjr",O_RDONLY);while(1){char buf[100] = {0};read(fd,buf,sizeof(buf));printf("张立:%s\n",buf);buf[strlen(buf)-1] = 0;if(strcmp("886",buf) == 0){break;}}close(fd);_exit(0);}else if(i == 2){while(1){int pid = waitpid(-1,NULL,WNOHANG);if(pid == -1){break;//子进程已经回收完毕}}}return 0;
}

09_demoB.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char const *argv[])
{int i = 0;for(i=0;i < 2;i++){int pid = fork();if(pid == 0){break;}}if(i == 0){mkfifo("./zltozjr",0666);//张立发消息int fd = open("./zltozjr",O_WRONLY);while(1){char buf[120] = {0};fgets(buf,120,stdin);printf("张立:%s\n",buf);write(fd,buf,strlen(buf));buf[strlen(buf)-1] = 0;if(strcmp("886",buf) ==0){break;}}close(fd);_exit(0);}else if(i == 1){mkfifo("./zjrtozl",0666);//张晋若接收消息int fd =open("./zjrtozl",O_RDONLY);while(1){char buf[] = {0};read(fd,buf,sizeof(buf));printf("张晋若:%s\n",buf);buf[strlen(buf)-1] = 0;if(strcmp("886",buf)==0){break;}}close(fd);_exit(0);}else if(i == 2){while(1){int pid = waitpid(-1,NULL,WNOHANG);if(pid == -1){break;//子进程回收完毕}}}return 0;
}

总结

无名管道与有名管道的使用场景

1.无名管道应用于有血缘关系的进程中

2.有名管道应用于没有血缘关系的进程中

有名管道与无名管道的区别

1.无名管道是基于内存的,无需文件管理系统

2.有名管道是基于文件和内存的,需要文件管理系统

dup

作用;复制文件描述符

意义:可以通过dup函数实现文件的重定向

http://www.lryc.cn/news/256237.html

相关文章:

  • 低代码:美味膳食或垃圾食品?
  • 免费网页抓取工具大全【附下载和工具使用教程】
  • Leetcode 39 组合总和
  • Windows下使用AndroidStudio及CMake编译Android可执行程序或静态库动态库
  • MySQL七 | 存储引擎
  • 网上下载的pdf文件,为什么不能复制文字?
  • Linux下apisix离线安装教程
  • 基于STM32 + DMA介绍,应用和步骤详解(ADC多通道)
  • openGauss学习笔记-144 openGauss 数据库运维-例行维护-慢sql诊断
  • 计算机毕业设计springboot+ssm停车场车位预约系统java
  • 打破常规思维:Scrapy处理豆瓣视频下载的方式
  • 系列学习前端之第 2 章:一文精通 HTML
  • SCSS Module 这样处理配置和使用太赞了
  • 【Unity动画】Unity 2D动画创建流程
  • 【算法每日一练]-图论(保姆级教程篇12 tarjan篇)#POJ3352道路建设 #POJ2553图的底部 #POJ1236校园网络 #缩点
  • Python数据科学视频讲解:数据挖掘与建模的注意事项
  • unity | 动画模块之循环滚动选项框
  • TinyMPC - CMU (卡耐基梅隆大学)开源的机器人 MPC 控制器
  • C++ 对象的初始化和清理:构造函数和析构函数
  • Tmux中使用Docker报错 - 解决方案
  • 如何在WordPress中批量替换图片路径?
  • el-pagination 纯前端分页
  • 基于springboot的校园二手市场
  • 【开源】基于Vue和SpringBoot的在线课程教学系统
  • Mysql分布式集群部署---MySQL集群Cluster将数据分成多个片段,每个片段存储在不同的服务器上
  • 身份认证技术
  • Centos7、Mysql8.0 load_file函数返回为空的终极解决方法--暨selinux的深入理解
  • 基于Spring Boot的水产养殖管理系统
  • LCR 090. 打家劫舍 II(leetcode)动态规划
  • 【小沐学Python】Python实现语音识别(Whisper)