使用在写在2019年来临前的倒数0.5小时的system_test()函数来调用如下loop_echo程序:
#include <stdio.h>
#include <string.h>
#include <signal.h>
#define BUFSZ 1024
static void handler(int sig)
{
printf("<.q> to exit\n");
}
int main(void)
{
struct sigaction sa;
printf("###################################\n");
char buf[BUFSZ] = {0};
/* 捕捉SIGQUIT和SIGINT信号 */
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handler;
if (sigaction(SIGINT, &sa, NULL) < 0) {
perror("sigaction");
return -1;
}
if (sigaction(SIGQUIT, &sa, NULL) < 0) {
perror("sigaction");
return -1;
}
while (1) {
/* 获取终端输入被回显,直至用户键入.q才退出 */
fgets(buf, BUFSZ, stdin);
if(strstr(buf, ".q")) {
puts("Bye\n");
break;
}
puts(buf);
bzero(buf, BUFSZ);
}
return 0;
}
程序主流程main.c是这样的:
#include <stdio.h>
#include <signal.h>
#includ