据我所知,如果我只需要在一个fd上执行读取操作,选择/轮询这样的IO多路复用将无助于性能的提高,它甚至会带来比仅仅读fd更多的开销。
但是,linux审计项目在单个读取套接字上使用select。有人能解释一下它的含义吗?
审核代码在1套接字/1管道和5个信号上使用libev。如果我只关心主要的netlink套接字,那么使用阻塞读取更好吗?
我认为可能的情况可能是监听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 }发布于 2016-04-29 16:00:44
根据平台(在本例中是Linux),性能可能是可变的,但它不应该更快。
代码使用select的主要原因似乎是它的超时能力。它允许读取与超时,而不实际修改套接字的超时(SO_SNDTIMEO,SO_RCVTIMEO),或者如果套接字甚至支持超时。
发布于 2016-04-29 14:54:15
不,通过使用I/O多路复用系统(如select()或poll() )侦听单个套接字上的数据,与该套接字上的连续阻塞读取相比,无法获得更好的性能。
https://stackoverflow.com/questions/36931522
复制相似问题