1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/uio.h> #include <string.h> #include <errno.h> #include <sys/resource.h>
// 日志文件读取示例 void log_file_reader_example() { printf("=== 日志文件读取场景 ===\n"); // 创建模拟日志文件 const char *log_file = "application.log"; int fd = open(log_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd != -1) { const char *log_entries[] = { "2023-01-01 10:00:00 INFO Application started\n", "2023-01-01 10:00:01 DEBUG Loading configuration\n", "2023-01-01 10:00:02 WARN Low memory warning\n", "2023-01-01 10:00:03 ERROR Database connection failed\n", "2023-01-01 10:00:04 INFO Recovery attempt started\n" }; for (int i = 0; i < 5; i++) { write(fd, log_entries[i], strlen(log_entries[i])); } close(fd); } // 使用 pread 读取特定时间段的日志 fd = open(log_file, O_RDONLY); if (fd != -1) { char buffer[256]; printf("读取最后一条日志记录:\n"); // 从文件末尾附近读取 ssize_t bytes_read = pread(fd, buffer, sizeof(buffer) - 1, 150); if (bytes_read > 0) { buffer[bytes_read] = '\0'; printf(" %s", buffer); } close(fd); } unlink(log_file); }
// 数据库页读取示例 void database_page_reader_example() { printf("\n=== 数据库页读取场景 ===\n"); const char *db_file = "database_pages.dat"; int fd = open(db_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd != -1) { // 创建模拟的数据库页 char page_data[4096]; for (int page = 0; page < 10; page++) { snprintf(page_data, sizeof(page_data), "Page %d: Database page content with ID=%d and timestamp=%ld\n", page, page * 1000, time(NULL)); write(fd, page_data, strlen(page_data)); } close(fd); } // 使用 preadv 读取多个数据库页 fd = open(db_file, O_RDONLY); if (fd != -1) { char page1[1024], page2[1024], page3[1024]; struct iovec iov[3]; // 设置分散读取 iov[0].iov_base = page1; iov[0].iov_len = sizeof(page1) - 1; iov[1].iov_base = page2; iov[1].iov_len = sizeof(page2) - 1; iov[2].iov_base = page3; iov[2].iov_len = sizeof(page3) - 1; printf("使用 preadv 读取多个数据库页:\n"); ssize_t total_bytes = preadv(fd, iov, 3, 0); // 从开头读取 printf(" 总共读取: %zd 字节\n", total_bytes); if (total_bytes > 0) { page1[iov[0].iov_len] = '\0'; page2[iov[1].iov_len] = '\0'; page3[iov[2].iov_len] = '\0'; printf(" 页1: %.50s...\n", page1); printf(" 页2: %.50s...\n", page2); printf(" 页3: %.50s...\n", page3); } close(fd); } unlink(db_file); }
// 网络数据包处理示例 void network_packet_processor_example() { printf("\n=== 网络数据包处理场景 ===\n"); // 模拟网络数据包结构 struct packet_header { uint32_t magic; uint16_t version; uint16_t type; uint32_t length; uint32_t checksum; } __attribute__((packed)); struct packet_payload { char data[1024]; }; // 创建测试数据包文件 const char *packet_file = "network_packets.dat"; int fd = open(packet_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd != -1) { // 写入多个数据包 for (int i = 0; i < 3; i++) { struct packet_header header = { .magic = 0x12345678, .version = 1, .type = i, .length = 100, .checksum = 0xABCDEF00 + i }; char payload[1024]; snprintf(payload, sizeof(payload), "Packet %d payload data with timestamp %ld", i, time(NULL)); write(fd, &header, sizeof(header)); write(fd, payload, strlen(payload) + 1); } close(fd); } // 使用 preadv2 读取数据包(如果支持) fd = open(packet_file, O_RDONLY); if (fd != -1) { struct packet_header headers[3]; char payloads[3][256]; struct iovec iov[6]; // 3个头部 + 3个载荷 // 设置分散读取结构 for (int i = 0; i < 3; i++) { iov[i*2].iov_base = &headers[i]; iov[i*2].iov_len = sizeof(struct packet_header); iov[i*2+1].iov_base = payloads[i]; iov[i*2+1].iov_len = sizeof(payloads[i]) - 1; } printf("使用分散读取处理网络数据包:\n"); ssize_t bytes_read = preadv(fd, iov, 6, 0); printf(" 读取字节数: %zd\n", bytes_read); if (bytes_read > 0) { for (int i = 0; i < 3; i++) { printf(" 数据包 %d:\n", i); printf(" 魔数: 0x%08X\n", headers[i].magic); printf(" 版本: %d\n", headers[i].version); printf(" 类型: %d\n", headers[i].type); printf(" 长度: %d\n", headers[i].length); printf(" 校验: 0x%08X\n", headers[i].checksum); payloads[i][iov[i*2+1].iov_len] = '\0'; printf(" 载荷: %.50s...\n", payloads[i]); printf("\n"); } } close(fd); } unlink(packet_file); }
// 资源限制管理示例 void resource_limit_management_example() { printf("\n=== 资源限制管理场景 ===\n"); struct rlimit64 old_limit, new_limit; // 获取当前文件大小限制 if (prlimit64(0, RLIMIT_FSIZE, NULL, &old_limit) == 0) { printf("当前文件大小限制:\n"); if (old_limit.rlim_cur == RLIM64_INFINITY) { printf(" 软限制: 无限制\n"); } else { printf(" 软限制: %lld 字节 (%.2f GB)\n", (long long)old_limit.rlim_cur, (double)old_limit.rlim_cur / (1024 * 1024 * 1024)); } } // 获取打开文件数限制 if (prlimit64(0, RLIMIT_NOFILE, NULL, &old_limit) == 0) { printf("当前文件描述符限制:\n"); printf(" 软限制: %lld\n", (long long)old_limit.rlim_cur); printf(" 硬限制: %lld\n", (long long)old_limit.rlim_max); } // 获取内存限制 if (prlimit64(0, RLIMIT_AS, NULL, &old_limit) == 0) { printf("当前虚拟内存限制:\n"); if (old_limit.rlim_cur == RLIM64_INFINITY) { printf(" 软限制: 无限制\n"); } else { printf(" 软限制: %lld 字节 (%.2f GB)\n", (long long)old_limit.rlim_cur, (double)old_limit.rlim_cur / (1024 * 1024 * 1024)); } } printf("\n资源限制管理最佳实践:\n"); printf("1. 合理设置文件大小限制防止磁盘填满\n"); printf("2. 适当增加文件描述符限制支持高并发\n"); printf("3. 监控内存使用防止内存泄漏\n"); printf("4. 使用 prlimit64 动态调整资源限制\n"); }
int main() { printf("=== Linux I/O 系统调用应用场景演示 ===\n\n"); // 日志文件读取场景 log_file_reader_example(); // 数据库页读取场景 database_page_reader_example(); // 网络数据包处理场景 network_packet_processor_example(); // 资源限制管理场景 resource_limit_management_example(); printf("\n=== 总结 ===\n"); printf("I/O 系统调用选择指南:\n"); printf("\n"); printf("┌─────────────┬────────────────────────────────────┐\n"); printf("│ 场景 │ 推荐函数 │\n"); printf("├─────────────┼────────────────────────────────────┤\n"); printf("│ 简单读写 │ read/write │\n"); printf("│ 位置指定 │ pread/pwrite │\n"); printf("│ 多缓冲区 │ readv/writev │\n"); printf("│ 位置+多缓冲 │ preadv/pwritev │\n"); printf("│ 高级控制 │ preadv2/pwritev2 │\n"); printf("│ 资源限制 │ prlimit64 │\n"); printf("└─────────────┴────────────────────────────────────┘\n"); printf("\n"); printf("性能优化建议:\n"); printf("1. 批量操作减少系统调用次数\n"); printf("2. 合理选择缓冲区大小\n"); printf("3. 使用位置指定避免文件位置移动\n"); printf("4. 分散/聚集 I/O 减少内存拷贝\n"); printf("5. 合理设置资源限制防止系统过载\n"); return 0; }
|