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

  • SEARCH
  • Home
  • 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 8621795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:53:53+00:00 2026-06-12T06:53:53+00:00

I am having trouble trying to handle a signal… I have a multithreaded application,

  • 0

I am having trouble trying to handle a signal…

I have a multithreaded application, which receives a signal that interrupts system calls in a library that I am using. After some research, I found that if a signal is not handled, it gets sent to a random thread in the application. I have confirmed from the library I am using that they are not using any signals in their application. Neither am I. Here is my main driver class:

void sig_handler(int signum)
{
    cout << "Signal Handle: " << signum << endl;
    signal(signum, SIG_IGN);
}


class Driver
{
    public:
        Driver();
        void LaunchLog();
        void logonListen();
};

Driver::Driver()
{
    pthread_t logThread;
    pthread_create(&logThread, NULL, LaunchLogThread, (void*) this);
    pthread_detach(logThread);

    pthread_t listenThread;
    pthread_create(&listenThread, NULL, LaunchListenThread, (void*)this);
    pthread_detach(listenThread);

    bool running = true;

    while(running)
    {
        //Simple loop to resemble a menu for the console
    }
}

void Driver::logonListen()
{
    char buffer[256];

    int logonPort = BASEPORT;
    int sockfd;
    struct sockaddr_in serv_addr, cli_addr;
    socklen_t clilen;

    //initialize socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0){
        perror("Error opening socket");
        exit(1);
    }
    bzero((char*)&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(logonPort);

    //bind socket to our address
    if(bind(sockfd,(struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
    {
        perror("Error on bind");
        exit(1);
    }

    clilen = sizeof(struct sockaddr_in);

    int n;
    int nextUser = 2;
    char reply[32];

    while(1)
    {
        //Wait for incoming connection on a socket
    }
}

void Driver::LaunchLog()
{
    Logger::Instance()->WriteToFile();
}

void* LaunchListenThread(void* ptr)
{
    Driver* driver = (Driver*) ptr;
    driver->logonListen();
}

int main()
{
    signal (SIGHUP, sig_handler);
    signal (SIGINT, sig_handler);
    signal (SIGQUIT, sig_handler);
    signal (SIGILL, sig_handler);
    signal (SIGTRAP, sig_handler);
    signal (SIGFPE, sig_handler);
    signal (SIGKILL, sig_handler);
    signal (SIGUSR1, sig_handler);
    signal (SIGSEGV, sig_handler);
    signal (SIGUSR2, sig_handler);
    signal (SIGPIPE, sig_handler);
    signal (SIGALRM, sig_handler);
    signal (SIGTERM, sig_handler);
    signal (SIGCHLD, sig_handler);
    signal (SIGCONT, sig_handler);
    signal (SIGSTOP, sig_handler);
    signal (SIGTSTP, sig_handler);
    signal (SIGTTIN, sig_handler);
    signal (SIGTTOU, sig_handler);
    signal (SIGABRT, sig_handler);
    Driver driver;
    return 0;
}

I am unable to handle the signals. Interrupted System Calls keep creeping up, and my signal handler never gets used. Even when I press CTRL+C in the console, the program ends with interruption rather than SIGINT being handled. Am I installing the handler incorrectly?

Is there a way to handle all signals and to ignore them if they arise?

Thanks

  • 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-06-12T06:53:55+00:00Added an answer on June 12, 2026 at 6:53 am

    You want to use sigaction(2) with the SA_RESTART flag to ignore the signals and insure that system calls get restarted instead of interrupted:

    struct sigaction sa;
    sa.sa_hanlder = SIG_IGN;
    sa.sa_flags = SA_RESTART;
    
    sigaction(SIGALRM, &sa);
    sigaction(SIGPIPE, &sa);
    /* repeat for all the signals you want to ignore */
    

    Note that you might not want to ignore things like SIGINT as you then won’t be able to stop you program with ctrl-C. Likewise, ignoring SIGSEGV may cause your program to hang if it contains a bug, rather than exiting.

    edit

    Your description that neither you nor the library is using any signals doesn’t quite ring true — the signals are coming from SOMEWHERE, and it may just be a case that you don’t realize something you are doing is using signals under the hood. If you’re using any alarms or itimers anywhere, those involve signals. If your sig_handler is truly not being called, that implies that someone else (your library?) is installing a signal handler to replace it.

    If you still can’t figure out where the signals are coming from, you can run under a debugger, and enable the debugger’s signal debugging ability (if needed). That should at least tell you which signals are occurring, and where they are occurring.

    In general, with any system call, you should ALWAYS check for errors, and if you see the error EINTR unexpectedly, you should probably just loop and redo the system call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm having trouble trying to define this function-like macro, which takes 4 vector magnitudes
I been having trouble trying to figure this out. When I think I have
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I am having trouble trying to print a table from a webpage. When I
I'm having trouble trying to use a function in a javascript file I've included
I am really having trouble trying to figure out how to find certain records
I'm having trouble trying to get a linebreak included in a Stringbuilder to appear
I am having trouble trying to put in data for this project I'm working
I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
I am having trouble trying to connect an Infopath 2007 form to an WCF

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.