当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ raise()用法及代码示例



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

用法:

int raise ( int signal_ )

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


  • SIGILL
  • SIGINT
  • SIGSEGV
  • SIGTERM
  • SIGABRT
  • SIGFPE

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

以下示例程序旨在说明上述方法:
程序1:

// C++ program to illustrate the 
// raise() function when SIGABRT is passed 
#include <csignal> 
#include <iostream> 
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 <csignal> 
#include <iostream> 
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 <csignal> 
#include <iostream> 
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 <csignal> 
#include <iostream> 
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 <csignal> 
#include <iostream> 
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


相关用法


注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 raise() function in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。