如何在几秒钟后终止 scanf?

How to terminate scanf after a few seconds?

我正在使用 C 语言中的信号。该程序包括等待用户键盘输入几秒钟,如果时间结束,程序将终止。但是,我总是必须输入文本,尽管时间已经结束,如果不是程序永远不会结束的话。有什么办法可以避免scanf吗?

这是我的代码

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#define NSECS 10
#define TRUE 1
#define FALSE 0
# define BELLS "[=10=]7[=10=]7[=10=]7"

int alarm_flag = FALSE;

void bye(){
    printf("Good bye...");
}

void setflag(){
    alarm_flag = TRUE;
}

int main() {
char name[20];
    do {
        

        printf("File name: \n"); 
        scanf("%s", name);

        signal(SIGALRM, setflag);
        alarm(NSECS);
        
        if (alarm_flag == TRUE){
            printf(BELLS);
            atexit(adios); 
            return 0;
        } // if the user enters a file name, the program continues 

           

快到了——先设置闹钟,然后再拨打 scanf。该信号将中断 scanf() 内对 read() 的调用,这会导致 scanf() 立即变为 return。

volatile int alarm_flag = 0;
void setflag(int sig) {
    alarm_flag = 1;
}

...

struct sigaction act = {};
act.sa_handler = set_flag;
sigaction(SIGALRM, &act, NULL);

...

    alarm(NSECS);
    scanf("%s", name);
    if (alarm_flag) {
        ...

几个注意事项:

  • alarm_flag 应该是 volatile.

  • setflag 应该带一个 int 参数。

  • 将函数声明为 func(void) 而不是 func()。将函数声明为 func() 是 1990 年左右以前的 old-fashioned 风格,今天使用它没有任何好处。 (注意 C++ 是不同的。)

更多注释:

  • 您不应使用 == TRUE== FALSE。在这种特殊情况下,它可能工作正常,但在某些情况下却不能。所以我几乎不会使用 == TRUE.

  • 作为练习,这段带有 alarm 的代码是合理的,但是如果你想在生产应用程序中做这种事情,你可能会使用 libuv 之类的东西alarm()。并不是说这种方法有什么错误,只是使用non-blocking IO 和libuv 可能更容易处理。