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
  • 仅供参考
相关推荐
code bean1 分钟前
【C#基础】函数传参大总结
服务器·开发语言·c#
shelby_loo2 分钟前
通过 Docker 部署 WordPress 服务器
服务器·docker·容器
Hqst_Kevin9 分钟前
Hqst 品牌 H81801D 千兆 DIP 网络变压器在光猫收发器机顶盒中的应用
运维·服务器·网络·5g·网络安全·信息与通信·信号处理
前端-文龙刚13 分钟前
小程序给对象赋值(双向绑定)方法之一
服务器·小程序·apache
mez_Blog18 分钟前
个人小结(2.0)
前端·javascript·vue.js·学习·typescript
DREAM依旧18 分钟前
《深入了解 Linux 操作系统》
linux
Rookie也要加油19 分钟前
WebRtc一对一视频通话_New_peer信令处理
笔记·学习·音视频·webrtc
孙小二写代码20 分钟前
[leetcode刷题]面试经典150题之1合并两个有序数组(简单)
算法·leetcode·面试
QXH20000020 分钟前
数据结构—单链表
c语言·开发语言·数据结构
little redcap25 分钟前
第十九次CCF计算机软件能力认证-1246(过64%的代码-个人题解)
算法