⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test2.c

📁 socket程序大家见多了
💻 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;	}}inline void do_run( int fd ){	struct pollfd pollfdset[ POLLFD_NUM ]; 	int result;	fd_set rfds;	while( 1 )	{		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" );			}		}		#if 0		memset( &pollfdset, 0, sizeof( pollfdset ) );		pollfdset[ 0 ].fd = fd;		pollfdset[ 0 ].events = POLLIN | 0x02 | 0x04 | 0x08;		pollfdset[ 0 ].revents = 0;		result = poll( pollfdset, fd + 1, 1000 * 1000 );		printf( "after poll\n" );		if( 0 > result )		{			printf( "poll error: %s\n", strerror( errno ) );		}		else if( 0 == result )		{			printf( "poll timeout\n" );		}		else 		{			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 );	count = 2;	ioctl( test_fd, 0x7F, &count );	close( test_fd );	return 0;}#endif /*#ifdef AUTO_SCRIPTE*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -