Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:37:27+00:00 2026-05-27T23:37:27+00:00

I am reading the APUE, Chapter 10. Here is my code. #include apue.h #include

  • 0

I am reading the APUE, Chapter 10. Here is my code.

#include "apue.h"
#include <unistd.h>
#include <setjmp.h>
#include <time.h>
#include <errno.h>

static void sig_usr1(int), sig_alrm(int);
static sigjmp_buf jmpbuf;
static volatile sig_atomic_t canjmp;

int 
main(void)
{
    if(signal(SIGUSR1, sig_usr1) == SIG_ERR)
        err_sys("signal(SIGUSR1) error");
    if(signal(SIGALRM, sig_alrm) == SIG_ERR)
        err_sys("signal(SIGALRM) error");
    //print signal.
    pr_mask("Starting main: ");
    if(sigsetjmp(jmpbuf, 1)) {
        pr_mask("End main: ");
        exit(0);
    }
    canjmp = 1;
    for(;;)
        pause();
}

static void
sig_usr1(int signo)
{
    time_t starttime;

    if(canjmp == 0) {
        return;
    }

    pr_mask("starting sig_usr1: ");
    alarm(3);
    starttime = time(NULL);
    for(;;) 
        if(time(NULL) > starttime + 5)
            break;
    pr_mask("finishing sig_usr1: ");
    canjmp = 0;
    siglongjmp(jmpbuf, 1);
}

static void
sig_alrm(int signo)
{
    pr_mask("in sig_arlm: ");
}

void
pr_mask(const char *str)
{
    sigset_t    sigset;
    int         errno_save;

    errno_save = errno;     /* we can be called by signal handlers */
    if (sigprocmask(0, NULL, &sigset) < 0)
        err_sys("sigprocmask error");

    printf("%s", str);
    if (sigismember(&sigset, SIGUSR1))  printf("SIGUSR1 ");
    if (sigismember(&sigset, SIGALRM))  printf("SIGALRM ");
    /* remaining signals can go here  */

    printf("\n");
    errno = errno_save;
}

I thought the output would be like this:

Starting main:
starting sig_usr1: SIGUSR1
in sig_alrm: SIGUSR1 SIGALRM
finishing sig_usr1: SIGUSR1
End main:

but it seems something wrong, this is my output in fact:

Starting main:
starting sig_usr1: 
in sig_alrm: 
finishing sig_usr1: 
End main:

that is no signals. Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:37:28+00:00Added an answer on May 27, 2026 at 11:37 pm

    I think the trouble is probably that you are using signal() and not sigaction() to set the signal handling. And signal() does not mask any other signals – so there are no signals to show as being blocked. I modified your code as shown below, to use signal() and sigaction() depending on whether there are any arguments or not.

    #include <signal.h>
    #include <unistd.h>
    #include <setjmp.h>
    #include <time.h>
    #include <errno.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef void (*Handler)(int);
    static void sig_usr1(int), sig_alrm(int);
    static sigjmp_buf jmpbuf;
    static volatile sig_atomic_t canjmp;
    static void pr_mask(const char *str);
    
    static void err_sys(const char *str)
    {
        int errnum = errno;
        fprintf(stderr, "%s (%d: %s)\n", str, errnum, strerror(errnum));
        exit(1);
    }
    
    static void set_sigaction(int signum, Handler handler)
    {
        struct sigaction nact;
        nact.sa_handler = handler;
        sigfillset(&nact.sa_mask);
        //sigemptyset(&nact.sa_mask);
        nact.sa_flags = 0;
        if (sigaction(signum, &nact, 0) != 0)
            err_sys("Failed to set signal handling");
    }
    
    int 
    main(int argc, char **argv)
    {
        printf("PID = %u\n", (unsigned)getpid());
        if (argc > 1)
        {
            if (signal(SIGUSR1, sig_usr1) == SIG_ERR)
                err_sys("signal(SIGUSR1) error");
            if (signal(SIGALRM, sig_alrm) == SIG_ERR)
                err_sys("signal(SIGALRM) error");
        }
        else
        {
            set_sigaction(SIGUSR1, sig_usr1);
            set_sigaction(SIGALRM, sig_alrm);
        }
        //print signal.
        pr_mask("Starting main: ");
        if (sigsetjmp(jmpbuf, 1)) {
            pr_mask("End main: ");
            exit(0);
        }
        canjmp = 1;
        for (;;)
            pause();
    }
    
    static void
    sig_usr1(int signo)
    {
        time_t starttime;
    
        if (canjmp == 0) {
            return;
        }
    
        pr_mask("starting sig_usr1: ");
        alarm(3);
        starttime = time(NULL);
        for (;;) 
            if (time(NULL) > starttime + 5)
                break;
        pr_mask("finishing sig_usr1: ");
        canjmp = 0;
        siglongjmp(jmpbuf, 1);
    }
    
    static void
    sig_alrm(int signo)
    {
        pr_mask("in sig_arlm: ");
    }
    
    void
    pr_mask(const char *str)
    {
        sigset_t    sigset;
        int         errno_save;
    
        errno_save = errno;     /* we can be called by signal handlers */
        if (sigprocmask(0, NULL, &sigset) < 0)
            err_sys("sigprocmask error");
    
        printf("%s", str);
        if (sigismember(&sigset, SIGUSR1))  printf("SIGUSR1 ");
        if (sigismember(&sigset, SIGALRM))  printf("SIGALRM ");
        /* remaining signals can go here  */
    
        printf("\n");
        errno = errno_save;
    }
    

    Running on MacOS X 10.7.2 with current XCode (4.2?), I get (for example):

    $ ./sigtest
    PID = 11066
    Starting main: 
    starting sig_usr1: SIGUSR1 SIGALRM 
    finishing sig_usr1: SIGUSR1 SIGALRM 
    in sig_arlm: SIGUSR1 SIGALRM 
    End main: 
    $ ./sigtest 1
    PID = 11067
    Starting main: 
    starting sig_usr1: SIGUSR1 
    in sig_arlm: SIGUSR1 SIGALRM 
    finishing sig_usr1: SIGUSR1 
    End main: 
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading this question I found this as (note the quotation marks) code to solve
Reading source code of sample projects, such as Beast and Bort, are recommended as
Reading source code of my current project, I see: [self retain] in one class,
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading this site, I've found this : [The] line private static final Foo INSTANCE
Reading the Python 3.2 tutorial here , towards the end one of the examples
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
Reading or writing a C code, I often have difficulties translating the numbers from
Reading questions, comments and answers on SO, I hear all the time that MSVC
Reading the section Zend_Application_Resource_Modules in the docs here: http://framework.zend.com/manual/1.10/en/zend.application.available-resources.html I noticed this: You can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.