📜  C++中的raise()函数

📅  最后修改于: 2021-05-25 21:49:55             🧑  作者: Mango

csignal头文件声明了函数raise()以处理特定信号。 Signal在程序中学习了一些异常行为,并调用了信号处理程序。它用于检查是否将调用默认处理程序或将其忽略。

句法:

int raise ( int signal_ )

参数:该函数接受单个参数sig ,该参数指定人为引发的信号。它可以接受任何6 C标准信号。
定义的信号类型

  • SIGILL
  • SIGINT
  • 西格斯
  • SIGTERM
  • SIGABRT
  • 信息系统

返回值:返回非零值,信号中无错误,否则返回零。该函数返回具有不同定义信号的不同非零元素。

下面的程序说明了上述方法:
程序1:

// C++ program to illustrate the
// raise() function when SIGABRT is passed
#include 
#include 
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGABRT, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGABRT);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}
输出:
Before called Signal = 0
After called Signal = 6

程式2:

// C++ program to illustrate the
// raise() function when SIGINT is passed
#include 
#include 
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGINT, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGINT);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}
输出:
Before called Signal = 0
After called Signal = 2

程序3:

// C++ program to illustrate the
// raise() function when SIGTERM is passed
#include 
#include 
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGTERM, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGTERM);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}
输出:
Before called Signal = 0
After called Signal = 15

计划4:

// C++ program to illustrate the
// raise() function when SIGSEGV is passed
#include 
#include 
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGSEGV, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGSEGV);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}
输出:
Before called Signal = 0
After called Signal = 11

计划5:

// C++ program to illustrate the
// raise() function when SIGFPE is passed
#include 
#include 
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGFPE, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGFPE);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}
输出:
Before called Signal = 0
After called Signal = 8
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”