0
点赞
收藏
分享

微信扫一扫

量化交易之linux篇 - 《UNIX环境高级编程》 - 捕捉信号SIGINT

Xin_So 2023-03-02 阅读 105


#include "apue.h"
#include <stdio.h>
#include <dirent.h>
#include <sys/wait.h>

static void sig_int(int value); // signal catching function

int main(int argc, char* argv[]) {
char buf[MAXLINE];
pid_t pidl
int status;

if (signal(SIGINT, sig_int) == SIG_ERR)
err_sys("signal error.");

printf("%%");

while (fgets(buf, MAXLINE, stdin) != NULL) {
if (buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = 0;

if ((pid = fork()) < 0) {
err_sys("fork error");
} else if ((pid == 0)) {
execlp(buf, buf, (char *)0);
err_ret("couldn't exe: %s", buf);
exit(127);
}

if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error.");
printf("%%");
}

exit(0);
}


void sig_int(int value) {
printf("interupt\n");
}

举报

相关推荐

0 条评论