linux系统编程之信号,linux系统编程之信号(三):信号安装、signal、kill,arise讲解...

一,信号安装

如果进程要处理某一信号,那么就要在进程中安装该信号。安装信号主要用来确定信号值及进程针对该信号值的动作之间的映射关系,即进程将要处理哪个信号;该信号被传递给进程时,将执行何种操作。

linux主要有两个函数实现信号的安装:signal()、sigaction()。其中signal()只有两个参数,不支持信号传递信息,主要是用于前32种非实时信号的安装;而sigaction()是较新的函数(由两个系统调用实现:sys_signal以及sys_rt_sigaction),有三个参数,支持信号传递信息,主要用来与 sigqueue() 系统调用配合使用,当然,sigaction()同样支持非实时信号的安装。sigaction()优于signal()主要体现在支持信号带有参数。

二,signal()用法

#include

typedef void (*__sighandler_t) (int);

#define SIG_ERR ((__sighandler_t) -1)

#define SIG_DFL ((__sighandler_t) 0)

#define SIG_IGN ((__sighandler_t) 1)

void (*signal(int signum, void (*handler))(int)))(int);

如果该函数原型不容易理解的话,可以参考下面的分解方式来理解:

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler));

第一个参数指定信号的值,第二个参数指定针对前面信号值的处理,可以忽略该信号(参数设为SIG_IGN);可以采用系统默认方式处理信号(参数设为SIG_DFL);也可以自己实现处理方式(参数指定一个函数地址)。

如果signal()调用成功,返回最后一次也就是上一次为安装信号signum而调用signal()时的handler值;失败则返回SIG_ERR。

传递给信号处理例程的整数参数是信号值,这样可以使得一个信号处理例程处理多个信号。

man帮助说明:

DESCRIPTION

The behavior of signal() varies across Unix versions, and has also var-

ied historically across different versions of Linux.   Avoid  its  use:

use sigaction(2) instead.  See Portability below.

signal() sets the disposition of the signal signum to handler, which is

either SIG_IGN, SIG_DFL, or the address of a  programmer-defined  func-

tion (a "signal handler").

If  the signal signum is delivered to the process, then one of the fol-

lowing happens:

*  If the disposition is set to SIG_IGN, then the signal is ignored.

*  If the disposition is set to SIG_DFL, then the default action  asso-

ciated with the signal (see signal(7)) occurs.

*  If  the disposition is set to a function, then first either the dis-

position is reset to SIG_DFL, or the signal is blocked  (see  Porta-

bility  below), and then handler is called with argument signum.  If

invocation of the handler caused the signal to be blocked, then  the

signal is unblocked upon return from the handler.

The signals SIGKILL and SIGSTOP cannot be caught or ignored.

RETURN VALUE

signal()  returns  the previous value of the signal handler, or SIG_ERR

on error.

示例程序:

#include

sig_handler(main((signal(SIGINT,sig_handler) ==(; ;);sig_handler(

结果:

447138.html

sig_handler(main(=(prehandler ==,(sig_handler(结果:

447138.html

sig_handler(main(=(prehandler ==(; ;);结果:

447138.html#include <.h>

ERR_EXIT(m) \

(handler(main(argc, *(signal(SIGUSR1, handler) ===(pid == -(pid == n = =(n > handler(

结果:

447138.html<.h>

ERR_EXIT(m) \

(handler(main(argc, *(signal(SIGUSR1, handler) ===(pid == -(pid == =-n = =(n > handler(结果:

447138.html

sig_handler(main((signal(SIGINT,sig_handler) ==sig_handler(结果:

9d42025dedddddec7e35c1c6f8dc2b64.png

可知两种方式都可以给自身发送信号。

http://www.dengb.com/Linuxjc/447138.htmlwww.dengb.comtruehttp://www.dengb.com/Linuxjc/447138.htmlTechArticle一,信号安装 如果进程要处理某一信号,那么就要在进程中安装该信号。安装信号主要用来确定信号值及进程针对该信号值的动作之间的映...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值