5

网络编程-Linux高性能服务器编程

 2 years ago
source link: https://mikeygithub.github.io/2022/08/13/yuque/cp0k82/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

image.png

image.png

1659328210018-2cd496c2-2769-4e37-87c7-b5f9db5bd187.jpeg

第六章 I/O 复用

I/O 复用使得程序同时监听多个文件描述符

通常网络程序在以下情况需要使用到 I/O 复用

  • 客户端程序(用户进程)要同时处理多个 socket
  • 客户端程序要同时处理用户输入和网络连接
  • TCP 服务要同时处理监听 socket 和连接 socket(这是 I/O 复用最多的场景)
  • 服务器需要同时处理 TCP 请求和 UDP 请求
  • 服务器要同时监听多个端口,或者处理多种服务

Linux 下实现 I/O 复用的系统调用主要有 select、poll 和 epoll

select 系统调用

select 系统调用的用途是:在一段时间内,监听用户感兴趣的文件描述符上的可读、可写和异常事件。

//代码基于Linux5-19-RC8.linux/fs/select.c


/* for select() */
typedef struct {
uint32_t fd32[(FD_SETSIZE + 31) / 32];
} fd_set;

Linux 高性能服务器编程.pdf


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK