select() and file descriptor usage troubles

  • Follow


Hello All

I am an experienced C programmer but new to Linux development and am 
experiencing difficulties with the select() function.

I have added two serial port file descriptors and the console file 
descriptor (obtained with fileno(stdin)) to an fd_set. As long as I get 
input on ONLY ONE of these file descriptors the select() function does 
what I expect. However when there is input on more than one of these 
descriptors select() only sees activity on one.

For example, if I press a key on the keyboard, select() returns and I 
handle the character and select() continues to do this everytime I press 
a key - perfect! But select() never returns when there is input on ttyS0 
or ttyS1.

Restarting the program and sending chars to ttyS0, then somehow stops me 
getting input from the keyboard or ttyS1.

Its seems like whatever file descriptor is first to get input takes 
control and stops the select() function from seeing input on the other 
file descriptors.

If I work with only one descriptor everything is perfect; more than one 
and the first to see activity works properly and disables the others. 
Closing the program and executing it again allows me to immediately get 
data that was in one of the other file descriptors buffers, so the ports 
are working and holding data in buffers but select() just never knows 
about it.

I would greatly appreciate some help with this matter as my development 
is being held up because of this issue.

Leonard

0
Reply Leonard 11/18/2004 12:44:02 PM

I guess you should paste your code calling select() here, because your
problem lokks strange to me.

> Its seems like whatever file descriptor is first to get input takes
> control and stops the select() function from seeing input on the other
> file descriptors.

Just a thought: are you properly reinitializing your file descriptor sets
between each call to select()? If you didn't, then it could explain the
behavior you observe. To make things clear:

FD_ZERO( &readset );
FD_SET( STDIN_FILENO, &readset );
FD_SET( one_more_fd, &readset );
FD_SET( another_fd, &readset );
while ( run ) {
  select( n, &readset, NULL, NULL, NULL );
  if ( FD_ISSET( STDIN_FILENO, &readset ) ) {
    /* do something */
  }
  if ( FD_ISSET( one_more_fd, &readset ) ) {
    /* do something */
  }
  if ( FD_ISSET( another_fd, &readset ) ) {
    /* do something */
  }
}

Doesn't work and would explain the symptoms you describe. readset has to
be reinitialized before each call to select().

-- 
BBP

0
Reply Brieuc 11/18/2004 11:48:22 AM


1 Replies
364 Views

(page loaded in 0.063 seconds)

Similiar Articles:













7/11/2012 10:29:42 PM


Reply: