Linux select机制

一、poll和seclect机制的区别

  • select was introduced in BSD Unix, released in August 1983, whereas poll was introduced in SVR3 Unix, released in 1986.
  • Operations in poll and select is linear and slow because of having a lot of checks.
  • The main advantage of select is the fact that it is very portable – every UNIX-like OS has it. Some Unix systems do not support poll.
  • With select, the file descriptor sets are reconstructed on return, so each subsequent call must reinitialize them. The poll system call separates the input (events field) from the output (revents field), allowing the array to be reused without change.
  • The timeout parameter to select is undefined on return. So, we need to reinitialize it.
  • poll does not require the user to calculate the value of the highest-numbered file descriptor +1. Whereas in select, we have to calculate the nfds or pass the maximum number using FD_SETSIZE.
  • poll is more efficient for large-valued file descriptors. For example, when you want to wait for the events in the file descriptor (fd) 1000, poll will directly use that fdBut if you use this 1000 fd in select, it has to iterate (loop) from 0 to 999 to check any file has data.
  • In the select, we need to iterate over the file descriptors to check if it exists on the set returned from select. But in the poll, we can check only we wanted to.
  • In the poll, you can use the file descriptor which is more than 1024. But in select you cannot use it. If you use the file descriptor which is more than or equal to 1024 (FD_SETSIZE), then it may destroy your stack. How? select’s file descriptor sets are statically sized.

        用一句更通俗的话来说:select和poll机制驱动侧实现原理一致,都依赖于file_operation结构里面的poll函数实现。主要区别在于应用层的API不一样。 

二、seclect机制的应用层API

 int select( int nfds, fd_set *readfds,  fd_set *writefds,  fd_set *exceptfds, struct timeval *timeout );

Where,

nfds – This argument should be set to the highest-numbered file descriptor in any of the three sets, plus 1. The indicated file descrip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值