gpt4 book ai didi

c - SIGINT 和 SIGQUIT

转载 作者:行者123 更新时间:2023-12-02 03:30:59 26 4
gpt4 key购买 nike

我想从我的代码中启动计算器应用程序,用sigint-2中断它显示它已被中断,再次启动它,然后用sigquit-9退出它,想法是在C代码中中断它所以不需要按 Ctrl+CCtrl+

Write a C program that accepts the signals SIGINT and SIGQUIT via a signalfd file descriptor. The program terminates after accepting a SIGQUIT signal.

C语言中启动一个进程,然后中断它,然后结束它的语法是什么?

最佳答案

我想这可能就是你要找的

//
// main.c
// Project 4
//
// Found help with understanding and coding at
// http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
//

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
//signal handling function that will except ctrl-\ and ctrl-c
void sig_handler(int signo)
{
//looks for ctrl-c which has a value of 2
if (signo == SIGINT)
printf("\nreceived SIGINT\n");
//looks for ctrl-\ which has a value of 9
else if (signo == SIGQUIT)
printf("\nreceived SIGQUIT\n");
}

int main(void)
{
//these if statement catch errors
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
if (signal(SIGQUIT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGQUIT\n");
//Runs the program infinitely so we can continue to input signals
while(1)
sleep(1);
return 0;
}

关于c - SIGINT 和 SIGQUIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26747590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com