免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2013 | 回复: 0
打印 上一主题 下一主题

the syscall of select(2) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-02 19:03 |只看该作者 |倒序浏览

                I wanna outline the process of select(2) syscall on a modern SMP Operating System ( FreeBSD amd64box 6.2-RELEASE ). This is just my note, and it is a pleasure for me if anyone found it useful. Let's read the source.
1. the syscall entry(select) copys in the timeval argument from user space and calls kern_select to do the real work.
2. kern_select copys in fd_in/fd_out/fd_ex arguments from user space(if needed) according to the nd param.
3. sets the TDF_SELECT thread flag and calls selscan to scan all the file descriptors.
4. selscan calls every file dependent poll function to test if it is ready (for read / write / exception), and record any file that is ready. A file that is not ready typically calls selrecord to record the thread which selects on it. selrecord simply records a thread with selinfo.
5. after the scan, kern_select decides to scan again or not according to thread flags and nselcoll. If it is not needed to scan again and no file is ready, kern_select will conditionally wait on selwait. otherwise kern_select will goto 7 to finish the syscall.
6. being wakeuped from selwait, kern_select gotos 3.
7. the main parts of the syscall is finished, sets error properly and copys out the result.
8. A file may call selwakeup to wakeup any threads that selected on it when it is ready. selwakeup will wakeup the thread conditionally waited on selwait.
There are several somewhat important locks and flags used during the syscall.
TDF_SELECT
    It is set before the real start of select(2) and cleared at the end. kern_select will ensure that this flag is set before waiting on selwait, so selwakeup uses this flag to tell threads (that has been recorded with selinfo) not to wait on selwait but rescan the file descriptors.
nselcoll
    As there is only one thread pointer (si_thread) in a selinfo, only one thread can be recorded with a selinfo. So there will be collisions if two (or more) threads try to select on one selinfo and we have no way to know which threads should be waken up (and/or tell them not to wait on selwait but rescan the file descriptors) when the selinfo should be selwakeuped. nselcoll is used for this reason. Before scanning file descriptors, threads remember the value of nselcoll and recheck its value after the scan, if it is different, they will do a rescan. nselcoll is changed by selwakeup if it finds a wakeup requested from a selinfo that has collisions.
sellock
    This is the main lock of the select(2) implementation. It protects the TDF_SELECT thread flag and nselcoll and some other datastructures, and it is also used for selwait.
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10543/showart_290695.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP