سؤال

I'm currently studying OS's and i came across a question that i cant quite figure it out.

I have this piece of pseudo-code:

for(;;) {
   mask = teskmask;
   select(MAXSOCKS,&mask,0,0);
   if(FD_ISSET(strmfd,&mask)) {
     clilen=sizeof(cliaddr);
     newfd=accept(strmfd,(struct sockaddr*)&cliaddr,&clilen);
     echo(newfd);
     close(newfd);
   }
   if (FD_ISSET(dgrmfd,&mask)) echo (dgrmfd);
}

Note: consider MAXSOCKS to be defined as whatever (doesn't matter here), strmfd to be a Stream socket, dgrmfd as a datagram socket, clilen is the size of client address, echo(newfd); is just a function to echo whats in the socket.

So my question is:

what is/whats for the testmask and mask and how is it used here?

I know that select blocks the process until some socket is available to read/write or exceptions.

هل كانت مفيدة؟

المحلول

The simplest way to get answer to your question is just to read select(2) manual page, which says:

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

or a quite similar declaration. So "testmask" is fd_set. Select() gets these masks on input and refills them on output, unless an error is signaled.

In all Unix systems I've seen fd_set is bit array up to FD_SETSIZE bits, and it could be hard to extend this to larger value, so a descriptor greater or equal to FD_SETSIZE can't be select()ed without target-specific hacking. So poll() is preferred for a large descriptor set. (And, platform-specific solutions as kqueue (*BSD, MacOSX), epoll (Linux), /dev/poll (Solaris) are better if relatively small subset of all descriptors is ready on each cycle; for Linux it's ~30% when poll() is more efficient than epoll-based scheme, measured for systems ~3 years ago.)

In Windows systems, fd_set is array of socket numbers (integers) with unlimited value, but limited total count (AFAIR, 64).

Using only standard macros to deal with fd_set gives code which is portable between Unix and Windows. But this code could be inefficient for both in different manner.

نصائح أخرى

Actually the proper way to copy the select mask is to use the FD_COPY macro.

I have just posted an article about the difference between select, poll and epoll here: http://www.ulduzsoft.com/2014/01/select-poll-epoll-practical-difference-for-system-architects/ - I suggest you take a look on it as it covers many things you asked, and there's a chance it covers something you missed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top