struct termios tio
是什么
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>#define SERIAL_PORT "/dev/ttyS0"
#define BUF_SIZE 256int main(void)
{int fd;struct termios tio;/* 1. 打开串口 */fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_SYNC);if (fd < 0) {perror("open");return 1;}/* 2. 配置串口:115200 8N1,无流控 */memset(&tio, 0, sizeof(tio));cfsetospeed(&tio, B115200);cfsetispeed(&tio, B115200);tio.c_cflag &= ~PARENB; /* 无校验 */tio.c_cflag &= ~CSTOPB; /* 1 停止位 */tio.c_cflag &= ~CSIZE;tio.c_cflag |= CS8; /* 8 数据位 */tio.c_cflag |= (CLOCAL | CREAD);tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* 原始模式 */tio.c_iflag &= ~(IXON | IXOFF | IXANY); /* 无软件流控 */tio.c_oflag &= ~OPOST; /* 原始输出 */tcflush(fd, TCIOFLUSH);tcsetattr(fd, TCSANOW, &tio);/* 3. 写入数据 */char *tx = "hello world\n";if (write(fd, tx, strlen(tx)) < 0) {perror("write");close(fd);return 1;}/* 4. 循环读取并查找 "flush" */char buf[BUF_SIZE];int len, total = 0;char line[BUF_SIZE * 2] = {0};while (1) {len = read(fd, buf, sizeof(buf));if (len < 0) {perror("read");break;}if (len == 0)continue;/* 把新数据追加到 line 缓冲区 */if (total + len < sizeof(line) - 1) {memcpy(line + total, buf, len);total += len;line[total] = '\0';}/* 判断是否出现 "flush" */if (strstr(line, "flush")) {printf("Received \"flush\", exit.\n");break;}/* 打印刚收到的数据(可选) */printf("Rx[%d]: %.*s", len, len, buf);fflush(stdout); //立即刷新缓冲区//把 C 标准库 为 `stdout`(通常是终端)维护的用户态缓冲区立即刷到内核,确保之前 `printf/puts` 的内容立刻出现在屏幕上}close(fd);return 0;
}
- pthread_create
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <pthread.h>#define SERIAL_PORT "/dev/ttyS0"
#define BUF_SIZE 256static int stop = 0; /* 线程退出标志 */
static pthread_t tid;/* 串口初始化:115200 8N1,阻塞读 */
static int open_serial(void)
{int fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY);if (fd < 0) {perror("open " SERIAL_PORT);return -1;}struct termios tio;tcgetattr(fd, &tio);cfmakeraw(&tio);cfsetispeed(&tio, B115200);cfsetospeed(&tio, B115200);tcflush(fd, TCIOFLUSH);tcsetattr(fd, TCSANOW, &tio);return fd;
}/* 线程函数:一直读串口直到看到 "flush" */
static void *rx_thread(void *arg)
{int fd = (long)arg;char buf[BUF_SIZE];char line[BUF_SIZE * 2] = {0};int total = 0;while (!stop) {int n = read(fd, buf, sizeof(buf));if (n <= 0) continue;/* 追加到行缓冲区并检查子串 */if (total + n < sizeof(line) - 1) {memcpy(line + total, buf, n);total += n;line[total] = '\0';}if (strstr(line, "flush")) {printf("[RX-THREAD] Found \"flush\", exiting.\n");break;}}close(fd);return NULL;
}int main(void)
{int fd = open_serial();if (fd < 0) return 1;/* 创建接收线程 */if (pthread_create(&tid, NULL, rx_thread, (void *)(long)fd) != 0) {perror("pthread_create");close(fd);return 1;}/* 主线程可以干别的事,这里简单等待用户输入 */printf("Press Enter to quit...\n");getchar();/* 通知线程退出并等待结束 */stop = 1;pthread_cancel(tid); /* 若线程阻塞在 read(),可强制唤醒 */pthread_join(tid, NULL);printf("Main thread exit.\n");return 0;
}
- poll
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <poll.h>#define SERIAL_PORT "/dev/ttyS0"
#define BUF_SIZE 256static int open_serial(void)
{int fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);if (fd < 0) { perror("open"); return -1; }struct termios tio;tcgetattr(fd, &tio);cfmakeraw(&tio);cfsetispeed(&tio, B115200);cfsetospeed(&tio, B115200);tcflush(fd, TCIOFLUSH);tcsetattr(fd, TCSANOW, &tio);return fd;
}int main(void)
{int fd = open_serial();if (fd < 0) return 1;char line[BUF_SIZE * 2] = {0};int total = 0;struct pollfd pfd = { .fd = fd, .events = POLLIN };while (1) {int ret = poll(&pfd, 1, -1); /* 无限等待可读 */if (ret < 0) { perror("poll"); break; }if (pfd.revents & POLLIN) {char buf[BUF_SIZE];int n = read(fd, buf, sizeof(buf));if (n <= 0) continue;/* 追加到行缓冲并检查 */if (total + n < sizeof(line) - 1) {memcpy(line + total, buf, n);total += n;line[total] = '\0';}if (strstr(line, "flush")) {printf("Got flush, exit.\n");break;}}}close(fd);return 0;
}
- epoll
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/epoll.h>#define SERIAL_PORT "/dev/ttyS0"
#define BUF_SIZE 256
#define MAX_EVENTS 1static int open_serial(void)
{int fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);if (fd < 0) { perror("open"); return -1; }struct termios tio;tcgetattr(fd, &tio);cfmakeraw(&tio);cfsetispeed(&tio, B115200);cfsetospeed(&tio, B115200);tcflush(fd, TCIOFLUSH);tcsetattr(fd, TCSANOW, &tio);return fd;
}int main(void)
{int fd = open_serial();if (fd < 0) return 1;int epfd = epoll_create1(0);if (epfd < 0) { perror("epoll_create1"); return 1; }struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd };if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {perror("epoll_ctl"); return 1;}char line[BUF_SIZE * 2] = {0};int total = 0;struct epoll_event events[MAX_EVENTS];while (1) {int nfds = epoll_wait(epfd, events, MAX_EVENTS, -1);if (nfds < 0) { perror("epoll_wait"); break; }for (int i = 0; i < nfds; ++i) {if (events[i].events & EPOLLIN) {char buf[BUF_SIZE];int n = read(fd, buf, sizeof(buf));if (n <= 0) continue;if (total + n < sizeof(line) - 1) {memcpy(line + total, buf, n);total += n;line[total] = '\0';}if (strstr(line, "flush")) {printf("Got flush, exit.\n");goto out;}}}}
out:close(fd);close(epfd);return 0;
}
深入浅出理解select、poll、epoll的实现
select | poll | epoll | |
---|---|---|---|
性能 | 随着连接数的增加,性能急剧下降,处理成千上万的并发连接数时,性能很差 | 随着连接数的增加,性能急剧下降,处理成千上万的并发连接数时,性能很差 | 随着连接数的增加,性能基本没有变化 |
连接数 | 一般1024 | 无限制 | 无限制 |
内存拷贝 | 每次调用select拷贝 | 每次调用poll拷贝 | fd首次调用epoll_ctl拷贝,每次调用epoll_wait不拷贝 |
数据结构 | bitmap | 数组 | 红黑树 |
内在处理机制 | 线性轮询 | 线性轮询 | FD挂在红黑树,通过事件回调callback |
时间复杂度 | O(n) | O(n) | O(1) |
目前支持I/O多路复用的系统调用有
select,pselect,poll,epoll
- 在Linux的缓存IO机制中,操作系统会将IO的数据缓存在文件系统的页缓存(page cache)。也就是说,数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓存区拷贝到应用程序的地址空间中
select
select
存在三个问题- 每次调用
select
,都需要把被监控的fds集合从用户态空间拷贝到内核态空间 - 能监听端口的数量有限,单个进程所能打开的最大连接数由
FD_SETSIZE
宏定义 - 被监控的
fds
集合中,只要有一个有数据可读,整个socket
集合就会被遍历一次调用sk
的poll
函数收集可读事件
- 每次调用
poll
- 针对select遗留的三个问题中(问题(2)是fd限制问题,问题(1)和(3)则是性能问题)
- poll只是使用pollfd结构而不是select的fd_set结构,这就解决了select的问题(2)fds集合大小1024限制问题
epoll
- 红黑树
二叉树
AVL树 - 平衡因子
红黑树
- 理解左旋右旋,实现插入
文档链接说明
-
参考47
(99+ 封私信 / 57 条消息) IO多路复用——深入浅出理解select、poll、epoll的实现 - 知乎 -
参考48
5分钟学二叉搜索树(手机动画版)_哔哩哔哩_bilibili
二叉搜索树的删除操作(手机动画版)_哔哩哔哩_bilibili -
参考49
红黑树的旋转操作_哔哩哔哩_bilibili
红黑树的插入算法_哔哩哔哩_bilibili