【问题标题】:Timeout Function超时功能
【发布时间】:2011-08-29 05:55:18
【问题描述】:

我想制作一个要求输入用户名的代码,但时间限制为 15 秒。如果用户超过限制并且未能输入名称(或任何字符串),则代码将被终止并显示“超时”按摩,否则应保存名称并显示“谢谢”按摩。我曾尝试过这样的尝试,但这是错误的并且无法正常工作。请给我一个解决方案。谢谢。

#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;
}

【问题讨论】:

  • 建议使用:getitimer()setitimer()(可能只需要setitimer() 函数。建议阅读setitimer() 的MAN 页面
  • 建议使用select()函数,最后一个参数为struct timeval,内容为15秒

标签: c linux


【解决方案1】:

这个虚拟程序可能会对你有所帮助:

#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(STDIN_FILENO, &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;
}

更新:
现在这是经过测试的代码。

另外,我从 man 那里得到了select 的提示。本手册已包含一个代码 sn-p,用于从终端读取并在 5 秒内超时,以防无活动。

如果代码写得不够好,只是一个简短的解释:

  1. 我们将输入流 (fd = 1) 添加到 FD 集。
  2. 我们发起 select 调用以监听为 任何活动。
  3. 如果在timeout 期间发生任何活动,即 通读read 电话。
  4. 如果没有活动,则会发生超时。

希望这会有所帮助。

【讨论】:

  • 顺便说一句,您的 scanf 不起作用,因为它是对输入流的阻塞读取调用。
  • 非常感谢 Shrey。真的,这段代码对我非常有用。因为我从互联网上得到了很多 select() 的例子,但这些例子非常复杂。这段代码很容易理解并让我工作。感谢 4 您的帮助。
  • Shrey,我观察到如果我从两个 printf 语句中删除 \n ,那么它首先采用 read() 函数,然后打印 printf() 语句的消息。应该是什么原因??
  • read() 函数也会在输出中给出一些垃圾字符。
  • @Heet 至于前面的评论,关于 printf 之前的读取,那是因为行缓冲。 \n 将强制终端上的输出(据我所知)。可能,使用类似刷新的调用(在读取之前)可以确保在执行读取之前所有内容都在屏幕上。
【解决方案2】:

scanf() 并不是在有限的时间范围内获得输入的最佳函数。

相反,我会围绕select()(用于管理超时)和read()(用于获取输入)系统调用构建一个特定的输入函数。

【讨论】:

  • 谢谢 mouviciel,能否请您给我 select() 的语法以及示例示例?
【解决方案3】:

您必须考虑的一件事是,您的程序中有一个执行线程。因此,timeout 函数只会在 scanf 函数终止时被调用。这不是你想要的。

完成此任务的一种方法是使用select 函数。它会等待可能有限的时间(您的超时),以便在某些文件描述符(stdin 为您)上输入可用性。

【讨论】:

    猜你喜欢
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    相关资源
    最近更新 更多