【Linux网络】五种IO模型与非阻塞IO

五种IO模型与非阻塞IO


文章目录


五种IO模型










高级IO重要概念



实现函数SetNoBlock


轮询方式获取标准输入

代码如下(示例):

c 复制代码
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void SetNoBlock(int fd) {
	int fl = fcntl(fd, F_GETFL);
	if (fl < 0) {
		perror("fcntl");
		return;
	}
	fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}
int main() {
	SetNoBlock(0);
	while (1) {
		char buf[1024] = { 0 };
		ssize_t read_size = read(0, buf, sizeof(buf) - 1);
		if (read_size < 0) {
			perror("read");
			sleep(1);
			continue;
		}
		printf("input:%s\n", buf);
	}
	return 0;
}

相关推荐
哈基咪怎么可能是AI3 小时前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行19 小时前
Linux和window共享文件夹
linux
Sinclair1 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
木心月转码ing1 天前
WSL+Cpp开发环境配置
linux
Rockbean2 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
茶杯梦轩2 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
崔小汤呀2 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应2 天前
vi编辑器使用
linux·后端·操作系统
何中应2 天前
Linux进程无法被kill
linux·后端·操作系统
何中应2 天前
rm-rf /命令操作介绍
linux·后端·操作系统