关于epoll和拼接的问题 [英] Question about epoll and splice

查看:49
本文介绍了关于epoll和拼接的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将通过网络发送大量数据,因此我决定(因为我使用的是Linux)使用epoll和splice.这是我的看到方式(伪代码):

My application is going to send huge amount of data over network, so I decided (because I'm using Linux) to use epoll and splice. Here's how I see it (pseudocode):

epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event
while(1)
{
    epoll_wait (tmp_structure);

    if (tmp_structure->fd == file_descriptor)
    {
        epoll_ctl (file_fd, EPOLL_CTL_DEL); 
        epoll_ctl (tcp_socket_fd, EPOLL_CTL_ADD); // wait for EPOLLOUT event
    }

    if (tmp_structure->fd == tcp_socket_descriptor)
    {
        splice (file_fd, tcp_socket_fd);
        epoll_ctl (tcp_socket_fd, EPOLL_CTL_DEL); 
        epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event
    }
}

我假设我的应用程序最多可以打开2000个TCP套接字.我想问你两件事:

I assume, that my application will open up to 2000 TCP sockets. I want o ask you about two things:

  1. 会有很多epoll_ctl调用,当我有这么多套接字时,会不会很慢?
  2. 文件描述符必须首先变得可读,并且套接字将要写入之前要间隔一段时间.我可以确定的是,在套接字变为可写文件描述符的那一刻,它仍然是可读的(以避免阻塞调用)?

推荐答案

第一个问题

  1. 您可以使用边缘触发而不是触发轮询,因此不必每次都删除套接字.
  2. 您可以使用EPOLLONESHOT防止卸下插座

首先必须使文件描述符可读,并且套接字要写入之前要间隔一段时间.

File descriptor has to become readable first and there will be some interval before socket will become writable.

什么样的文件描述符?如果此文件在文件系统上不能使用选择/轮询或其他工具来实现,则无论磁盘和缓存处于什么状态,该文件始终是可读或可写的.如果您需要异步工作人员,则可以使用 aio _ * API,但通常只是从文件读取到文件写入,并假定它是非阻塞的.

What kind of file descriptor? If this file on file system you can't use select/poll or other tools for this purpose, file will be always readable or writeable regardless the state if disk and cache. If you need to do staff asynchronous you may use aio_* API but generally just read from file write to file and assume it is non-blocking.

如果它是TCP套接字,那么大多数时候它是可写的.最好用无阻塞调用,并在收到EWOULDBLOCK时将套接字放入epoll.

If it is TCP socket then it would be writeable most of the time. It is better to use non-blocking calls and put sockets to epoll when you get EWOULDBLOCK.

这篇关于关于epoll和拼接的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆