signal阻塞, 挂起

signal阻塞, 挂起, 恢复

例1: 信号阻塞及阻塞解除

mysigprocmask.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>

void handler(int signum)
{
    static int count = 0;
    count++;
    printf("hello i'm signal %d,count = %d\n",signum,count);
}

void child_process(int signum)
{
    puts("i'm process my child");
    wait(NULL);
}

int main(int argc, char *argv[])
{
    int i;
    printf("pid = %d\n",getpid());

    for(i = 1;i <= 64;i++)
    {
        if(SIG_ERR == signal(i,handler))
        {
            printf("error = %d\n",i);
        }
    }

    sigset_t now, old;
    sigfillset(&now);
    sigprocmask(SIG_SETMASK,&now,&old);

    puts("all signal is blocked...");
    puts("please enter any key to unblocked...");
    getchar();

    sigprocmask(SIG_SETMASK,&old,NULL);

    getchar();

    return 0;
}

编译链接执行, 结果如下:

解除阻塞前, 多次改变窗口大小, 多次按下"ctrl+c", 按下回车后, 28和2各打印输出一次. 再次改变窗口大小, 或按下ctrl+c, 每次都有打印输出.

 

例2: 信号挂起, 取出挂起的信号

mysigpending.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>

#undef SIGINT
#define SIGINT 60

void handler(int signum)
{
    static int count = 0;
    count++;
    printf("hello i'm signal %d,count = %d\n", signum, count);
}


int main(int argc, char *argv[])
{
    int i;
    sigset_t new,old,pend;

    printf("pid = %d\n",getpid());
    signal(SIGINT,handler);
    puts("SIGINT is set...");
    getchar();

    sigemptyset(&new);
    sigaddset(&new,SIGINT);
    sigprocmask(SIG_BLOCK,&new,&old);
    puts("SIGINT is blocked...");
    getchar();

    sigpending(&new);
    if(sigismember(&new,SIGINT))
        puts("ISGINT is in pending queue...");
    else
        puts("SIGINT is not int pending queue..");
    sigprocmask(SIG_SETMASK,&old,NULL);
    puts("SIGINT is unblocked...");
    getchar();

    return 0;
}

编译链接执行后, 结果如下:

另一终端运行如下命令:

运行起来后,

 a. 在另一个终端运行"kill -60 9581"向其发信号, 每次都有响应信号. 

 b. 通过sigemptyset()/sigaddset()/sigprocmask()阻塞60信号. 此时在另一个终端运行"kill -60 9581"向其发信号, 没有响应. 

 c. sigpending()/sigismember()取得挂起的信号集合

 d. sigprocmask()解除60信号阻塞.  立即有打印输出.

 

例3: 暂停进程, 直到收到信号

mysigsuspend.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>

void handler(int signum)
{
    printf("signal: %d\n");
}

int main(int argc, char *argv[])
{
    printf("pid = %d\n",getpid());    
    
    signal(28,handler);

    sigset_t new,old;
    sigfillset(&new);
    sigprocmask(SIG_SETMASK,&new,&old);
    puts("BLOCKED..");

    sigsuspend(&old);
    printf("[%s - %d]info \n", __func__, __LINE__);
#if 0
    sigprocmask(SIG_SETMASK,&old,NULL);
    pause();

#endif
    return 0;
}

编译链接执行, 结果输出如下:

运行后程序暂停, 直到窗口发生变化(产生SIGWINCH信号), 或从其它终端向其发信号(如"kill -61 10672")时, 程序才退出.

posted @ 2016-01-05 19:52  zhanglong71  阅读(729)  评论(0编辑  收藏  举报