system call return badly

system call return badly

Post by olucch.. » Fri, 17 Dec 1999 04:00:00



Hi there,

I'm trying to use a select in order to switch between
different input descriptor, but, even if no data is present
in the files, select call always return with all the descriptor
set as ready for read.
Here the sample code running on Linux RedHat 6.0.

#include    <stdio.h>
#include    <sys/time.h>
#include    <sys/types.h>
#include    <fcntl.h>
#include    <errno.h>

main()
{
    int     iFDa, iFDb, iFDc, iMax;
    int     iRet;
    char    c;
    char    acBuf[80];
    fd_set  fdsR;

    iFDa = open( "./sa", O_RDONLY | O_NDELAY );
    iFDb = open( "./sb", O_RDONLY | O_NDELAY );
    iMax = iFDc = open( "./sc", O_RDONLY | O_NDELAY );

    if ( iMax < iFDa ) iMax = iFDa;
    if ( iMax < iFDb ) iMax = iFDb;

    FD_ZERO( &fdsR );
    FD_SET( iFDa, &fdsR );
    FD_SET( iFDb, &fdsR );
    FD_SET( iFDc, &fdsR );
    while( 1 )
    {
        iRet = select( iMax + 1, &fdsR, NULL, NULL, NULL );

        printf( "Select return %d (%d)- ", iRet, errno );
        if ( FD_ISSET( iFDa, &fdsR ) )
        {
            while( read( iFDa, &c, 1 ) == 1 )
            {
                printf( "%c", c );
            }
            printf( "EOF on a\n" );
        }

        if ( FD_ISSET( iFDb, &fdsR ) )
        {
            while( read( iFDb, &c, 1 ) == 1 )
            {
                printf( "%c", c );
            }
            printf( "EOF on b\n" );
        }

        if ( FD_ISSET( iFDc, &fdsR ) )
        {
            while( read( iFDc, &c, 1 ) == 1 )
            {
                printf( "%c", c );
            }
            printf( "EOF on c\n" );
        }
    }

Quote:}

Running this code, select return immediately with
value 3 and FD_ISSET is true for all the iFD.
Reading from them will immediately catch an EOF.

Do I mistake something?
Must I include something I don't?
Must I use some special library?

Any help will be appreciated.

Osvaldo

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

system call return badly

Post by David Schwart » Sat, 18 Dec 1999 04:00:00



> Hi there,

> I'm trying to use a select in order to switch between
> different input descriptor, but, even if no data is present
> in the files, select call always return with all the descriptor
> set as ready for read.
[snip]
> Running this code, select return immediately with
> value 3 and FD_ISSET is true for all the iFD.
> Reading from them will immediately catch an EOF.

> Do I mistake something?

        No, you are correct. Since the read completes immediately, the file is
ready for reading. This is what select is telling you.

Quote:> Must I include something I don't?
> Must I use some special library?

        No. You must poll the file, either trying to read from it or checking
its length and/or modification date.

        Since a local file never blocks (AFAIK), select is not particularly
useful on it.

        DS