linux SA_RESTART的问题

信号是异步的,它会在程序的任何地方发生。由此程序正常的执行路径被打破,去执行信号处理函数。一般情况下进程正在执行某个系统调用,那么在该系统调用返回前信号是不会被递送的。但慢速系统调用除外如读写终端、网络、磁盘,以及wait和pause。这些系统调用都会返回-1,errno置为EINTR。当系统调用被中断时,我们可以选择使用循环再次调用,或者设置重新启动该系统调用(SA_RESTART)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>

void int_handler (int signum)
{
        printf ("int handler %d\n",signum);
}

int main(int argc, char **argv)
{
        char buf[100];
        ssize_t ret;
        struct sigaction oldact;
        struct sigaction act;

        act.sa_handler = int_handler;
        act.sa_flags=0;
        act.sa_flags |= SA_RESTART;
        sigemptyset(&act.sa_mask);
        if (-1 == sigaction(SIGINT,&act,&oldact))
        {
                printf("sigaction failed!\n");
                return -1;
        }

        bzero(buf,100);

        ret = read(STDIN_FILENO,buf,10);
        if (ret == -1)
        {
                printf ("read error %s\n", strerror(errno));

        }
        printf ("read %d bytes, content is %s\n",ret,buf);
        sleep (10);
        return 0;
}


^Cint handler 2
^Cint handler 2
^Cint handler 2
^Cint handler 2
^Cint handler 2
^Cint handler 2
hgfd
read 5 bytes, content is hgfd


把程序不用SA_RESTART

结果

^Cint handler 2
read error Interrupted system call
read -1 bytes, content is
程序立即结束啦。


http://blog.csdn.net/ccccdddxxx/article/details/6308381

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值