• IO学习系列之阻塞IO


    • 阻塞IO:
    • 若资源没有准备就绪,会阻塞等待资源
    • 若资源准备就绪,会获取相关资源
    • 特点:
    • 在所有的IO模型中,阻塞IO是最简单最常用效率最低的;
    • 写阻塞:
    • 无名管道有名管道等进程间的通信;
    • 读阻塞:
    • 管道为例,具体读阻塞操作为:
    • 当进程执行到读操作的时候,若缓冲区有内容,则读取内容继续向下执行,若缓冲区没有内容,进程进入休眠态,直到缓冲区中有内容,由内核唤醒该进程,来读取缓冲区内容,然后继续向下执行;
    • 三个写端:
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	
    	int main(int argc, char const *argv[])
    	{
    	    int fd = open("myfifo1",O_WRONLY);
    	    char buf[128] = {0};
    	
    	    while(true)
    	    {
    	        memset(buf,0,sizeof(buf));
    	
    	        fgets(buf,sizeof(buf),stdin);
    	
    	        buf[strlen(buf)-1] = '\0';
    	
    	        write(fd,buf,sizeof(buf));
    	    }
    	    return 0;
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	
    	int main(int argc, char const *argv[])
    	{
    	    int fd = open("myfifo2",O_WRONLY);
    	    char buf[128] = {0};
    	
    	    while(true)
    	    {
    	        memset(buf,0,sizeof(buf));
    	
    	        fgets(buf,sizeof(buf),stdin);
    	
    	        buf[strlen(buf)-1] = '\0';
    	
    	        write(fd,buf,sizeof(buf));
    	    }
    	    return 0;
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	
    	int main(int argc, char const *argv[])
    	{
    	    int fd = open("myfifo3",O_WRONLY);
    	    char buf[128] = {0};
    	
    	    while(true)
    	    {
    	        memset(buf,0,sizeof(buf));
    	
    	        fgets(buf,sizeof(buf),stdin);
    	
    	        buf[strlen(buf)-1] = '\0';
    	
    	        write(fd,buf,sizeof(buf));
    	    }
    	    return 0;
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 一个读端:
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	#include 
    	
    	
    	int main(int argc, char const *argv[])
    	{
    	    int fd1 = open("myfifo1",O_RDONLY);
    	    int fd2 = open("myfifo2",O_RDONLY);
    	    int fd3 = open("myfifo3",O_RDONLY);
    	
    	    char buf[128] = {0};
    	
    	    while(true)
    	    {
    	        memset(buf,0,sizeof(buf));
    	        read(fd1,buf,sizeof(buf));
    	        printf("myfifo1:%s\n",buf);
    	
    	        memset(buf,0,sizeof(buf));
    	        read(fd2,buf,sizeof(buf));
    	        printf("myfifo2:%s\n",buf);
    	
    	        memset(buf,0,sizeof(buf));
    	        read(fd3,buf,sizeof(buf));
    	        printf("myfifo3:%s\n",buf);       
    	
    	    }
    	
    	    close(fd1);
    	    close(fd2);
    	    close(fd3);
    	    return 0;
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 运行结果:
    	myfifo1:hello
    	myfifo2:world
    	myfifo3:hi
    	myfifo1:china
    	myfifo2:beijing
    	myfifo3:the create wall
    	myfifo1:i love u
    	myfifo2:miss u
    	myfifo3:miss u
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 仅供参考
  • 相关阅读:
    Web开发-新建Spring Boot项目
    阶段总结(技术向)
    第四章:数据操作Ⅰ 第二节:读写CSV文件
    qt day 6
    Map集合的遍历:键值对
    2022-10-28 开源会议分享开场词
    深度学习-参数量&模型大小&理论计算量
    Eureka
    人工智能--机器学习概述、motplotlib的使用-折线图、散点图、柱状图、饼图
    一个简单证件照的设计过程
  • 原文地址:https://blog.csdn.net/qq_41878292/article/details/134228657