ssize_t naive_writev (int fd, const struct iovec *iov, int count)
{
ssize_t ret = 0;
int i;
for (i = 0; i < count; i++) {
ssize_t nr;
errno = 0;
nr = write (fd, iov[i].iov_base, iov[i].iov_len);
if (nr ==
−
1) {
if (errno == EINTR)
continue;
ret =
−
1;
break;
}
ret += nr;
}
return ret;
}
幸好,這不是Linux 的實作方式:Linux把
readv()
與
writev()
實作成系統呼叫,並在
內部進行分散∕聚集I/O。事實上,Linux 核心內部所有I/O均採用向量的方式;儘管
read()
和
write()
被實作成向量I/O,但是向量中只具有一個區段。
事件輪詢
察覺
poll()
與
select()
的限制後,Linux 核心2.6版
2
引進了
事件輪詢
(
event poll
,簡稱
epoll
)措施。儘管比
poll()
與
select()
還複雜,epoll 不僅解決了這兩個介面的效能問 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.