信号

 

void (*signal(int signum, void (*func)(int)))(int)

使用sighandler_t容易造成名字空间冲突,所以还是使用上述定义方式;

ctrl+c 等于 SIGINT 终端中断符

忽略信号

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

int main()
{
    int i = 0;

    //signal (SIGINT, SIG_IGN);//忽略Ctrl+C

    for (i = 0; i < 10; i++)
    {
        printf ("*");
        sleep(1);          
    }

   exit (0);
}

为信号注册动作

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

void sig_handler(int s)
{
    printf ("SIGINT-handler %d\n", s);
}

int main()
{
    int i = 0;

    signal (SIGINT, sig_handler);

    getchar();

   exit (0);
}

信号会打断阻塞的系统调用,比如sleep时如果有信号到来可以打断sleep,如果打开一些比较慢的设备时就可能被信号打断

防止打开慢速设备时被信号打断
do
{
    fd = open(argv[1], O_RDONLY);
    if (fd < 0)
    {
        if (errno != EINTR)//等待过程中被信号打断就返回EINTR
        {
            perror ("open()");
            exit (1);
        }
    }
}while(fd < 0);

可重入

alarm不能累加会以最后一个为准

int main()
{
    alarm(10);
    alarm(1);
    alarm(5);//代码执行结果是5s后退出

   exit (0);
}

 

fork   exec  wait

 

 

exec()族之前必须要fflush(NULL)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值