C++ | C++ 信号处理

C++ | C++ 信号处理

C++ 信号处理

信号是由操作系统传给进程的中断,会提早终止一个程序。在 UNIXLINUXMac OS XWindows 系统上,可以通过按 Ctrl+C产生中断。

有些信号不能被程序捕获,但是下表所列信号可以在程序中捕获并可以基于信号采取适当的动作

这些信号是定义在 C++ 头文件 <csignal> 中。

信号描述
SIGABRT程序的异常终止,如调用 abort
SIGFPE错误的算术运算,比如除以零或导致溢出的操作。
SIGILL检测非法指令。
SIGINT程序终止(interrupt)信号。
SIGSEGV非法访问内存。
SIGTERM发送到程序的终止请求。

signal() 函数

C++ 信号处理库提供了 signal 函数,用来捕获突发事件。

SIGNAL(2)                                      Linux Programmer's Manual                                     SIGNAL(2)

NAME
       signal - ANSI C signal handling

SYNOPSIS
       #include <signal.h>

       typedef void (*sighandler_t)(int);

       sighandler_t signal(int signum, sighandler_t handler);

void (*signal (int sig, void (*func)(int)))(int); 

signal(registered signal, signal handler)

实例1:

/*******************************************************************
 *   > File Name: signal.cpp
 *   > Create Time: 2021年09月21日 12:38:10
 ******************************************************************/
#include <iostream>
#include <csignal>
#include <unistd.h>
using namespace std;

void signalHandler(int signum)
{
    cout << "Interrupt signal :" << signum << ", received.\n" ;

    exit(signum);
}

int main(int argc, char* argv[])
{
    static unsigned int count = 0;
    signal(SIGINT, signalHandler); /* 注册信号和信号处理程序 */

    for(;;){
        cout << "Going to sleep " << count << "..." << endl;
        sleep(1);
        count ++;
    }

    return 0;
}

编译、运行(使用Ctrl + C结束运行):

PS D:\study\cplusplus\day8> make
g++ -o signal signal.cpp -g -Wall
PS D:\study\cplusplus\day8> .\signal.exe
Going to sleep 0...
Going to sleep 1...
Going to sleep 2...
Going to sleep 3...
Interrupt signal :2, received.

raise() 函数

使用函数 raise() 生成信号。

RAISE(3)                                       Linux Programmer's Manual                                      RAISE(3)

NAME
       raise - send a signal to the caller

SYNOPSIS
       #include <signal.h>

       int raise(int sig);

DESCRIPTION
       The  raise()  function  sends  a  signal  to the calling process or thread.  In a single-threaded program it is
       equivalent to

           kill(getpid(), sig);

       In a multithreaded program it is equivalent to

           pthread_kill(pthread_self(), sig);

       If the signal causes a handler to be called, raise() will return only after the signal handler has returned.

RETURN VALUE
       raise() returns 0 on success, and nonzero for failure.

实例2:

/*******************************************************************
 *   > File Name: raise.cpp
 *   > Author: fly
 *   > Mail: 1358326274@qq.com
 *   > Create Time: 2021年09月21日 12:48:24
 ******************************************************************/

#include <iostream>
#include <csignal>
#include <unistd.h>
using namespace std;

void signalHandler(int signum)
{
    cout << "Interrupt signal (" << signum << ") recevied.\n";
    exit(signum);
}

int main(int argc, char* argv[])
{
    int i = 0;
    signal(SIGINT, signalHandler);

    while(++i){
        cout << "Going to sleep "<< i <<"..." << endl;
        if(i == 3){
            raise(SIGINT);
        }
        sleep(1);
    }

    return 0;
}

编译、运行:

PS D:\study\cplusplus\day8> make
g++ -o raise raise.cpp -g -Wall
PS D:\study\cplusplus\day8> .\raise.exe
Going to sleep 1...
Going to sleep 2...
Going to sleep 3...
Interrupt signal (2) recevied.

笔记:

Sleep 函数

功能:执行挂起一段时间,也就是等待一段时间在继续执行

  • (1)Sleep区分大小写的,有的编译器是大写,有的是小写。
  • (2)Sleep 括号里的时间,在 Windows 下是以毫秒为单位,而 Linux 是以为单位。
  • (3)Linux#include <unistd.h>sleep()Windos#include <windows.h>Sleep()

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值