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

The Archive Base Latest Questions

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

How to design a C/C++ program so that it can save some data after

  • 0

How to design a C/C++ program so that it can save some data after receiving interrupt signal.

I have a long running program that I might need to kill (say, by pressing Ctrl-C) before it finished running. When killed (as opposed to running to conclusion) the program should be able to save some variables to disk. I have several big Linux books, but not very sure where to start. A cookbook recipe would be very helpful.

Thank you.!

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

    to do that, you need to make your program watch something, for example a global variable, that will tell him to stop what it is doing.

    For example, supposing your long-running program execute a loop, you can do that :

    g_shouldAbort = 0;
    while(!finished)
    {
       // (do some computing)
    
       if (g_shouldAbort)
       {
          // save variables and stuff
          break; // exit the loop
       }
    }
    

    with g_shouldAbort defined as a global volatile variable, like that :

    static volatile int g_shouldAbort = 0;
    

    (It is very important to declare it “volatile”, or else the compiler, seeing that no one write it in the loop, may consider that if (g_shouldAbort) will always be false and optimize it away.)

    then, using for example the signal API that other users suggested, you can do that :

    void signal_handler(int sig_code)
    {
       if (sig_code == SIGUSR1) // user-defined signal 1
          g_shouldAbort = 1;
    }
    

    (you need to register this handler of course, cf. here.

    signal(SIGUSR, signal_handler);
    

    Then, when you “send” the SIGUSR1 signal to your program (with the kill command for example), g_shouldAbort will be set to 1 and your program will stop its computing.

    Hope this help !

    NOTE : this technique is easy but crude. Using signals and global variables makes it difficult to use multiple threads of course, as other users have outlined.

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

Sidebar

Related Questions

I have a (C#) genetic program that uses financial time-series data and it's currently
I have a contact manager program that I'd like to design for multiple network
I have some functions that can be grouped together, but don't belong to some
Can I design my program so that it will automatically change OS's hotkeys parameters?
gooday programers. I have to design a C++ program that reads a sequence of
I am writing a program that does some batch processing. The batch elements can
I want to design a c# program which can run program a 3rd exe
I have to write a program that performs highly computationally intensive calculations. The program
Background: I have a function in my program that takes a set of points
How can I write a program that will automatically generate a sample examination? For

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.