#include <csignal>
#include <iostream>
#include <set>
#include <string>
static volatile std::sig_atomic_t g_last_signal = 0;
static volatile std::sig_atomic_t g_hit_count = 0;
extern "C" void safe_signal_handler(int signum) {
g_last_signal = signum;
g_hit_count = g_hit_count + 1;
}
int main() {
// 1. Register SIGINT handler via std::signal and trigger via std::raise
using SigHandler = void (*)(int);
SigHandler prev_int = std::signal(SIGINT, safe_signal_handler);
int rc_int = std::raise(SIGINT);
int captured_int = static_cast<int>(g_last_signal);
int count_int = static_cast<int>(g_hit_count);
bool ok1 = (prev_int != SIG_ERR) && (rc_int == 0) &&
(captured_int == SIGINT) && (count_int == 1);
// 2. Register SIGTERM handler, raise, then restore SIG_DFL and verify returned handler pointer
g_last_signal = 0;
SigHandler prev_term_set = std::signal(SIGTERM, safe_signal_handler);
int rc_term = std::raise(SIGTERM);
int captured_term = static_cast<int>(g_last_signal);
SigHandler prev_term_restore = std::signal(SIGTERM, SIG_DFL);
bool ok2 = (prev_term_set != SIG_ERR) && (rc_term == 0) &&
(captured_term == SIGTERM) && (prev_term_restore == safe_signal_handler);
// 3. Ignore signal with SIG_IGN and confirm process continues cleanly
g_last_signal = 0;
std::signal(SIGINT, SIG_IGN);
int rc_ign = std::raise(SIGINT);
SigHandler prev_after_ign = std::signal(SIGINT, SIG_DFL);
bool ok3 = (rc_ign == 0) && (g_last_signal == 0) && (prev_after_ign == SIG_IGN);
// 4. Verify all 6 ISO C++ <csignal> constants exist and are distinct positive ints
std::set<int> sigs = {SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM};
bool all_pos = true;
for (int s : sigs) {
if (s <= 0) all_pos = false;
}
bool ok4 = (sigs.size() == 6) && all_pos;
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"signal_register_and_raise_sigint\", \"expected\": true, \"actual\": "
<< (ok1 ? "true" : "false") << ", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"signal_raise_sigterm_and_restore\", \"expected\": true, \"actual\": "
<< (ok2 ? "true" : "false") << ", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"signal_ignore_sig_ign\", \"expected\": true, \"actual\": "
<< (ok3 ? "true" : "false") << ", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"standard_csignal_constants\", \"expected\": true, \"actual\": "
<< (ok4 ? "true" : "false") << ", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
<< " ]\n"
<< "}\n";
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-signal-handling-lab/explore-cpp-signal-handling.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-signal-handling.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2393.zip