使用signal()函数将处理程序设置为忽略SIGINT信号。在父进程中,我们可以使用fork()函数创建子进程,并在其中使用exec()函数执行所需的命令。在子进程中,我们可以调用signal()函数将处理程序设置回默认值。
以下是一个示例代码,演示了如何在父进程中忽略SIGINT信号并在子进程中将信号处理程序设置回默认值:
#include
#include
#include
#include
void sigint_handler(int sig) {
// do nothing
}
int main() {
pid_t pid;
// set SIGINT handler to ignore in parent
signal(SIGINT, sigint_handler);
pid = fork();
if (pid == -1) {
perror("fork");
return 1;
}
else if (pid == 0) {
// child process
printf("Executing child process...\n");
// set SIGINT handler to default in child
signal(SIGINT, SIG_DFL);
execl("/bin/sleep", "sleep", "10", NULL);
perror("execl");
return 1;
}
else {
// parent process
printf("Executing parent process...\n");
wait(NULL);
printf("Child process finished.\n");
return 0;
}
}