码迷,mamicode.com
首页 > 系统相关 > 详细

Linux Signal Demo

时间:2015-08-07 15:57:37      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

#include <signal.h>

#include <sys/types.h>

#include <unistd.h>

void new_op(int,siginfo_t*,void*);

int main(int argc,char**argv)

{

       struct sigaction act;

       union sigval mysigval;

       int i;

       int sig;

       pid_t pid;

       char data[10];

       memset(data,0,sizeof(data));

       for(i=0;i < 5;i++)

              data[i]=‘2‘;

       mysigval.sival_ptr=data;

       sig=atoi(argv[1]);

       pid=getpid();



       sigemptyset(&act.sa_mask);

       act.sa_sigaction=new_op;

       act.sa_flags=SA_SIGINFO;

       if(sigaction(sig,&act,NULL) < 0)

       {

              printf("install sigal error\n");

       }

       while(1)

       {

              sleep(2);

              printf("wait for the signal\n");

              sigqueue(pid,sig,mysigval);
       }

}

void new_op(int signum,siginfo_t *info,void *myact)

{

       int i;

       for(i=0;i<10;i++)

       {

              printf("%c\n ",(*( (char*)((*info).si_ptr)+i)));

       }

       printf("handle signal %d over;",signum);

}



 

 

#include <unistd.h>
#include <signal.h>
#include <sys/types.h>

void new_op(int, siginfo_t*, void*);

int main(int argc, char **argv)
{
    struct sigaction act;
    int              sig;

    sig = atoi(argv[1]);

    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;
    act.sa_sigaction = new_op;

    if (sigaction(sig,&act,NULL) < 0)
    {
        printf("install sigal error\n");
    }

    while (1)
    {
        sleep(2);

        printf("wait for the signal\n");

    }
}

void new_op(int signum,siginfo_t *info,void *myact)

{

       printf("receive signal %d", signum);

       sleep(5);

}

 

 

#include "signal.h"

#include "unistd.h"

static void my_op(int);

int main(int argc, char **argv)

{

       sigset_t new_mask,old_mask,pending_mask;

       struct sigaction act;

       sigemptyset(&act.sa_mask);

       act.sa_flags=SA_SIGINFO;

       act.sa_sigaction=(void*)my_op;

       if(sigaction(SIGRTMIN+10,&act,NULL))

              printf("install signal SIGRTMIN+10 error\n");

       sigemptyset(&new_mask);

       sigaddset(&new_mask,SIGRTMIN+10);

       if(sigprocmask(SIG_BLOCK, &new_mask,&old_mask))

              printf("block signal SIGRTMIN+10 error\n");

       sleep(10);

       printf("now begin to get pending mask and unblock SIGRTMIN+10\n");

       if(sigpending(&pending_mask)<0)

              printf("get pending mask error\n");

       if(sigismember(&pending_mask,SIGRTMIN+10))

              printf("signal SIGRTMIN+10 is pending\n");

       if(sigprocmask(SIG_SETMASK,&old_mask,NULL)<0)

              printf("unblock signal error\n");

       printf("signal unblocked\n");

       sleep(10);

}

static void my_op(int signum)

{

       printf("receive signal %d \n",signum);

}

Linux Signal Demo

标签:

原文地址:http://www.cnblogs.com/qingxueyunfeng/p/4711004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!