问题描述
刚刚得知sigacation,还实施了另外一个定时器,使其更有趣。
的#include<&stdio.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&; SYS / time.h中>挥发性sig_atomic_t gotsignal;
空隙的处理程序(){ gotsignal = 1;}诠释主(){结构sigaction的签名;
结构体itimerval定时器;timer.it_value.tv_sec = 5;
timer.it_value.tv_usec = 0;
timer.it_interval = timer.it_value;sig.sa_handler =处理程序;
sig.sa_flags = 0;
sigemptyset(安培; sig.sa_mask);setitimer函数(ITIMER_REAL,&放大器;计时器,0);
的sigaction(SIGALRM,&安培; SIG,NULL);
int值,Y = 100,X = 0;而(gotsignal&安培;!&安培; X< Y){
的printf(插入值:\\ n);
scanf函数(%d个,&安培;值);
X + =价值;
的printf(当前x值数:%d \\ n,x)的;
}
}
我不明白的是,当等待用户输入和我写5,但不是preSS进入。他还读它?他不应该清除它关掉?
输出它给我:
插入一个值:
1
当前x值:1
插入值:
2
当前x值:3
插入值:
5Current x值:5
我希望会是这样的:
插入一个值:
1
当前x值:1
插入值:
2
当前x值:3
插入值:
5(Wthout pressing进入!)当前x值:3(他会忘记5不是吗?)
A(迂腐)正确的可以做的非常几件事情:特别设置挥发性sig_atomic_t
变量(这是一些整型),也许叫 siglongjmp
[我甚至不能确定为 siglongjmp
。
所以先申报
挥发性sig_atomic_t gotsignal;
那么你的信号处理程序很简单
无效处理程序(无效)
{
gotsignal = 1;
}
和你的循环是
而(gotsignal&安培;!&安培; X< Y){
的printf(插入值:\\ n);
scanf函数(%d个,&安培;值);
X + =价值;
}
不要忘了异步的信号发生在任何时间(所有的机的指令!),里面包括的malloc
或的printf
。切勿从处理器中调用这些函数。
相关不好的信号处理错误是很难调试:他们的不可再生的
考虑可能使用。
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?)
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.
这篇关于scanf的环,和信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!