Home > Archive > Unix Programming > November 2004 > select() and file descriptor usage troubles
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
select() and file descriptor usage troubles
|
|
| Leonard 2004-11-18, 8:58 am |
| 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
| |
| Brieuc Jeunhomme 2004-11-18, 3:59 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
|
|
|
|
|