我们当前的项目基于通过包括滚动来扩展more
。为此,定时器间隔必须设置一段时间。我不确定的部分是报警信号的回路应该在哪里。我见过的所有例子都有主值中的定时器值,然后通过无限循环中的pause()
明确调用信号处理程序。带间隔计时器的故障
我的代码是有点不同,因为功能性要求去像
print first screen of text after getting terminal dimensions
print prompt
if prompt = space, print another screen of text //WORKS
if prompe = q, restore original terminal settings & quit program //WORKS
if prompt = ENTER, initialize scroll at 1 line every 2 seconds //DOESN'T WORK
if prompt == f/s, increase/decrease scroll speed by 20% //DOESN'T WORK
读缓冲区,文件指针和itimerval结构都是全局变量,以避免将作为参数通过功能链。
该方案的主要功能是
void processInput(FILE *fp){
void printLine(int); //prints a single line of text
signal(SIGPROF, printLine);
int c;
//print first screen of text, check for more text to display
info(); //print prompt at bottom of screen
FILE *fterm= fopen("/dev/tty", "r");
while ((c=getc(fterm)) != EOF){
if (c== '\n'){
setTimer(2);
//four more conditionals like this in basic similarity
}
}
我SetTimer函数具有2秒的间隔基,和改变通过加/减基于从用户F/S输入的20%。
void setTimer(int direction){
int speed=2000000; //2 seconds
int change= 400000; //400 milliseconds, 20% of 2 seconds
if (direction == 1) //slow down by 20%
speed+= change;
if (direction == 0)
speed -= change;
timer.it_value.tv_sec=2;
timer.it_value.tv_usec=0;
timer.it_interval.tv_sec=0;
timer.it_interval.tv_usec= speed;
setitimer(ITIMER_PROF, &timer, NULL);
}
第一个问题:我应该使用SIGALRM VS SIGPROF,并相应地改变ITIMER_XXXX变量?
其次,我应该在哪里放置回路来触发信号?我试过
while(1)
pause();
在几个条件中,但它具有停止执行和忽略任何输入的效果。