超时功能 [英] Timeout Function
问题描述
我想打一个code,其中用户的名称将被要求输入,但在15秒的时间限制。如果用户跨越极限和放大器;没有输入一个名称(或任何字符串),那么code将被终止和放大器; 超时按摩会显示姓名,否则应保存和放大器; 谢谢你按摩会显示。我曾尝试这样的,但它是错误的&安培;不工作。请给我这个..感谢的解决方案。
的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;INT超时(INT秒)
{
clock_t表示endwait;
endwait =时钟()+秒* CLOCKS_PER_SEC;
而(时钟()&下; endwait){} 返回1;
}诠释的main()
{
焦炭名[20];
的printf(请输入用户名:(15秒),\\ n);
的printf(时间从现在做起!\\ n); scanf函数(%S,名);
如果(超时(5)== 1){
的printf(超时\\ n);
返回0;
} 的printf(Thnaks \\ n);
返回0;
}
也许这虚拟程序可能会帮助您:
的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / time.h中>
#包括LT&; SYS / types.h中>#定义等待3诠释的main()
{
焦炭名[20] = {0}; //在单个字符输入的情况下
FD_SET input_set;
timeval结构超时;
INT ready_for_reading = 0;
INT read_bytes = 0; / *清空FD集* /
FD_ZERO(安培; input_set);
/ *听输入描述符* /
FD_SET(0,&放大器; input_set); / *等待几秒钟* /
timeout.tv_sec =等待; //等待秒
timeout.tv_usec = 0; // 0毫秒 / *邀请用户写的东西* /
的printf(请输入用户名:(单位:%d秒)\\ n,等待);
的printf(时间从现在做起!\\ n); / *监听输入流的任何活动* /
ready_for_reading =选择(1,&安培; input_set,NULL,NULL,&安培;超时);
/ *在这里,第一个参数是文件描述符的数量设置,
*第二个是我们的FD集阅读,
*第三个是在任何写操作需要更新FD集,
*这是不是在这种情况下,必需的。
*四是超时
* / 如果(ready_for_reading == -1){
/ *一些错误发生在输入* /
的printf(无法读取您的输入\\ n);
返回-1;
} 如果(ready_for_reading){
read_bytes =读(0,名称,19);
如果(名称[read_bytes-1] =='\\ n'){
--read_bytes;
名称[read_bytes] ='\\ 0';
}
如果(read_bytes == 0){
的printf(你只需点击进入\\ n);
}其他{
的printf(读,从输入%d字节:%S \\ n,read_bytes,名);
}
}其他{
的printf(%d秒结束了 - 没有数据输入\\ n,等待);
} 返回0;
}
更新:结果
这是目前测试code。
另外,我采取了从人的提示为选择
。本手册已包含正在被用于从在5秒内终端和超时在没有活动的情况下,读了code片段。
以防万一简要说明了code写得不好不够的:
- 我们添加的输入流(
FD = 1
)的FD集。 - 我们启动
选择
打电话听为创建此FD集
任何活动。 - 在任何情况下的活动
超时内发生
期,即
通过读
通话读取。 - 在情况下,有没有活动,超时发生。
希望这有助于。
I want to make a code in which Name of User will be asked to input, but in a time limit of 15 seconds. If user cross the limit & failed to input a name(or any string), then code will be terminated & "Time Out" Massage will be display otherwise Name should be saved & "Thanks" massage will be display. I had try like this but it's wrong & not working. Please give me a solution for this.. Thanks.
#include <stdio.h>
#include <time.h>
int timeout ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
return 1;
}
int main ()
{
char name[20];
printf("Enter Username: (in 15 seconds)\n");
printf("Time start now!!!\n");
scanf("%s",name);
if( timeout(5) == 1 ){
printf("Time Out\n");
return 0;
}
printf("Thnaks\n");
return 0;
}
Probably this dummy program might help you:
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#define WAIT 3
int main ()
{
char name[20] = {0}; // in case of single character input
fd_set input_set;
struct timeval timeout;
int ready_for_reading = 0;
int read_bytes = 0;
/* Empty the FD Set */
FD_ZERO(&input_set );
/* Listen to the input descriptor */
FD_SET(0, &input_set);
/* Waiting for some seconds */
timeout.tv_sec = WAIT; // WAIT seconds
timeout.tv_usec = 0; // 0 milliseconds
/* Invitation for the user to write something */
printf("Enter Username: (in %d seconds)\n", WAIT);
printf("Time start now!!!\n");
/* Listening for input stream for any activity */
ready_for_reading = select(1, &input_set, NULL, NULL, &timeout);
/* Here, first parameter is number of FDs in the set,
* second is our FD set for reading,
* third is the FD set in which any write activity needs to updated,
* which is not required in this case.
* Fourth is timeout
*/
if (ready_for_reading == -1) {
/* Some error has occured in input */
printf("Unable to read your input\n");
return -1;
}
if (ready_for_reading) {
read_bytes = read(0, name, 19);
if(name[read_bytes-1]=='\n'){
--read_bytes;
name[read_bytes]='\0';
}
if(read_bytes==0){
printf("You just hit enter\n");
} else {
printf("Read, %d bytes from input : %s \n", read_bytes, name);
}
} else {
printf(" %d Seconds are over - no data input \n", WAIT);
}
return 0;
}
Update:
This is now tested code.
Also, I have taken hints from man for select
. This manual already contains a code snippet which is being used to read from the terminal and timeout in 5 seconds in case of no activity.
Just a brief explanation in case the code is not well written enough:
- We add the input stream (
fd = 1
) to the FD set. - We initiate
select
call to listen to this FD set created for any activity. - In case any activity occurs within the
timeout
period, that is read through theread
call. - In case there was no activity, timeout occurs.
Hope this helps.
这篇关于超时功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!