Linux信号处理

Linux信号处理函数详解 一下内容引述《Linux/Unix系统编程详解》 设计信号处理函数 信号处理函数设计得越简单越好,这样降低引发竞争条件的风险。 两种常见设计 信号处理函数设置全局性标志变量并退出,主程序对此标记进行周期性检查,一旦置位随机采取相应动作; 信号处理器函数执行某种类型的清理动作,接着终止进程或者使用非本地跳转将栈解开并将控制返回到主程序中的预定位置; 可重入函数与异步信号安全函数 可重入函数 首先区分单线程程序和多线程程序。典型的UNIX程序都具有一条执行线程,贯穿程序始终,CPU围绕单条执行逻辑来处理指令 阅读详情

大家应该都了解当我们在Linux下面运行程序时,如果程序跑飞了,那我们可以通过Ctrl-C来终止掉跑飞的程序,其实,当我们按下Ctrl-C时,涉及到了信号的相关处理工作,所以,今天我们就来谈一谈Ctrl-C被后的信号处理吧。

什么是信号

信号其实是一种软件中断,它为程序提供了一种处理异步事件的方法,所谓的异步事件就是事件的发生在任何事件都有可能,很多重要的程序都需要对信号进行处理。在Linux中一共有多少信号呢?我们可以通过在shell下运行kill -l指令,来查看Linux下的信号数目和他们对应的编号。如下图:(信号)

其中,我们把编号为1~31的信号称为普通信号,把34~64的信号称为实时信号。所有的信号名在头文件signal.h,中都被定义为正整数常量,即他们的信号编号。


信号的产生方式

在Linux下,信号可以通过一下几种方式来产生:

  1. 当用户按某些终端键时,引发终端产生的信号,例如我们前面提到的Ctrl-C按键对应的是SIGINT信号,Ctrl-\按键对应的是SIGQUIT信号。
  2. 硬件异常产生信号,例如除数为0、无效的内存引用对应的SIGSEGV信号,硬件异常产生信号不同,一旦硬件异常产生,那么他会一直存在,直到程序被终止位置,所以处理硬件异常信号一般都采用终止程序的方法。
  3. 进程调用kill(系统调用)函数可将任意信号发送给另一个进程或者进程组;(但是这条是有限制的,例如:要求接收信号和发送信号的进程的所有者必须相同,或者发送信号的所有者必须是超级用户)。
  4. 进程可以通过在shell下运行kill指令来对某个进程发出信号,kill指令是kill系统调用的一个接口。
  5. 当检测到某些软件条件时,也会产生信号。

信号的处理方式

有了信号的产生,当然也就有信号的处理,在Linux中,对信号的处理有下面的几种方式:
1. 忽略此信号,大多数信号都可以采用这种方式进行处理,除了9) SIGKILL 和19) SIGSTOP 信号。因为这两种信号都直接向内核提供了进程终止和停止的可靠办法。(SIGKILL),还有硬件异常信号我们最好不要忽略,因为硬件异常一旦产生如果不进行处理就会一直存在。
2. 捕捉信号。 进程要通知内核在某种信号产生时,需要调用一个用户函数,在用户函数中,用户可以自己定义信号处理的方式,在Linux下我们不能捕捉SIGKILL信号和SIGSTOP信号。
3. 执行系统的默认动作。


终止+core

在系统默认动作中,有一种动作叫做“终止+core”,它表示在进程当前工作目录中的core文件中复制了该进程当前的内存映像,(该文件名为:core.进程pid号),很多信号都使用了这种处理方式,例如SIGFPE信号。当产生了内存映像文件,我们可以在gdb下面进行调试。如下图:(core)

其中ulimit -c + number是指定core文件的大小上限,
ulimit -a是查看当前系统中的各个资源上限。


相关信号处理函数

  1. signal函数(指定信号的处理方式)
       #include <signal.h>

       typedef void (*sighandler_t)(int);

       sighandler_t signal(int signum, sighandler_t handler);

signum:参数为信号名,或者信号编号。
handler:为指向返回值为void参数为int的函数指针,或者是SIG_IGN或SIG_DFL宏定义。

在signal.h头文件中,上面的常量被定义如下
#define SIG_ERR (void(*)()) -1
#define SIG_DFL (void(*)()) 0
#define SIG_IGN (void(*)()) 1

其中,SIG_DFL指定信号处理的方式为默认方式,SIG_IGN指定信号处理的方式为忽略。

  1. alarm函数
       #include <unistd.h>

       unsigned int alarm(unsigned int seconds);

alarm函数相当于一个闹钟,它可以为进程注册闹钟时间,例如使用alarm(5)可以为进程注册5秒钟的闹钟时间,5秒后会产生SIGALRM信号;
如果在调用alarm函数时,之前已经为该进程注册的闹钟时间还没有超时,则该闹钟时间的余留值作为本次alarm函数调用的值返回。以前注册的闹钟时间则被新值所取代;
使用alarm(0)可以取消以前所注册的闹钟,并返回之前注

linux信号处理函数的可重入性 在信号处理函数中,尽量使用可重入的函数接口(即操作的数据都是在堆栈上,不涉及全局变量),否则会引起数据的崩溃。一个简单的演示程序可以说明这点。 #include #include int a=0; void  handleSig5(int num, siginfo_t * info, void * no_used) { a = 5; printf("before 阅读详情

相关推荐

linux0.11信号处理

linux中的信号处理函数提供了对程序异步处理的功能,比如键盘按下ctrl+c键,该操作将产生一个SIGINT信号,并被发送到当前前台的进程中。内核采用一个无符号长整形变量中(32bit)的每一个比特位来表示不同的信号,因此最多可以定义32种信号。对于一个进程来说,当收到一个信号时,有3种不同的处理方式: 1. 忽略该信号。其中SIGKILL和SIGSTOP不能忽略。 2. 捕获该信号。调用s

FK的专栏 1367

深入浅出一文搞懂Raft协议

前言 在分布式的世界里,要说最核心最复杂的功能,一致性的实现无出其右,之前的paxos算法堪称经典,被认为是同类算法中效果最好的,基本上成为分布式一致性的代名词,但是paxos算法也是出了名的难理解,而且相当不好实现。本人也花了很多时间、看了很多材料也没有真正理解。所以基于paxos的思想进行的一致性算法的简化和实现就成为了现实的需求,在此背景下,本文的主角Raft就出现了。 Raft算法的头号目标就是容易理解(UnderStandable),这从论文中就可以看出来。当然,R...

microGP的专栏 6485

FPGA网络调试实战:基于88E1111和DP83867两款PHY芯片的SGMII-UDP数据回环对比与避坑指南

本文深入对比了88E1111与DP83867两款PHY芯片在SGMII-UDP数据回环中的性能差异,涵盖硬件设计、Vivado工程配置及调试技巧。通过实测数据揭示DP83867在吞吐量、延迟和功耗方面的优势,并提供FPGA网络调试中的常见问题解决方案,助力工程师高效完成项目开发。

weixin_31682031的博客 384

Win10系统蓝屏错误BAD SYSTEM CONFIG INFO的解决方案

BAD SYSTEM CONFIG INFO错误是Win10系统中较为常见的一种蓝屏错误,通常与系统配置文件损坏或丢失有关。本文介绍了三种有效的解决方法:使用系统修复高级选项修复、通过PE修复文件和重装新系统。用户可以根据实际情况选择合适的方法进行操作。同时,建议用户定期备份系统重要文件和数据,以防止类似问题的发生。在遇到问题时,保持冷静并耐心操作,通常可以顺利解决问题。

mmoo_python的博客 7723

全志 T527_Android13 编译 [ 避坑必看 ]

Swap 概念: Linux中Swap(即:交换分区),类似于Windows的虚拟内存,就是当内存不足的时候,把一部分硬盘空间虚拟成内存使用,从而解决内存容量不足的情况。它的功能就是在内存不够的情况下,操作系统先把内存中暂时不用的数据,存到硬盘的交换空间,腾出内存来让别的程序运行。当输入编译命令make -j16 的时候,会有如下提示,这是官方建议通过减少编译线程来解决编译时消耗内存过大的问题。Android 13 系统的编译流程,编译一共分为两部分:内核编译与 Android 编译。

公众号:嵌入式分享 2111

9.9元ESP32-C3开发板实战:手把手教你用VSCode搭建RT-Thread最小系统(附避坑指南)

本文提供了一份详尽的实战指南,教你如何在售价仅9.9元的ESP32-C3开发板上,使用VSCode搭建RT-Thread最小系统。内容涵盖从ESP-IDF环境配置、BSP源码获取、内核补丁应用到编译下载与调试的全流程,并重点分享了版本选择、补丁应用等关键避坑技巧,旨在帮助开发者低成本、高效地启动嵌入式实时操作系统开发。

o4p5q6r7s的博客 226

芯片封装、功率密度提升及热管理系统优化三个维度分析高功率芯片散热技术挑战

本文探讨高功率芯片散热技术挑战与创新。随着新能源汽车MOSFET用量增至400颗/车,文章聚焦封装技术创新(双面散热、集成模块)、功率密度提升(SGT-MOSFET优化)和热管理系统协同优化三大方向。通过热仿真案例(如CPO散热优化)展示温升改善方案,强调需协同器件级设计、系统级仿真与先进封装技术(混合键合/玻璃基板)来解决高密度互连与散热矛盾,为功率密度跃升提供技术支撑。

莱歌数字的博客 1473

群晖NAS硬盘挂了别慌!用Ubuntu 18外接USB保姆级数据抢救指南

本文提供群晖NAS硬盘故障时的数据抢救指南,详细介绍了使用Ubuntu 18外接USB硬盘盒的完整流程。从构建救援环境、硬盘识别到数据迁移策略,涵盖关键步骤和实用技巧,帮助用户零成本恢复重要数据。特别适合非RAID配置的单盘故障场景。

9o8p7i6u5y的博客 1146

【skill】漫剧 AI 视频创作的知识库 + API 调用手册

方案适用场景在对话中使用,自动触发注入到任何 LLM(Cursor/Windsurf/Claude API)Python 模块集成到自动化管线/批量生成GitHub 又限速了,不过前几轮搜到的已经够用了。根据搜索结果,结合已分析过的项目,给你整理完整对比 🐮。

突围 984

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

上一篇: 例说读者写者模型
下一篇: Linux作业控制
_stark
博客等级 码龄11年 175粉丝 89原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值