【发布时间】:2018-03-20 22:38:11
【问题描述】:
我需要制作一个程序,该程序无限期地写入三个点。当按下 CTRL+C(使用 SIGINT)时,程序需要写入再见信息并终止进程。当按 CTRL+Z 然后按 fg(使用 SIGCONT),程序需要测量在这次以秒为单位,每经过一秒,程序就需要写一个点。当程序写入所有点时,它返回到三个点。
$ ./program
...
...
...
...
... <pressing Ctrl+Z>
$ <we wait few seconds (5)>
$ fg
.
.
.
.
.
...
...
...
...
<pressing Ctrl+C>
Ending
$
我设法通过了第一部分(在无限循环中使用 CTRL+C 终止进程)但我找不到测量使用 CTRL+Z 停止进程后的时间,然后使用 fg 继续。
当前工作代码:
bool stop = true;
void end(int sig){
printf("ending\n");
//signal(SIGINT, SIG_DFL);
//kill(getpid(), SIGINT);
stop = false;
}
int main(){
struct sigaction act;
act.sa_handler = end;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);
while(stop) {
printf("...\n");
sleep(1);
}
return 0;
}
我打算如何测量时间:
t = clock();
pause();
t = clock() - t;
double time = ((double)t)/CLOCKS_PER_SEC;
printf("time: %f", time);
【问题讨论】:
-
请发帖Minimal, Complete, and Verifiable example。欢迎来到 StackOverflow!
-
对不起,我忘了。添加。谢谢。
标签: c linux c++11 signals sigint