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 6750833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:51:18+00:00 2026-05-26T12:51:18+00:00

I have a shutdown hook handler defined in a SINGLE threaded C application. int

  • 0

I have a shutdown hook handler defined in a SINGLE threaded C application.

int main(int argc, char * argv[]){
    //Shutdown hook for CTRL-C
    (void) signal(SIGINT, shutdownHook);
    ...
    ...
}

So when the user hits CTRL-C the shutdown hook is initiated…

void shutdownHook(int sig){
    rc = wmq_sender_stop(errorStr);
    if (rc != 0){
        printf("\n%s\n", errorStr);
    }
    while(transactionRunning == TRUE){
        sleep(1);
        printf("Transaction still running\n");
    }
    ....
    ....
}

You can see above that I call a “wmq_sender_stop” routine (in a shared lib) that essentially sets a variable to FALSE to end a loop in that shared lib.

int wmq_sender_stop(char errorStr[ERROR_STR_LEN]){
       running = FALSE;
       ...
}

And that variable “running” will stop (hopefully) the main loop running in that shared lib.

while(running == TRUE){
...
...
}

The problem I am having is on my Linux box all works 100%…but when I install the application on a BIG powerful server it calls the “wmq_sender_stop” and sets the variable fine, but it seems like the application is running too fast on the server to allow the “while(running == TRUE)” variable to exit…..OR it is simply returning to the main application too fast and the “while(transactionRunning == TRUE)” loop just continuously runs…

Essentially on the BIG server if I hit CRTL-C the following is continuosly output to the screen:

Transaction still running
Transaction still running
Transaction still running
Transaction still running
Transaction still running
Transaction still running
...

Perhaps I need to make threads here and have intercommunication between them but is their no easier way to elegantly allow a “loop in a shared lib” to end it’s “critical code / transactions” before shutting down even on extremely fast servers? And why does it work on my linux laptop?

And if I need to use threads what is the best and FASTEST way for them to comminucate? Currently I am using CALLBACKS….but am not sure what the best IPC is for threads?

Thanks for the help, much appreciated

Lynton

  • 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-26T12:51:19+00:00Added an answer on May 26, 2026 at 12:51 pm

    Make sure that running (and transactionRunning, too) is declared as volatile. Otherwise, the C compiler may cache its value under some circumstances. For now, think of volatile as meaning “this value could get changed by a signal handler”.

    Note that you have to be careful with code like this:

    rc = wmq_sender_stop(errorStr);
    

    If you do that in a signal handler, you had better hope that that function is safe to call from a signal handler. Most functions aren’t. (Even functions that are safe to call from multiple threads are unsafe to call from a signal handler.)

    The last issue is that if your code is single-threaded, but you never return from your signal handler, how is your main loop supposed to exit? The signal handler runs in one of your application’s existing threads. Generally the only thing you want to do in a signal handler is change the value of a global variable that is marked volatile and then return.

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

Sidebar

Related Questions

I have a fun issue where during application shutdown, try / catch blocks are
I have a Java application which I want to shutdown 'nicely' when the user
I have a main application that spawns a seperate thread to process messages off
I recently needed to add a shutdown hook to a Scala app I have,
I have a spring application which is not calling bean destroy methods on shutdown.
I have come across the shutdown hooks functionality which is invoked when java application
I have a critical process running in java (1.6), with a registered shutdown hook.
I have a service application that on startup and shutdown logs an event log
I have a finalizer that seems to always fail during application shutdown. I think
Have the following in my module's BEGIN section: use sigtrap qw(handler shutdown normal-signals); use

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.