使用select()达到输入延时的效果

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STDIN 0 /* file descriptor for standard input */

int main(int argc, char *argv[])
{
	struct timeval tv; 
	fd_set readfds; 
	tv.tv_sec = 2; 
	tv.tv_usec = 500000; 
	FD_ZERO(&readfds); 
	FD_SET(STDIN, &readfds); 
	/* don't care about writefds and exceptfds: */ 
	select(STDIN+1, &readfds, NULL, NULL, &tv); 
	if (FD_ISSET(STDIN, &readfds))
		printf("A key was pressed!\n"); 
	else 
		printf("Timed out.\n");
	
	return 0;
}

你可能感兴趣的:(struct,File,null,input,include,Descriptor)