Linux捕获信号的方法
在Linux系统中,信号是一种用于通知进程发生了某个事件的机制,信号可以由内核发送给进程,也可以由进程之间发送,捕获信号是进程对外部事件的一种响应方式,通过捕获信号,进程可以在收到信号时执行特定的操作,本文将介绍Linux中捕获信号的方法以及相关的问题与解答。
1、使用信号处理函数
Linux系统中,每个进程都可以定义一个信号处理函数(signal handler),用于处理收到的信号,当进程收到信号时,会自动调用该信号处理函数,信号处理函数的原型如下:
include <signal.h> typedef void (*sighandler_t)(int);
int
参数表示收到的信号编号;sighandler_t
类型的函数指针表示信号处理函数,要捕获信号,需要在程序中注册信号处理函数,要捕获SIGINT信号(通常由Ctrl+C产生),可以使用以下代码:
include <stdio.h> include <signal.h> include <unistd.h> void signal_handler(int signum) { printf("接收到信号 %d ", signum); } int main() { signal(SIGINT, signal_handler); // 注册信号处理函数 while (1) { printf("等待信号... "); sleep(1); } return 0; }
2、使用sigaction系统调用
除了使用信号处理函数外,还可以使用sigaction
系统调用来注册信号处理函数。sigaction
的原型如下:
include <signal.h> int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
signum
参数表示要捕获的信号编号;act
指向一个sigaction
结构体,用于指定信号处理函数和附加选项;oldact
用于保存原来的信号处理函数(如果有的话),下面是一个使用sigaction
的例子:
include <stdio.h> include <signal.h> include <unistd.h> include <string.h> include <sys/time.h> include <sys/types.h> include <errno.h> include <stdlib.h> include <fcntl.h> include <pthread.h> include <semaphore.h> include <math.h> include <limits.h> include <sys/resource.h> include <sys/stat.h> include <sys/wait.h> include <assert.h> include <time.h> include <ucontext.h> include <bitset> include <vector> include <algorithm> include <map> include <set> include <queue> include <stack> include <iostream> include <sstream> include <iomanip> include <functional> include <numeric> include <cmath> include <complex> include <iterator> include <memory> include <utility> include <tuple> include <regex> include <locale> include <codecvt> include <thread> include <mutex> include <condition_variable> include <future> include <chrono> include <unordered_map> include <unordered_set> include <list> include <forward_list> include <array> define FOREACH(i, m) for (auto i = (m).begin(); i != (m).end(); ++i) template<typename T, size_t N, size_t M, bool Sorted = true, bool UseStl = true> struct STLWrapper{ typedef T value_type; typedef std::array<T, N > array_type; typedef std::vector<T, std::allocator<T>> vector_type; typedef std::list<T, std::allocator<T>> list_type; typedef std::deque<T, std::allocator<T>> deque_type; typedef std::forward_list<T, std::allocator<T>> forward_list_type; }; template<typename T, size_t N, bool Sorted = true, bool UseStl = true> struct STLWrapper<T[N], N, N, Sorted, UseStl> : public STLWrapper<T, N, N, Sorted, UseStl> {}; template<typename T, size_t N, bool Sorted = true, bool UseStl = true> struct STLWrapper<T[N], N, N, Sorted, false> : public STLWrapper<T*, N, N, Sorted, UseStl> {}; template<typename T, size_t N, bool Sorted = true> struct STLWrapper<T[N], N, Sorted> : public STLWrapper<T*, N, N, Sorted> {}; template<typename T, size_t N> struct STLWrapper<T[N], N> : public STLWrapper<T*, N> {}; template<typename T> struct STLWrapper<T[]> : public STLWrapper<T*> {}; template<typename T> struct STLWrapper<std::vector<T>> {}; template<typename T> struct STLWrapper<std::list<T>> {}; template<typename T> struct STLWrapper<std::deque<T>> {}; template<typename T> struct STLWrapper<std::forward_list<T>> {}; template<typename T> struct STLWrapper<const T[]> {}; template<typename T> struct STLWrapper<const std::vector<T>> {}; template<typename T> struct STLWrapper<const std::list<T>> {}; template<typename T> struct STLWrapper<const std::deque<T>> {}; template<typename T> struct STLWrapper<const std::forward_list<T>> {}; template<typename T> struct STLWrapper<const volatile T[]> {}; template<typename T> struct STLWrapper<const volatile std::vector<T>> {}; template<typename T> struct STLWrapper<const volatile std::list<T>> {}; template<typename T> struct STLWrapper<const volatile std::deque<T>> {}; template<typename T> struct STLWrapper<const volatile std::forward_list<T>> {}; dblrounder() { dblrounder(0);} dblrounder(unsigned int x) { dblrounder(x);} ostream &operator<<(ostream &os, const dblrounder &r) { sprintf(buffer, "%g", r.value); os << buffer; osflush(buffer); return os;} dblrounder& operator+=(dblrounder &a) { a.value += a.value >= 0.5 ? 0.5 : -0.5;return a; ostream &operator+=(ostream &os, const dblrounder &a) {return os << a;} dblrounder& operator-=(dblrounder &a) { a.value -= a.value >= 0.5 ? 0.5 : -0.5;return a; ostream &operator-=(ostream &os, const dblrounder &a) {return os << a;} dblrounder& operator*=(dblrounder &a) { a.value *= a.value >= 1 ? 10000000000000000032765463127458959121053859791337890625 : 0.0000000000000000032765463127458959121053859791337890625;return a; ostream &operator*=(ostream &os, const dblrounder &a) {return os << a;} dblrounder& operator/=(d
原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/132255.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复