scanf 循环和信号处理程序
刚刚了解了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个(迂腐的)正确的 signal 处理程序可以做很少的事情:特别是设置 < code>volatile sig_atomic_t 变量(这是某种整数类型),并且可能调用
siglongjmp
[我什至不确定siglongjmp
]。因此,首先声明
,然后您的信号处理程序很简单
,您的循环是
不要忘记异步信号随时发生(任何机器指令!!!),包括内部<代码>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 callingsiglongjmp
[I'm not even sure forsiglongjmp
].So declare first
then your signal handler is simply
and your loop is
Don't forget that asynchronous signals happen at any time (any machine instruction!!!), including inside
malloc
orprintf
. 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.