- 论坛徽章:
- 0
|
询问fd_set
[quote]原帖由 "gg123"]什么意思?[/quote 发表:
在终端输入man 2 select ,看完手册就懂了。
- #include <unistd.h>;
- #include <stdlib.h>;
- #include <fcntl.h>;
- #include <sys/time.h>;
- int main( void )
- {
- int console, serial;
- struct timeval tv;
- fd_set rfd;
- int n;
- if( ( console = open( "/dev/con1", O_RDONLY ) ) == -1
- || ( serial = open( "/dev/ser1", O_RDONLY ) ) == -1 ) {
- perror( "open" );
- return EXIT_FAILURE;
- }
- FD_ZERO( &rfd );
- FD_SET( console, &rfd );
- FD_SET( serial, &rfd );
- tv.tv_sec = 5; /* set a 5 second timeout */
- tv.tv_usec = 0;
- switch ( n = select( 1 + max( console, serial ),&rfd, 0, 0, &tv ) ) {
- case -1:
- perror( "select" );
- return EXIT_FAILURE;
- case 0:
- puts( "select timed out" );
- break;
- default:
- printf( "%d descriptors ready ...\n", n );
- if( FD_ISSET( console, &rfd ) )
- puts( " -- console descriptor has data pending" );
- if( FD_ISSET( serial, &rfd ) )
- puts( " -- serial descriptor has data pending" );
- }
- return EXIT_SUCCESS;
- }
复制代码 |
|