the syscall of select(2)

2009-05-13 07:58:24来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折


                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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:The tools to extract the BIOS from vmware

下一篇:FreeBSD环境下SSH的相关阅读笔记