linux C语言select函数怎么不能实现sleep呀?
#include <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main()
{
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int i =0;
while( i++ < 10)
{
printf("%d\n", i);
fflush(stdout);
int ret = select (0, NULL, NULL, NULL, &tv);
if (ret == -1)
{
fprintf (stderr, "select error . errno=%d [%s]\n", errno, strerror(errno));
break;
}
//sleep(1);
//usleep(1000000);
}
return 0;
}
答案:我修改了一下,运行一下试试,祝你好运:
#include <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main()
{
struct timeval tv;
int i =0;
while( i++ < 10)
{
tv.tv_sec = 1;
tv.tv_usec = 0;
printf("%d\n", i);
fflush(stdout);
int ret = select (0, NULL, NULL, NULL, &tv);
if (ret == -1)
{
fprintf (stdout, "select error . errno=%d [%s]\n", errno, strerror(errno));
break;
}
//sleep(1);
//usleep(1000000);
}
return 0;
}
其他:无回显输入
noecho();
参考例子;
#include<stdlib.h>;
#include<stdio.h>;
#include<termios.h>;
#include<string.h>;
static struct termios stored_settings1;
static struct termios stored_settings2;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings1);
new_settings =stored_settings1;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}
void echo_on(void)
{
tcsetattr(0,TCSANOW,&stored_settings1);
return;
}
void set_keypress(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings2);
new_settings = stored_settings2;
/*Disable canornical mode, and set buffer size to 1 byte */
new_settings.c_lflag&=(~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_settings);
return;
}
void reset_keypress(void)
{
tcsetattr(0, TCSANOW, &stored_settings2);
return;
}
int main()
{
printf("========================\n");
printf("0. Main menu\n");
printf("1. Chapter one\n");
printf("2. Chapter two\n");
printf("3. Chapter three\n");
printf("========================\n");
echo_off();
set_keypress();
char ch;
while((ch = getchar() ) != 'q')
{
switch(ch)
{
case '0':
printf("In main menu.\n");break;
case '1':
printf("Entering chapter one\n");break;
case '2':
printf("Entering chapter two\n");break;
case '3':
printf("Entering chapter three\n");break;
default:
printf("Entering other chapter.\n");
}
}
reset_keypress();
echo_on();
return 0;
}
上一个:在编写c语言程序的时候如何点亮液晶显示器的一点或者一部分让其显示???
下一个:C语言 题一道 急求高手 改错