scanf 循环和信号处理程序

发布于 2024-12-18 13:08:23 字数 1131 浏览 1 评论 0原文

刚刚了解了 sigacation,还实现了另一个计时器以使其更有趣。

#include <stdio.h>
#include <signal.h> 
#include <sys/time.h>

volatile sig_atomic_t gotsignal;


void handler(){

    gotsignal = 1;

}

int main(){

struct sigaction sig;
struct itimerval timer;

timer.it_value.tv_sec = 5;
timer.it_value.tv_usec = 0;
timer.it_interval = timer.it_value;

sig.sa_handler = handler; 
sig.sa_flags = 0; 
sigemptyset(&sig.sa_mask); 

setitimer(ITIMER_REAL, &timer, 0);
sigaction(SIGALRM, &sig, NULL);


int value, y = 100, x=0;

while(!gotsignal && x < y){
    printf("Insert a value: \n");
    scanf("%d", &value);
    x+=value;
    printf("Current x value: %d\n", x);
}
}

我不明白的是,当它等待用户输入时,我写了 5,但没有按 Enter 键。他还读吗?他不应该清除掉吗? 它给我的输出:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5Current x value: 5

我想要的是:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5(Wthout pressing enter!)Current x value: 3 (He would forget about 5 no?)

Just learned about sigacation, also implemented another timer to make it more interesting.

#include <stdio.h>
#include <signal.h> 
#include <sys/time.h>

volatile sig_atomic_t gotsignal;


void handler(){

    gotsignal = 1;

}

int main(){

struct sigaction sig;
struct itimerval timer;

timer.it_value.tv_sec = 5;
timer.it_value.tv_usec = 0;
timer.it_interval = timer.it_value;

sig.sa_handler = handler; 
sig.sa_flags = 0; 
sigemptyset(&sig.sa_mask); 

setitimer(ITIMER_REAL, &timer, 0);
sigaction(SIGALRM, &sig, NULL);


int value, y = 100, x=0;

while(!gotsignal && x < y){
    printf("Insert a value: \n");
    scanf("%d", &value);
    x+=value;
    printf("Current x value: %d\n", x);
}
}

What i don't understand is, when it is waiting for user input and i write 5, but not press enter. He still reads it? Shouldn't he clear it off?
The output it gave me:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5Current x value: 5

What I would want would be like:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5(Wthout pressing enter!)Current x value: 3 (He would forget about 5 no?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-12-25 13:08:23

一个(迂腐的)正确的 signal 处理程序可以做很少的事情:特别是设置 < code>volatile sig_atomic_t 变量(这是某种整数类型),并且可能调用 siglongjmp [我什至不确定siglongjmp]。

因此,首先声明

volatile sig_atomic_t gotsignal;

,然后您的信号处理程序很简单

void handler (void)
{
  gotsignal = 1;
}

,您的循环是

while(!gotsignal && x < y){
    printf("Insert a value: \n");
    scanf("%d", &value);
    x+=value;
}

不要忘记异步信号随时发生(任何机器指令!!!),包括内部<代码>malloc 或printf。切勿从处理程序内部调用这些函数。

与不良信号处理相关的错误很难调试:它们不可重现

考虑使用 sigaction

A (pedantically) correct signal handler can do very few things: notably setting a volatile sig_atomic_t variable (this is some integer type), and perhaps calling siglongjmp [I'm not even sure for siglongjmp].

So declare first

volatile sig_atomic_t gotsignal;

then your signal handler is simply

void handler (void)
{
  gotsignal = 1;
}

and your loop is

while(!gotsignal && x < y){
    printf("Insert a value: \n");
    scanf("%d", &value);
    x+=value;
}

Don't forget that asynchronous signals happen at any time (any machine instruction!!!), including inside malloc or printf. Never call these functions from inside a handler.

Bugs related to bad signal handling are hard to debug: they are not reproducible!

Consider perhaps using sigaction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文