From 19b615ef3329d0a67160e2ea603a0563370e3651 Mon Sep 17 00:00:00 2001 From: adminPyf <3043130461@qq.com> Date: Wed, 9 Jun 2021 22:10:57 +0800 Subject: [PATCH] =?UTF-8?q?linux=20=E4=BF=A1=E5=8F=B7=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_signal/makefile | 2 ++ test_signal/test_signal.cpp | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test_signal/makefile create mode 100644 test_signal/test_signal.cpp diff --git a/test_signal/makefile b/test_signal/makefile new file mode 100644 index 0000000..d3db79a --- /dev/null +++ b/test_signal/makefile @@ -0,0 +1,2 @@ +test_signal:test_signal.cpp + g++ $^ -o $@ -levent \ No newline at end of file diff --git a/test_signal/test_signal.cpp b/test_signal/test_signal.cpp new file mode 100644 index 0000000..5b6404a --- /dev/null +++ b/test_signal/test_signal.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; + +//sock 文件描述符 which 事件类型 arg传递的参数 +static void Ctrl_C(int sock,short which , void* arg){ + cout << "Ctrl_C" << endl; +} + +static void Kill(int sock,short which , void* arg){ + cout << "Kill" << endl; + + event *ev = (event*)arg; + //如果处于非待决 + if(!evsignal_pending(ev,NULL)){ + event_del(ev); + event_add(ev,NULL); //再次添加进来 + } +} + +int main(int agrc,char** agrv){ + + event_base *base = event_base_new(); + + //添加信号 ctrl+c ,处于no pending 状态 + //evsignal_new 隐藏的状态 EV_SIGNAL|EV_PERSIST + event *csig = evsignal_new(base,SIGINT,Ctrl_C,base); + + if(!csig){ + cerr << "SIGNAL evsignal_new failed!" << endl; + return -1; + } + + if (event_add(csig,0) != 0){ + cerr << "SIGANL event_add failed" << endl; + } + + //添加kill信号 + //非持久事件,只进入一次 event_self_cbarg()传递当前的event + event *ksig = event_new(base,SIGTERM,EV_SIGNAL,Kill, + event_self_cbarg()); + + if(!ksig){ + cerr << "SIGNAL evsignal_new failed!" << endl; + return -1; + } + + if (event_add(ksig,0) != 0){ + cerr << "SIGANL event_add failed" << endl; + } + //进入事件主循环 + event_base_dispatch(base); + event_free(csig); + event_base_free(base); + + return 0; + +} \ No newline at end of file