Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c - Is it necessary to reset the fd_set between select system call?

I am facing a problem using the select function in Unix.

I have a server that waits for for a connection. First I add the listening socket file descriptor listener to the fd_set readfds using FD_SET(listener, readfds) and then I use that in select().

When I get a connection, I call accept() and set the readfds in select with the accepted file descriptor and start receiving the data from connection. However, when I check the code in strace, The select doesn't show the listener in the readfds while select() is executing a second time.

Do I need to set the listener file descriptor again using FD_SET(listener, readfds) before calling select() again?

Thanks.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Yes (it is necessary to reset the fd_set between select() system calls).

It is a nuisance, but they act as input/output parameters; they are read by and modified by the system call. When select() returns, the values have all been modified to reflect the set of file descriptors ready. So, every time before you call select(), you have to (re)initialize the fd_set values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...