This article is mainly notes I have taken for CSCB09/CSC209 at UofT.

I/O Multiplexing

We use select to achieve I/O multiplexing.

int select(int maxfdp1,
           fd_set *readset,
           fd_set *writeset,
           fd_set *exceptset,
           const struct timeval *timeout);
struct timeval {
    long tv_sec;
    long tv_usec;
}

Sets are hidden within fd_set type, but usually is just a list of integers.

for(;;) {
    rset = allset;
    nready = select(maxfd+1, &rset, NULL, NULL, NULL);
    if(FD_ISSET(listenfd, &rset)) {
        connfd = accept(listenfd, &caddr, &clen);
        for(i = 0; i < FD_SETSIZE; i++) {
            if(client[i] < 0) {
                client[i] = connfd; break;
            }
        }
        FD_SET(connfd, &allset);
        if(connfd > maxfd) maxfd = connfd;
    }
    for(i = 0; i <= maxi; i++) {
        if(sockfd = client[i]) < 0) continue;
        if(FD_ISSET(sockfd, &rset))
            read(sockfd, line, MAXLINE);
    }
}

Bit Strings

Bitwise operators

<< // left shift
>> // right shift
~ // Not
& // And
^ // Xor
| // Or