IO学习系列之阻塞IO

  • 阻塞IO:
  • 若资源没有准备就绪,会阻塞等待资源
  • 若资源准备就绪,会获取相关资源
  • 特点:
  • 在所有的IO模型中,阻塞IO是最简单最常用效率最低的;
  • 写阻塞:
  • 无名管道有名管道等进程间的通信;
  • 读阻塞:
  • 管道为例,具体读阻塞操作为:
  • 当进程执行到读操作的时候,若缓冲区有内容,则读取内容继续向下执行,若缓冲区没有内容,进程进入休眠态,直到缓冲区中有内容,由内核唤醒该进程,来读取缓冲区内容,然后继续向下执行;
  • 三个写端:
c 复制代码
	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	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;
	}
c 复制代码
	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	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;
	}
c 复制代码
	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	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;
	}
  • 一个读端:
c 复制代码
	#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <stdbool.h>
	
	
	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;
	}
  • 运行结果:
c 复制代码
	myfifo1:hello
	myfifo2:world
	myfifo3:hi
	myfifo1:china
	myfifo2:beijing
	myfifo3:the create wall
	myfifo1:i love u
	myfifo2:miss u
	myfifo3:miss u
  • 仅供参考
相关推荐
ChoSeitaku9 分钟前
12.重复内容去重|添加日志|部署服务到Linux上(C++)
linux·c++·windows
挣扎与觉醒中的技术人14 分钟前
网络安全入门持续学习与进阶路径(一)
网络·c++·学习·程序人生·安全·web安全
haaaaaaarry20 分钟前
【分治法】线性时间选择问题
数据结构·算法
CS创新实验室29 分钟前
计算机考研之数据结构:P 问题和 NP 问题
数据结构·考研·算法
一颗小树x33 分钟前
Llama 3.1 本地电脑部署 Linux系统 【轻松简易】
linux·llama·本地部署·3.1
Java Fans38 分钟前
微信小程序——访问服务器媒体文件的实现步骤
服务器·微信小程序·小程序
技术小齐1 小时前
网络运维学习笔记 017HCIA-Datacom综合实验01
运维·网络·学习
yourkin6661 小时前
HTTPS(下)
服务器·网络协议·https
KingDol_MIni1 小时前
Spring Boot 集成 T-io 实现客户端服务器通信
java·服务器·spring boot
OTWOL1 小时前
【C++编程入门基础(一)】
c++·算法