Re: Select with device and stdin not working

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Wed Jul 25 2001 - 10:45:19 EST


On 25 Jul 2001, M. Tavasti wrote:

> "Richard B. Johnson" <root@chaos.analogic.com> writes:
>
> > Change:
> > } else if(FD_ISSET(fileno(stdin),&rfds) ) {
> > To:
> > } if(FD_ISSET(fileno(stdin),&rfds) ) {
> >
> > Both of these bits can be (probably are) set.
>
> You are third person to suggest that. Yes, it's good point, but
> doesn't make any difference. Or it makes when both device and stdin
> have something, stdin is read on second round.
>
> But now there is nothing coming from device, and typing + pressing
> enter won't make select() return like it should.

It works here...........

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <errno.h>
#include <string.h>

static const char dev[]="/dev/random";

int main(int args, char *argv[])
{
    int fd, retval;
    fd_set rfds;

    if((fd = open(dev, O_RDONLY)) < 0)
    {
        fprintf(stderr, "Can't open device, %s\n", dev);
        exit(EXIT_FAILURE);
    }
    for(;;)
    {
        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);
        FD_SET(STDIN_FILENO, &rfds);
        retval = select(fd+1, &rfds, NULL, NULL, NULL);
        if(retval < 0)
            fprintf(stderr, "Error was %s\n", strerror(errno));
        printf("Return = %d\n", retval);
        if(FD_ISSET(fd, &rfds))
            printf("Input is available from %s\n", dev);
        if(FD_ISSET(STDIN_FILENO, &rfds))
            printf("Input is available from %s\n", "terminal");
    }
    if(close(fd) < 0)
    {
        fprintf(stderr, "Can't close device, %s\n", dev);
        exit(EXIT_FAILURE);
    }
    return 0;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

    I was going to compile a list of innovations that could be
    attributed to Microsoft. Once I realized that Ctrl-Alt-Del
    was handled in the BIOS, I found that there aren't any.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue Jul 31 2001 - 21:00:23 EST