You are not logged in.

#1 2016-06-14 07:11:07

leetow2006
Member
Registered: 2013-02-13
Posts: 21

Ask a question about signal

I am sorry my English is poor.
Look:
<b>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

void printsigset(sigset_t *set)
{
    int i;
    for(i=1;i<NSIG;i++)
    {
        if(sigismember(set,i))
            putchar('1');
        else
            putchar('0');
    }
    printf("\n");
}

void handler(int);

int main()
{
    sigset_t pset;
    sigset_t bset;

    sigemptyset(&pset);
    sigemptyset(&bset);

    signal(SIGINT,handler);
    signal(SIGQUIT,handler);

    sigaddset(&bset,SIGINT);
    sigprocmask(SIG_BLOCK,&bset,NULL);

    for(;;)
    {
        sigpending(&pset);
        printsigset(&pset);
        sleep(1);
    }
   
    return 0;
}

void handler(int sig)
{
    if(sig==SIGINT)
        printf("recv sig=%d\n",sig);
    else if(sig==SIGQUIT)
    {   
        sigset_t uset;
        sigemptyset(&uset);
        sigaddset(&uset,SIGINT);
        sigprocmask(SIG_UNBLOCK,&uset,NULL);
    }
}
</b>
The first I press ctrl+c,the second bit of pending word is 1;the second
I press ctrl+\,the second bit of pending word is 0;but the third time
I press ctrl+c the second bit of pending word is 1.But the third time
the codes run in for(;;) statements,and it does not run
<b>
sigaddset(&bset,SIGINT);
sigprocmask(SIG_BLOCK,&bset,NULL);
</b>
why the second bit of pendingk word is 1?

Offline

#2 2016-06-14 07:16:45

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Ask a question about signal

Please use code tags when pasting to the boards: https://wiki.archlinux.org/index.php/Co … s_and_code


Moving to Programming & Scripting...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2016-06-14 11:28:34

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: Ask a question about signal

You have too many sigset_t's for what you are trying to do...
I don't know why you swap between SET's like you do.

In printsigset try a printf instead of putchar to see what's happening

if(sigismember(set,i))
    printf("%d", i);

it's the same value you get printed out in handler.
If you use puts at the start of your for loop you'll see it runs straight away

for(;;)
    puts("looping");
    ...

HTH


You're just jealous because the voices only talk to me.

Offline

Board footer

Powered by FluxBB