-
Notifications
You must be signed in to change notification settings - Fork 0
/
20190413-1.html
706 lines (422 loc) · 43.3 KB
/
20190413-1.html
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<!-- 元数据 -->
<meta charset="utf-8">
<link rel="icon" href="/images/favicon.ico">
<title>IO多路复用之select,poll,epoll总结 | 云海鹰影</title>
<meta name="author" content="云海鹰影" />
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="robots" content="index,follow" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="format-detection" content="telphone=no, email=no" />
<meta name="keywords" content="IO多路复用, epoll, poll, selct, 编程基础, 网络编程" />
<!-- 站点验证相关 -->
<meta name="baidu-site-verification" content="code-EjIoHOEm6V" />
<link rel="alternate" href="atom.xml" type="application/atom+xml">
<!-- 样式表文件 -->
<link rel="stylesheet" id="kratos-css" href="/css/kratosr.min.css" type="text/css" media="all">
<link rel="stylesheet" id="highlight-css" href="/css/highlight.min.css" type="text/css" media="all">
<link rel="stylesheet" id="fontawe-css" href="https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css" type="text/css" media="all">
<link rel="stylesheet" id="nprogress-css" href="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.min.css" type="text/css" media="all">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/[email protected]/dist/jquery.fancybox.min.css">
<link rel="stylesheet" id="darkmode-css" href="/css/kr-dark.min.css" type="text/css" media="all">
<!-- 不得不预先加载的一些JS文件 -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/qrcode.min.js"></script>
<meta name="generator" content="Hexo 5.4.2"></head>
<body class="custom-background">
<div id="kratos-wrapper">
<div id="kratos-page">
<div id="kratos-header">
<div class="nav-toggle"><a class="kratos-nav-toggle js-kratos-nav-toggle"><i></i></a></div>
<header id="kratos-header-section">
<div class="container">
<div class="nav-header">
<div class="color-logo"><a href="/">云海鹰影</a></div>
<nav id="kratos-menu-wrap">
<ul id="kratos-primary-menu" class="sf-menu">
<li><a href="/" ><i class="fa fa-home"></i>首页</a></li>
<li><a href="/tags/" ><i class="fa fa-file"></i>标签云</a></li>
<li><a href="/archives/" ><i class="fa fa-file"></i>档案馆</a></li>
<li><a href="/friends/" ><i class="fa fa-paw"></i>友链</a></li>
<li>
<a ><i class="fa fa-umbrella"></i>秘密小窝</a>
<ul class="sub-menu">
<li><a target="_blank" rel="noopener" href="https://smp.chaoswork.cn" >鹰の巢</a></li>
</ul>
</li>
<li>
<a ><i class="fa fa-random"></i>资源库</a>
<ul class="sub-menu">
<li><a href="/resources/frontend" >前端</a></li>
<li><a href="/resources/backend" >后端</a></li>
<li><a href="/resources/tools" >工具</a></li>
</ul>
</li>
<li>
<a ><i class="fa fa-gift"></i>看动漫</a>
<ul class="sub-menu">
<li><a target="_blank" rel="noopener" href="https://www.5dm.tv/" >五弹幕</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
</div>
<div class="kratos-start kratos-hero-2">
<!-- <div class="kratos-overlay"></div> -->
<div class="kratos-cover kratos-cover-2 text-center">
<div class="desc desc2 animate-box">
<a href="/">
<h2>云海鹰影</h2> <br />
<span>我独自度过的无数个晨昏,用来交换你身边一瞬</span>
</a>
</div>
</div>
</div>
<div id="kratos-blog-post">
<div class="container">
<div class="row">
<div id="main">
<section class="col-md-8">
<article>
<div class="kratos-hentry kratos-post-inner clearfix">
<header class="kratos-entry-header">
<h1 class="kratos-entry-title text-center">IO多路复用之select,poll,epoll总结</h1>
<div class="kratos-post-meta text-center">
<span>
<i class="fa fa-calendar"></i> 2019-04-13
<i class="fa fa-folder"></i> 分类 <a class="label-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/">编程基础</a>, <a class="label-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a>
<i class="fa fa-user"></i> 作者 云海鹰影
<i class="fa fa-edit"></i>
~4.46K
字
</span>
</div>
</header>
<div class="kratos-post-content">
<div id="expire-alert" class="alert alert-warning hidden" role="alert">
本文最后编辑于 <time datetime="1615457057383"></time> 前,其中的内容可能需要更新。
</div>
<hr />
<p>前篇: <a href="/20190408-1.html">网络编程之IO模型和IO多路复用</a></p>
<p>select, poll, epoll 在历史上是先后按顺序出现的,后者的提出都是为了解决前者遗留的问题。select是POSIX早期提出的规范,windows和linux等各大标准库都有实现。poll和epoll都是linux上的实现,其中poll和select差别很小, 在高并发和性能上有很大局限,而epoll则解决了大部分问题。</p>
<h2 id="select"><a href="#select" class="headerlink" title="select"></a>select</h2><p>select原型函数:</p>
<figure class="highlight cpp" data-lang="cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * maxfdp1: 最大的fd加1 (fd是一个整形)</span></span><br><span class="line"><span class="comment"> * fd_set: 文件描述符fd的集合,通过FD_ZERO,FD_SET,FD_CLR,FD_ISSET等宏来控制</span></span><br><span class="line"><span class="comment"> * return: int 若有就绪描述符返回其数目,若超时则为0,若出错则为-1</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">select</span> <span class="params">(<span class="type">int</span> maxfdp1, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, <span class="keyword">struct</span> timeval *timeout)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/////////////////////////////////////////////////</span></span><br><span class="line"><span class="comment">//相关接口:</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">FD_ZERO</span> <span class="params">(fd_set *fdset)</span></span>; <span class="comment">// clear all bits in fdset</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">FD_SET</span> <span class="params">(<span class="type">int</span> fd,fd_set *fdset)</span></span>; <span class="comment">// turn on the bit for fd in fdset</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">FD_CLR</span> <span class="params">(<span class="type">int</span> fd,fd_set *fdset)</span></span>; <span class="comment">// turn off the bit for fd in fdset</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">FD_ISSET</span><span class="params">(<span class="type">int</span> fd,fd_set *fdset)</span></span>; <span class="comment">// is the bit for fd on in fdset</span></span><br></pre></td></tr></table></figure>
<p>select存在以下限制:</p>
<blockquote>
<p>1 调用select函数会阻塞进程,直到有描述副就绪,或者超时<br>2 单个进程能够监视的文件描述符的数量存在最大限制,通常是1024;想要修改限制只能通过修改内核宏定义;<br>3 由于select采用轮询的方式扫描文件描述符,文件描述符数量越多,性能越差;<br>4 每次调用select,都需要把fd_set集合从用户态拷贝到内核态,如果fd_set集合很大,这个开销也很大;<br>5 select返回的是含有整个fd集合,应用程序需要遍历整个数组才能发现哪些句柄发生了事件;<br>6 select的触发方式是水平触发,应用程序如果没有完成对一个已经就绪的文件描述符进行IO操作,那么之后每次select调用还是会将这些文件描述符通知进程。</p>
</blockquote>
<h2 id="poll"><a href="#poll" class="headerlink" title="poll"></a>poll</h2><p>poll 和 select 实现机制相似,只是内核用结构链表取代固定数组保存文件描述符集合,也就没有了最大文件描述数量的限制;但是select面临的其他问题,poll上依然存在。</p>
<p>poll原型函数:</p>
<figure class="highlight cpp" data-lang="cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * fds: pollfd数组,文件描述及其监听事件的结构体集合</span></span><br><span class="line"><span class="comment"> * nfds: pollfd数组的大小</span></span><br><span class="line"><span class="comment"> * timeout: 超时时间,-1表示永久阻塞,0表示立即返回</span></span><br><span class="line"><span class="comment"> * return: int 若有就绪描述符返回其数目,若超时则为0,若出错则为-1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">poll</span> <span class="params">(<span class="keyword">struct</span> pollfd *fds, <span class="type">unsigned</span> <span class="type">int</span> nfds, <span class="type">int</span> timeout)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/////////////////////////////////////////////////</span></span><br><span class="line"><span class="comment">//相关结构:</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">pollfd</span> {</span><br><span class="line"> <span class="type">int</span> fd; <span class="comment">/* file descriptor */</span></span><br><span class="line"> <span class="type">short</span> events; <span class="comment">/* requested events to watch */</span></span><br><span class="line"> <span class="type">short</span> revents; <span class="comment">/* returned events witnessed */</span></span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>可以看到,poll对select参数传值(readfds, writefds)稍有改进,pollfd结构包含了要监视的event和发生的event。和select函数一样,poll返回后,依然需要轮询pollfd数组来获取就绪的描述符。</p>
<h2 id="epoll"><a href="#epoll" class="headerlink" title="epoll"></a>epoll</h2><p>鉴于select和poll的缺陷,epoll采用了完全不同的事件驱动机制,上述select和poll存在的问题在epoll上不复存在。<br>epoll在Linux内核中建立一个简易的文件系统(文件系统一般用什么数据结构实现?B+树),用来管理epoll对象。epoll对象封装了对于网络IO的所有操作,这样用户对IO的操作就转变为对epoll对象的操作。</p>
<p>epoll对用户提供三个调用接口:</p>
<figure class="highlight cpp" data-lang="cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 创建一个epoll对象</span></span><br><span class="line"><span class="comment"> * size: 监听的描述符数量</span></span><br><span class="line"><span class="comment"> * return: epoll句柄描述符 or -1 if failed</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">epoll_create</span><span class="params">(<span class="type">int</span> size)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 向epoll对象添加/删除/修改fd描述符,以及要监听的事件类型</span></span><br><span class="line"><span class="comment"> * epfd: epoll句柄描述符</span></span><br><span class="line"><span class="comment"> * op: fd操作类型: EPOLL_CTL_ADD, EPOLL_CTL_MOD, EPOLL_CTL_DEL</span></span><br><span class="line"><span class="comment"> * fd: 操作的fd描述符</span></span><br><span class="line"><span class="comment"> * event: 注册/取消监听的事件</span></span><br><span class="line"><span class="comment"> * return: 0 if success or -1 if failed</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">epoll_ctl</span><span class="params">(<span class="type">int</span> epfd, <span class="type">int</span> op, <span class="type">int</span> fd, <span class="keyword">struct</span> epoll_event *event)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 等待事件的就绪</span></span><br><span class="line"><span class="comment"> * epfd: epoll句柄描述符</span></span><br><span class="line"><span class="comment"> * events: 接收事件的数组</span></span><br><span class="line"><span class="comment"> * maxevents: 事件数组的大小</span></span><br><span class="line"><span class="comment"> * fd: 操作的fd描述符</span></span><br><span class="line"><span class="comment"> * timeout: 超时时间,-1表示永久阻塞,0表示立即返回</span></span><br><span class="line"><span class="comment"> * return: 就绪fd的数量 or -1 if failed</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">epoll_wait</span><span class="params">(<span class="type">int</span> epfd, <span class="keyword">struct</span> epoll_event * events, <span class="type">int</span> maxevents, <span class="type">int</span> timeout)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/////////////////////////////////////////////////</span></span><br><span class="line"><span class="comment">// 相关结构</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">epoll_event</span> {</span><br><span class="line"> <span class="type">__uint32_t</span> events; <span class="comment">/* Epoll events */</span></span><br><span class="line"> <span class="type">epoll_data_t</span> data; <span class="comment">/* User data variable */</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">union</span> <span class="title class_">epoll_data</span> {</span><br><span class="line"> <span class="type">void</span> *ptr;</span><br><span class="line"> <span class="type">int</span> fd;</span><br><span class="line"> <span class="type">__uint32_t</span> u32;</span><br><span class="line"> <span class="type">__uint64_t</span> u64;</span><br><span class="line">} <span class="type">epoll_data_t</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//events可以是以下几个宏的集合:</span></span><br><span class="line">EPOLLIN :表示对应的文件描述符可以读(包括对端SOCKET正常关闭);</span><br><span class="line">EPOLLOUT:表示对应的文件描述符可以写;</span><br><span class="line">EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);</span><br><span class="line">EPOLLERR:表示对应的文件描述符发生错误;</span><br><span class="line">EPOLLHUP:表示对应的文件描述符被挂断;</span><br><span class="line">EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。</span><br><span class="line">EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把这个socket加入到EPOLL队列里</span><br></pre></td></tr></table></figure>
<p>简单来说,epoll的工作机制是这样的:</p>
<ul>
<li>当调用epoll_create时,内核会创建一个epoll对象,epoll对象通过红黑树管理监听事件,通过双向链表存放发生的事件。</li>
<li>用户通过调用epoll_ctl向epoll对象注册/销毁监听事件;对于每一个添加到epoll对象中的事件,epoll都会向设备驱动程序注册回调(ep_poll_callback)。当驱动程序有事件发生时会调用该回调函数,它会将发生的事件添加到双向链表中。</li>
<li>当用户调用epoll_wait时,内核只需要检查epoll对象中的双向链表是否有事件。如果事件不为空,则把发生的事件复制到用户态,同时将事件数量返回给用户。(注意,epoll_wait依然可能造成阻塞。)</li>
</ul>
<p>由此可见,<strong>epoll通过红黑树,双向链表等数据结构,以及事件回调机制,保证了其高效和应对并发的能力。</strong></p>
<p>另外,epoll除了提供select/poll那种IO事件的水平触发(Level Triggered)外,还提供了边缘触发(Edge Triggered),这就使得用户空间程序有可能缓存IO状态,减少epoll_wait/epoll_pwait的调用,提高应用程序效率。</p>
<ul>
<li><p>水平触发(LT):默认工作模式,即当epoll_wait检测到某描述符事件就绪并通知应用程序时,应用程序可以不立即处理该事件;下次调用epoll_wait时,会再次通知此事件。</p>
</li>
<li><p>边缘触发(ET): 当epoll_wait检测到某描述符事件就绪并通知应用程序时,应用程序必须立即处理该事件。如果不处理,下次调用epoll_wait时,不会再次通知此事件。(直到你做了某些操作导致该描述符变成未就绪状态了,也就是说边缘触发只在状态由未就绪变为就绪时只通知一次)。</p>
</li>
</ul>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>可以用以下表格对比来看select, poll, epoll的区别:</p>
<table>
<thead>
<tr>
<th align="left"></th>
<th align="center">select</th>
<th align="center">poll</th>
<th align="center">epoll</th>
</tr>
</thead>
<tbody><tr>
<td align="left">操作方式</td>
<td align="center">遍历</td>
<td align="center">遍历</td>
<td align="center">回调</td>
</tr>
<tr>
<td align="left">底层实现</td>
<td align="center">数组</td>
<td align="center">链表</td>
<td align="center">红黑树</td>
</tr>
<tr>
<td align="left">IO效率</td>
<td align="center">每次调用都进行线性遍历,时间复杂度为O(n)</td>
<td align="center">每次调用都进行线性遍历,时间复杂度为O(n)</td>
<td align="center">事件通知方式,每当fd就绪,系统注册的回调函数就会被调用,将就绪fd放到readyList里面,时间复杂度O(1)</td>
</tr>
<tr>
<td align="left">最大连接数</td>
<td align="center">1024(x86)或2048(x64)</td>
<td align="center">无上限</td>
<td align="center">无上限</td>
</tr>
<tr>
<td align="left">fd拷贝</td>
<td align="center">每次调用select,都需要把fd集合从用户态拷贝到内核态</td>
<td align="center">每次调用poll,都需要把fd集合从用户态拷贝到内核态</td>
<td align="center">调用epoll_ctl时拷贝进内核并保存,之后每次epoll_wait不拷贝</td>
</tr>
</tbody></table>
<p>epoll是Linux目前大规模网络并发程序开发的首选模型。在绝大多数情况下性能远超select和poll。目前流行的高性能web服务器Nginx正式依赖于epoll提供的高效服务。但是,在并发连接不高的情况下,多线程+阻塞I/O方式可能性能更好。</p>
</div>
<div class="kratos-copyright text-center clearfix">
<h5>本作品采用 <a rel="license nofollow" target="_blank" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
</div>
<footer class="kratos-entry-footer clearfix">
<div class="post-like-donate text-center clearfix" id="post-like-donate">
<a class="share" href="javascript:;"><i class="fa fa-share-alt"></i> 分享</a>
<div class="share-wrap" style="display: none;">
<div class="share-group">
<a href="javascript:;" class="share-plain qq" onclick="share('qq');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-qq"></i>
</div>
</a>
<a href="javascript:;" class="share-plain qzone" onclick="share('qzone');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-star"></i>
</div>
</a>
<a href="javascript:;" class="share-plain weixin pop style-plain" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-weixin"></i>
</div>
<div class="share-int">
<div class="qrcode" id="wechat-qr"></div>
<p>打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮</p>
</div>
</a>
<a href="javascript:;" class="share-plain weibo" onclick="share('weibo');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-weibo"></i>
</div>
</a>
<a href="javascript:;" class="share-plain facebook style-plain" onclick="share('facebook');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-facebook"></i>
</div>
</a>
<a href="javascript:;" class="share-plain twitter style-plain" onclick="share('twitter');" rel="nofollow">
<div class="icon-wrap">
<i class="fa fa-twitter"></i>
</div>
</a>
</div>
<script type="text/javascript">
$(()=>{
new QRCode("wechat-qr", {
text: "https://www.chaoswork.cn/20190413-1.html",
width: 150,
height: 150,
correctLevel : QRCode.CorrectLevel.H
});
});
function share(dest) {
const qqBase = "https://connect.qq.com/widget/shareqq/index.html?";
const weiboBase = "https://service.weibo.com/share/share.php?";
const qzoneBase = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
const facebookBase = "https://www.facebook.com/sharer/sharer.php?";
const twitterBase = "https://twitter.com/intent/tweet?";
const hostUrl = "https://www.chaoswork.cn/20190413-1.html";
const title = "「IO多路复用之select,poll,epoll总结」";
const excerpt = `前篇: 网络编程之IO模型和IO多路复用
select, poll, epoll 在历史上是先后按顺序出现的,后者的提出都是为了解决前者遗留的问题。select是POSIX早期提出的规范,windows和linux等各大标准库都有实现...`;
let _URL;
switch (dest) {
case "qq" : _URL = qqBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy"; break;
case "weibo" : _URL = weiboBase+"url="+hostUrl+"&title="+title+excerpt; break;
case "qzone" : _URL = qzoneBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy"; break;
case "facebook" : _URL = facebookBase+"u="+hostUrl; break;
case "twitter" : _URL = twitterBase+"text="+title+excerpt+"&url="+hostUrl; break;
}
window.open(_URL);
};
</script>
</div>
</div>
<div class="footer-tag clearfix">
<div class="pull-left">
<i class="fa fa-tags"></i>
<a class="tag-none-link" href="/tags/IO%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8/" rel="tag">IO多路复用</a>, <a class="tag-none-link" href="/tags/epoll/" rel="tag">epoll</a>, <a class="tag-none-link" href="/tags/poll/" rel="tag">poll</a>, <a class="tag-none-link" href="/tags/selct/" rel="tag">selct</a>, <a class="tag-none-link" href="/tags/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/" rel="tag">编程基础</a>, <a class="tag-none-link" href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" rel="tag">网络编程</a>
</div>
<div class="pull-date">
<span>最后编辑:2021-03-11</span>
</div>
</div>
</footer>
</div>
<nav class="navigation post-navigation clearfix" role="navigation">
<div class="nav-previous clearfix">
<a title=" 网络编程之IO模型和IO多路复用" href="/20190408-1.html">< 上一篇</a>
</div>
<div class="nav-next clearfix">
<a title=" 开源镜像站整理(2019-04-21)" href="/20190421-1.html">下一篇 ></a>
</div>
</nav>
<link rel="stylesheet" href="https://cdn.bootcss.com/gitalk/1.4.1/gitalk.min.css">
<div id="gitalk-container"></div>
<script type="text/javascript">
var load_comm = () => {
const init = () => {
const gitalk = new Gitalk({
// Gitalk配置
language: "zh-CN",
clientID: "7dff06239ac01f6006c8",
clientSecret: "1cf779fc33e9e0d353f196eefa4bb43ecd560889",
repo: "comment_data",
owner: "stayknight",
admin: ["stayknight"],
id: location.pathname,
distractionFreeMode: false
});
gitalk.render('gitalk-container');
};
if (typeof Gitalk == 'undefined') {
const src = 'https://cdn.bootcss.com/gitalk/1.4.1/gitalk.min.js';
$.getScript(src, init);
} else {
init();
}
};
</script>
</article>
</section>
</div>
<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
<aside id="krw-about" class="widget widget-kratos-about clearfix">
<div class="photo-background"></div>
<div class="photo-wrapper clearfix">
<div class="photo-wrapper-tip text-center">
<img class="about-photo" src="https://cdn.jsdelivr.net/npm/[email protected]/avatars/icon41.jpg" />
</div>
</div>
<div class="textwidget">
<p class="text-center">路漫漫其修远兮,吾将上下而求索~<br/><span>(右下角搜索)</span></p>
</div>
</aside>
<!-- Moved to about.ejs -->
<aside id="krw-categories" class="widget widget-categories clearfix">
<h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
<ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/ACGN/">ACGN</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/">前端开发</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">游戏开发</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/%E6%9C%8D%E5%8A%A1%E5%99%A8/">服务器</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/">生活学习</a><span class="category-list-count">8</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/%E6%9D%82%E8%B0%88/">杂谈</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/%E8%AF%BB%E4%B9%A6/">读书</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%94%9F%E6%B4%BB%E5%AD%A6%E4%B9%A0/%E9%9F%B3%E4%B9%90/">音乐</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BB%8F%E9%AA%8C%E6%95%99%E7%A8%8B/">经验教程</a><span class="category-list-count">16</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BB%8F%E9%AA%8C%E6%95%99%E7%A8%8B/%E5%8D%9A%E5%AE%A2/">博客</a><span class="category-list-count">5</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/">编程基础</a><span class="category-list-count">39</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/C-%E5%9F%BA%E7%A1%80/">C++基础</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/linux%E5%9F%BA%E7%A1%80/">linux基础</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/">数据结构与算法</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B/">软件工程</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E6%BA%90%E5%88%86%E4%BA%AB/">资源分享</a><span class="category-list-count">2</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E6%BA%90%E5%88%86%E4%BA%AB/%E5%BC%80%E5%8F%91%E8%B5%84%E6%BA%90/">开发资源</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E6%BA%90%E5%88%86%E4%BA%AB/%E6%BA%90%E7%A0%81/">源码</a><span class="category-list-count">1</span></li></ul></li></ul>
</aside>
<aside id="krw-tags" class="widget widget-kratos-tags clearfix">
<h4 class="widget-title"><i class="fa fa-tags"></i>标签云</h4>
<div class="tag-clouds">
<a href="/tags/ACGN/" style="font-size: 0.75em;">ACGN</a> <a href="/tags/ACM/" style="font-size: 0.6em;">ACM</a> <a href="/tags/AVL%E6%A0%91/" style="font-size: 0.6em;">AVL树</a> <a href="/tags/C/" style="font-size: 0.6em;">C++</a> <a href="/tags/C-%E5%9F%BA%E7%A1%80/" style="font-size: 0.8em;">C++基础</a> <a href="/tags/C-%E7%B1%BB%E5%9E%8B/" style="font-size: 0.65em;">C++类型</a> <a href="/tags/IO%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8/" style="font-size: 0.65em;">IO多路复用</a> <a href="/tags/IO%E6%A8%A1%E5%9E%8B/" style="font-size: 0.6em;">IO模型</a> <a href="/tags/MYSQL/" style="font-size: 0.65em;">MYSQL</a> <a href="/tags/SAO/" style="font-size: 0.6em;">SAO</a> <a href="/tags/adb/" style="font-size: 0.6em;">adb</a> <a href="/tags/anacron/" style="font-size: 0.6em;">anacron</a> <a href="/tags/centos/" style="font-size: 0.7em;">centos</a> <a href="/tags/cms/" style="font-size: 0.6em;">cms</a> <a href="/tags/cron/" style="font-size: 0.6em;">cron</a> <a href="/tags/crontab/" style="font-size: 0.6em;">crontab</a> <a href="/tags/epoll/" style="font-size: 0.6em;">epoll</a> <a href="/tags/gcc/" style="font-size: 0.6em;">gcc</a>
</div>
</aside>
<aside id="krw-posts" class="widget widget-kratos-poststab">
<h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
<div class="tab-content">
<ul class="list-group">
<a class="list-group-item" href="/20230825-1.html"><i class="fa fa-book"></i> 工作中遇到的一些数据库问题记录(随时更新)</a>
<a class="list-group-item" href="/20230508-1.html"><i class="fa fa-book"></i> C++开源游戏服务器或脚手架记录</a>
<a class="list-group-item" href="/20230302-1.html"><i class="fa fa-book"></i> ubuntu scp命令的一个问题</a>
<a class="list-group-item" href="/20230227-1.html"><i class="fa fa-book"></i> linux僵尸进程和孤儿进程一点记录</a>
<a class="list-group-item" href="/20230224-1.html"><i class="fa fa-book"></i> 关于带返回值的函数未返回引起的Undefined Behavior问题</a>
</ul>
</div>
</aside>
</section>
</div>
</div>
</div>
<footer>
<div id="footer">
<div class="kr-tool text-center">
<div class="tool">
<div class="box search-box">
<a href="/search/">
<span class="fa fa-search"></span>
</a>
</div>
<div class="box theme-box" id="darkmode-switch">
<span class="fa fa-adjust"></span>
</div>
</div>
<div class="box gotop-box">
<span class="fa fa-chevron-up"></span>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 footer-list text-center">
<ul class="kratos-social-icons text-center">
<li><a href="mailto:[email protected]"><i class="fa fa-envelope"></i></a></li>
<li><a target="_blank" rel="me" href="https://smp.chaoswork.cn/@stayknight"><i class="fa fa fa-share-alt-square"></i></a></li>
<li><a target="_blank" rel="nofollow" href="https://github.com/stayknight"><i class="fa fa-github"></i></a></li>
<li><a target="_blank" rel="nofollow" href="/atom.xml"><i class="fa fa-rss"></i></a></li>
</ul>
<ul class="kratos-copyright">
<div>
<li>© 2023 云海鹰影 版权所有.</li>
<li>本站已运行<span id="span_dt">Loading...</span></li>
</div>
<div>
<li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
<li>Made with <i class="fa fa-heart throb" style="color:#d43f57"></i> by <a href="https://candinya.com" target="_blank" rel="nofollow">Candinya</a>.</li>
</div>
<div>
<li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
<li>Hosted on <a href="https://github.io" target="_blank">Github Pages</a></li>
</div>
<div>
<li><a href="https://beian.miit.gov.cn" rel="external nofollow" target="_blank">鄂ICP备17025858号-5</a></li>
<li><a href="http://www.beian.gov.cn" rel="external nofollow" target="_blank"><img src="/images/psr.webp" width="12" height="12"> xxxxx</a></li>
<li><a href="sitemap.xml" target="_blank">站点地图</a></li>
<li><a href="baidusitemap.xml" target="_blank">百度地图</a></li>
</div>
</ul>
</div>
</div>
</div>
</div>
</footer>
</div>
</div>
<script type="text/javascript" charset="utf-8">
$("img.headicon").one("error", function(e) {
$(this).attr("src", "/images/avatar.webp");
});
</script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.min.js"></script>
<script>const notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));</script>
<div>
<canvas id="snow"></canvas>
<script async type="text/javascript" src="/js/snow.min.js"></script>
</div>
<script async src="/js/candy.min.js"></script>
<script defer src=""></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js"></script>
<script defer src="/js/kratosr.min.js"></script>
<script defer src="/js/pjax.min.js"></script>
<script defer src="/js/kr-dark.min.js"></script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?bfb917536df0b5a503a5690e6de9ef9c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>