?? test1.c
字號:
#ifndef AUTO_SCRIPTE#define AUTO_SCRIPTE#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/poll.h>#include <errno.h>#include <signal.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>#define DEV_PATH "/dev/test"#define POLLFD_NUM 8int test_fd = -1;unsigned long count = 0;void signal_handler( int sig ){ switch( sig ) { case SIGINT: ioctl( test_fd, 0x7F, &count ); count = ( ++count % 4 ); if( count == 3 ) exit( 0 ); break; default: break; }}void do_run( int fd ){ struct pollfd pollfdset[ POLLFD_NUM ]; int result; fd_set rfds; while( 1 ) {#if 0 FD_ZERO( &rfds ); FD_SET( fd, &rfds ); result = select( fd + 1, &rfds, NULL, NULL, NULL ); if( 0 > result ) { printf( "select error: %s\n", strerror( errno ) ); } else if( 0 == result ) { printf( "selecttimeout\n" ); } else { if( FD_ISSET( fd, &rfds ) ) { printf( "select fd\n" ); } }#endif #if 1 //memset( pollfdset, 0, sizeof( pollfdset ) );#if 1 pollfdset[0].fd = 0; pollfdset[0].events = POLLIN; pollfdset[0].revents = 0;#endif #if 0 pollfdset[2].fd = 1; pollfdset[2].events = POLLIN; pollfdset[2].revents = 0;#endif pollfdset[1].fd = fd; pollfdset[1].events = POLLIN | 0x02 | 0x04 | 0x08; pollfdset[1].revents = 0; result = poll( pollfdset, 2, 1000 * 1000 ); if( 0 > result ) { printf( "poll error: %s\n", strerror( errno ) ); } else if( 0 == result ) { printf( "poll timeout\n" ); } else { printf( "result: %d\n", result ); if( 0x01 & pollfdset[ 0 ].revents ) printf( "0x01 poll\n" ); if( 0x02 & pollfdset[ 0 ].revents ) printf( "0x02 poll\n" ); if( 0x04 & pollfdset[ 0 ].revents ) printf( "0x04 poll\n" ); if( 0x08 & pollfdset[ 0 ].revents ) printf( "0x08 poll\n" ); }#endif }}int main( int argc, char *argv[] ){ //signal( SIGINT, signal_handler ); test_fd = open( DEV_PATH, O_RDWR ); if( 0 > test_fd ) { fprintf( stderr, "Open %s failed: %s\n", DEV_PATH, strerror( errno ) ); return -1; } do_run( test_fd ); close( test_fd ); return 0;}#endif /*#ifdef AUTO_SCRIPTE*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -