Linux 信号详解三(sleep,raise)

sleep()函数
1.sleep()函数作用:让进程睡眠
2.能被未忽略的信号打断,然后处理信号函数以后,就不再睡眠,直接向下执行代码
3.sleep函数的返回值是剩余秒数
//sleep 函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

void catch_signal(int sign)
{
    switch(sign)
    {
    case SIGINT:
        printf("accept signal!\n");
        break;
    default:
        break;
    }
}

int main(int arg,char *args[])
{
    //注册信号
    if(signal(SIGINT,catch_signal)==SIG_ERR)
    {
        perror("signal error");
        return-1;
    }
    int num=0;
    num=sleep(100);
    //手动执行 ctrl+C  进程被唤醒
    if(num>0)
    {
        printf("sleep()函数被打算睡眠,醒过来了!\n");
    }
    //说明:sleep()函数是可中断睡眠

    printf("新的测试!\n");
    //再次设计sleep()函数,让其sleep需要的时间
    num=15;
    do{
        num=sleep(num);
        printf("被唤醒了,但是还要继续睡眠!剩余时间%d\n",num);
    }while(num);


    printf("game over!\n");
    return 0;
}

 

int raise(int sig)
--给自己发送信号,raise(sig)等价于kill(getpid(),sig)
killpg
--给进程组发送信号,killpg(pgrp,sig)等价于kill(-pgrp,sig)
sigqueue
--给进程发送信号,支持排队,可以附带信息

 

sleep() makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which is not ignored.
sleep使线程沉睡,直到X秒——或者有一个未被忽略的信号到达

sleep() may be implemented using SIGALRM; mixing calls to alarm(2) and sleep() is a bad idea.
sllep函数可能是用SIGALRM来实现的,混合调用alarm和sleep可不是个好主意。

Using longjmp(3) from a signal handler or modifying the handling of SIGALRM while sleeping will cause undefined results.
在sleep期间修改SIGALRM的处理会导致未知错误

posted on 2016-11-19 11:53  寒魔影  阅读(2735)  评论(0编辑  收藏  举报

导航