ITEM: T4757L

Help with multi-descriptor async I/O


ENV:
        AIX 3.2.5

DESC:
        I am doing a split-screen "chat" type program 
        like talk.  I will have two file descriptors - 
        on one I will read and display to one half of
        the screen, the other I will display the other
        half of the screen.  What I don't know is how
        can a single process read from two file
        descriptors at once.  If I read from one of them,
        the read will block until there is something to
        read and thus anything coming in from the other
        descriptor is put on hold or lost.  How can I
        read from two TTYs at once?

ACT:
        Below is a small sample code fragment that does
        just what you need.  I use fd_set to build a set
        of ttys (set in the mathematical sense of the word)
        and then use select() to wait until data is present
        on at least one of these descriptors.  I could then
        safely read from it since I know for sure there is
        something there to read.

\#include \
\#include \
\#include \

int main()
{       fd_set  afds;           /* All fd's on which requests may come */

        int i;

        int fd1 = fileno(stdin);                /* fd1 is now stdin */
        int fd2 = open("/dev/tty0", O_RDWR);    

        
        FD_ZERO(&afds);         /* Clear all bits to 0 */
        FD_SET(fd1, &afds);     /* Set bit corresponding to fd1 descriptor */
        FD_SET(fd2, &afds);     /* Set bit corresponding to fd2 descriptor */

        /* Now wait until fd1 OR fd2 has something for us to read */
        select(getdtablesize(), &afds, (fd_set *)0, (fd_set *)0,
                (struct timeval *)0);

        /* Checking to see which bits were set so I know which
           file descriptors have something for me to read */
        for (i = 0; i \< getdtablesize(); i++) 
           if (FD_ISSET(i, &afds))
                printf("File descriptor %d has something to read\\n", i);


Support Line: Help with multi-descriptor async I/O ITEM: T4757L
Dated: April 1995 Category: N/A
This HTML file was generated 99/06/24~13:30:35
Comments or suggestions? Contact us