c-在一个只读fd上进行IO多路复用比简单地阻止读取有助于获得更好的性能吗



据我所知,我认为如果我只需要对一个fd执行读取操作,那么像select/poll这样的IO多路复用对性能没有帮助,它甚至会导致比阻塞读取fd更大的开销。

但是,经过linux审核的项目在一个读取套接字上使用了select。有人能解释一下它的含义吗?

审核的代码alse在1个socket/1管道和5个信号上使用libev。如果我只关心主链路套接字,那么使用阻塞读取是否更好?

我认为可能的情况可能是监听udp接收器插座等

为了方便起见,附上了代码块。提前感谢!

925 static int get_reply(int fd, struct audit_reply *rep, int seq)
926 {
927         int rc, i;
928         int timeout = 30; /* tenths of seconds */
929 
930         for (i = 0; i < timeout; i++) {
931                 struct timeval t;
932                 fd_set read_mask;
933 
934                 t.tv_sec  = 0;
935                 t.tv_usec = 100000; /* .1 second */
936                 FD_ZERO(&read_mask);
937                 FD_SET(fd, &read_mask);
938                 do {
939                         rc = select(fd+1, &read_mask, NULL, NULL, &t);
940                 } while (rc < 0 && errno == EINTR);
941                 rc = audit_get_reply(fd, rep, 
942                         GET_REPLY_NONBLOCKING, 0);
943                 if (rc > 0) {
944                         /* Don't make decisions based on wrong packet */
945                         if (rep->nlh->nlmsg_seq != seq)
946                                 continue;
947 
948                         /* If its not what we are expecting, keep looping */
949                         if (rep->type == AUDIT_SIGNAL_INFO)
950                                 return 1;
951 
952                         /* If we get done or error, break out */
953                         if (rep->type == NLMSG_DONE || rep->type == NLMSG_ERROR)
954                                 break;
955                 }
956         }
957         return -1;
958 }

性能可能会因平台而异(在本例中为Linux),但速度不应该更快。

代码使用select的主要原因似乎是它的超时能力。它允许带超时的读取,而无需实际修改套接字的超时(SO_SNDTIMEOSO_RCVTIMEO)[或者套接字是否支持超时]。

不,使用像select()poll()这样的I/O多路复用系统来侦听单个套接字上的数据,与该套接字上的连续阻塞读取相比,不会获得更好的性能。

最新更新