linux 信号

参考链接:  

信号内容具体分析:   http://www.cnblogs.com/hoys/archive/2012/08/19/2646377.html

信号的例子介绍:  http://blog.csdn.net/jnu_simba/article/details/8947652

 

 

 信号值位于SIGRTMIN和SIGRTMAX为可靠信号,也为实时信号,支持排队操作, 其余为非可靠信号。

 

信号的简单例子:

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>



void SigSendTestMail(int signo)
{
   printf("get signal:%d\n", signo);
}



int main(int argc, char const *argv[])
{
    
    printf("SIGRTMIN val:%d\n", SIGRTMIN);
    struct sigaction          act;
    act.sa_handler = SigSendTestMail;
    act.sa_flags   = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGRTMIN+10, &act, 0);



    act.sa_handler = SigSendTestMail;
    act.sa_flags   = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGRTMIN+11, &act, 0);



    act.sa_handler = SigSendTestMail;
    act.sa_flags   = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGRTMIN+12, &act, 0);




    while(1) {
        sleep(3);
    }

    return 0;
}
   

sigqueue结构体的使用:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

void sighandler(int signo, siginfo_t *info,void *ctx);
//给自身传递信息
int main(void)
{

    struct sigaction act;
    act.sa_sigaction = sighandler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;//信息传递开关
    if(sigaction(42,&act,NULL) == -1){
        perror("sigaction error");
        exit(EXIT_FAILURE);
    }
    sleep(2);
    union sigval mysigval;
    mysigval.sival_int = 100;
    if(sigqueue(getpid(),42,mysigval) == -1){
        perror("sigqueue error");
        exit(EXIT_FAILURE);
    }
    return 0;
}

void sighandler(int signo, siginfo_t *info,void *ctx)
{
    //以下两种方式都能获得sigqueue发来的数据
    printf("receive the data from siqueue by info->si_int is %d\n",info->si_int);
    printf("receive the data from siqueue by info->si_value.sival_int is %d\n",info->si_value.sival_int);

}

 

  

posted @ 2017-09-25 11:46  cogitoergosum  阅读(155)  评论(0编辑  收藏  举报