當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。