sigaction 和 signal 函数

1.sigaction 函数

NAME
       sigaction - examine and change a signal action

SYNOPSIS
       #include <signal.h>

       int sigaction(int sig,     const struct sigaction *  act,  struct sigaction * oact);

Parameters

sig
(Input) A signal from the list defined in ControlSignals Table .

*act
(Input) A pointer to the sigaction structure that describesthe action to be taken for the signal. Can be NULL.

If act is a NULL pointer, signal handling is unchanged. sigaction() can be used to inquire about the current handling ofsignalsig.

If act is not NULL, the action specified in the sigaction structure becomes the new action associated withsig.

*oact
(Output) A pointer to a storage location where sigaction()can store a sigaction structure. This structure contains theaction currently associated with sig. Can be NULL.

If oact is a NULL pointer, sigaction() does notstore this information.


structure sigaction:

Member TypeMember NameDescription
void(*) (int)sa_handlerPointer to a signal-catching function or one of the macros SIG_IGN or SIG_DFL.
sigset_tsa_maskAdditional set of signals to be blocked during execution of signal-catching function.
intsa_flagsSpecial flags to affect behaviour of signal.
void(*) (int, siginfo_t *, void *)sa_sigactionPointer to a signal-catching function.

代码说明:

#include "signal.h"
#include "stdio.h"
#include "unistd.h"


void fun_int()
{
        printf("\n*******fun_int catch SIGINT*****\n");
        printf("I'm sleeping......\n");
        sleep(10);
        printf("\n*********leave fun_int**********\n");
}

int main()
{
        struct sigaction new,old;
        new.sa_handler = &fun_int;
        sigfillset(&new.sa_mask);
        sigdelset(&new.sa_mask,SIGINT);
        sigaction(SIGINT,&new,NULL);
        printf("Initial finish, please send the signal\n");
        while(1);
}


结果显示1:

[elbort@elbort test1]$ ./test
Initial finish, please send the signal
^C                                                                //接收到SIGINT,调用相应的函数      
*******fun_int catch SIGINT*****
I'm sleeping......
^\^\^C^C                                                     //设置了阻塞,其他信号只会阻塞在执行函数里
*********leave fun_int**********

*******fun_int catch SIGINT*****           //退出信号执行函数,之前在执行函数中的信号被释放,首先对比注册登记信号函数,故虽然SIGQUIT首先接收,还是执行SIGINT
I'm sleeping......

*********leave fun_int**********           
退出(吐核)                                                //信号只会登记一次,重复无效,执行完SIGINT,现在轮到SIGQUIT
[elbort@elbort test1]$

结果显示2:

[elbort@elbort test1]$ ./test
Initial finish, please send the signal
^\退出(吐核)                                               //设置的阻塞只会在执行信号函数里
[elbort@elbort test1]$


2.signal函数

功能和sigaction类似,是sigaction的简化版,不能对执行函数运行时进行信号屏蔽。

代码说明:

#include "signal.h"
#include "stdio.h"
#include "unistd.h"

void fun_int()
{
        printf("\n*******fun_int catch SIGINT*****\n");
        printf("I'm sleeping......\n");
        sleep(10);
        printf("\n*********leave fun_int**********\n");
}


int main()
{
        signal(SIGINT,fun_int);
        printf("Initial finish, please send the signal\n");
        while(1);

}

结果显示:

[elbort@elbort test1]$ ./test
Initial finish, please send the signal
^C
*******fun_int catch SIGINT*****
I'm sleeping......
^C^C                                                                           //重复发送SIGINT信号,重复运行该执行函数(据说版本不同,这里现象不同,网上说有的重复调用会调用默认执行函数,在这则是终止该进程,但我的测试版本没出现这种情况)
*********leave fun_int**********

*******fun_int catch SIGINT*****
I'm sleeping......
^C^C^\退出(吐核)                                                    //接收到SIGQUIT直接退出
[elbort@elbort test1]$


个人总结:

        在运行上面代码,我发现当进程在调用接收到的signal函数时,向该进程再次发送同样的信号(在本例中该信号是SIGINT),该进程并不会立刻对该信号进行处理,只是将其进行压栈,等到信号函数完成后退出再检测是否调用该信号函数。而且在信号阻塞期间接收的各种信号,不论先后,当信号函数结束后,会优先检查信号注册函数表,查看是否有相匹配的信号处理,如有则调用该函数,如无则默认处理阻塞后被释放的信号(可看sigaction 结果显示1)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值