当前位置: 技术问答>linux和unix
如何通过ctrl+c终止自己的运行程序?
来源: 互联网 发布时间:2015-03-14
本文导语: 编写了一个小shell,想通过用户键入ctrl+C终止该shell程序,该怎么做?谢谢 | 哦,上面是内核态程序,用户程序是这样的: #include static void catch_sigint( int ); int main( int argc, char** argv ) { ...
编写了一个小shell,想通过用户键入ctrl+C终止该shell程序,该怎么做?谢谢
|
哦,上面是内核态程序,用户程序是这样的:
#include
static void catch_sigint( int );
int main( int argc, char** argv ) {
struct sigaction sa_old;
struct sigaction sa_new;
// set up signal handling
sa_new.sa_handler = catch_sigint;
sigemptyset(&sa_new.sa_mask);
sa_new.sa_flags = 0;
sigaction(SIGINT, &sa_new, &sa_old );
/* continue */
while(1) {
/* process loop that goes on forever */
}
}
static void catch_sigint( int signo ) {
printf("Catch Sigintn");
exit(1);
}
#include
static void catch_sigint( int );
int main( int argc, char** argv ) {
struct sigaction sa_old;
struct sigaction sa_new;
// set up signal handling
sa_new.sa_handler = catch_sigint;
sigemptyset(&sa_new.sa_mask);
sa_new.sa_flags = 0;
sigaction(SIGINT, &sa_new, &sa_old );
/* continue */
while(1) {
/* process loop that goes on forever */
}
}
static void catch_sigint( int signo ) {
printf("Catch Sigintn");
exit(1);
}