Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

c - What is the nfds from select() used for

I was wondering what the nfds does, after reading different manuals, I end up with the only answer being it is the highest numbered file descriptor plus one. What is it exactly used for?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you're using select(), you are trying to check the status of a set of file descriptors. The possible range of file descriptors you're interested in ranges from a low of 0 (standard input) to some maximum value (the highest file descriptor you have open that you're interested in checking the status of). You have to tell select() how big the list of file descriptors is because the total number can be 'vast' (32767, for example). In that case, it takes time for the kernel to process the descriptors, plus you may not have initialized the fd_set up to that number of entries. FD_SETSIZE also figures in the equation, but sometimes you can change that value.

So, if you want to monitor file descriptors 24-31, you'd set nfds to 32, and ensure that you use FD_ZERO() to zero the whole fd_set and FD_SET() to set entries 24-31. Note, too, that select() modifies the input parameters, so you have to use FD_ISSET() to test after the select() returns, and in general you have to redo the initialization (or copy a saved value) of fd_set before calling select() again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...