473,414 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to stop a program while it's executing and save progress

4
Hello,

I am writing a computationally heavy program which I know will need to run for many hours. Therefore, I need to make it possible to stop the program at any time while it's running and save my progress in an external file so that later the program can be started again and continue from where it left off. What I don't know how to do is how to actually get the program to stop, but make sure it executes some functions to save its progress before quitting.
I'm writing in C and using DOS on Windows. The only way I know how to stop a program in DOS is ctrl-c, and I've done some research but have been unable to catch this and act upon it before exiting.
If this helps - my program is recursive, so to save the progress it will need to backstep from whatever recursive depth it's on and save some data from each one.

Thank you.
Sep 4 '07 #1
8 2871
RRick
463 Expert 256MB
The concept you're talking about is called signals. Take a look at the signal.h header file. This will tell you what signals are supported and how to set up callbacks for them.

Typically, you register a callback subroutine for a specific signal. When the signal happens, your routine is called. As for traversing your recursive structure, you'll have to figure that out yourself. The callback routine knows nothing about what was running before it was called.

Some signals are "non-interruptable" and when they happen, you have no control over what happens. Usually, the program ends and that's that. The other interruptable types are the ones you want to deal with. I'm not sure which category Ctl-C falls into.
Sep 4 '07 #2
Stopping a program with Ctrl+C actually causes an exception in the program, so all you should need to do is surround your code with a try ... catch procedure.
If it's running in a DOS window, and the user clicks the close button I don't think the program is given any warning however (I'm not entirely sure).
Sep 5 '07 #3
goron
4
Stopping a program with Ctrl+C actually causes an exception in the program, so all you should need to do is surround your code with a try ... catch procedure.
If it's running in a DOS window, and the user clicks the close button I don't think the program is given any warning however (I'm not entirely sure).
I'm writing in plain C. Can C (not ++) catch exceptions? How is this done? If it can't be done, and what ctrl-C sends is an exception, not a signal, what is a better way to gracefully halt a program running in DOS?
Sep 5 '07 #4
Signalling is not the solution.

Checkpointing is.

I can give you an example, you modify toi your problem.

Suppose you want to calculate fibonacci numbers from 1 to 10000 and you will keep appending these numbers to a file.

The file may look like

1 0
2 1
3 1
4 2

....
....

But it may happen that you can't run the program continuously, so what do you do.

Every time you run the program, you read this file, read the last record and figure out the next fibonacci number to be calculated. Run the program for 100 numbers then close the file.

And so on.

Its never a good idea to use ctrl + c. Although you can handle them with signals in a better OS. You should give user the liberty to kill your program and let it die an un-natural death.
Sep 5 '07 #5
exit() in stdlib.h is much better than a ctrl + c
Sep 5 '07 #6
Also , DOS is a UGLY OS
Sep 5 '07 #7
goron
4
Also , DOS is a UGLY OS
If I'm programming on a windows machine for a windows machine, do I have an option?

As for the program - I see what you're saying with constantly saving to a file, but the amount of data I would have to constantly save is pretty large. Also, it would not be relevant to what the program is trying to produce. If we want all the fibonacci numbers, it makes sense to save them, but my program is just looking for the final result.
It just seems like a big waste of time to constantly be reading and saving files.
Sep 5 '07 #8
a lot of applications do provide checkpointing facility.

Check pointing is writing to disk in some way.

If you have to proceed with DOS for any inevitable reason, stick to it. If you are learning then come to Linux.

Coming to your task. What and how you will checkpoing to file depends on your application.

In general you will have to save the state of your program so that when it is restarted, it knows how much has been done and what it has to start doing.

Without knowing the details, I can just tell you to give more time to intelligently design what you will write to disk, how will you write it and how will on a restart reach a point where you will know how much was accomplished.


All the best.
Sep 5 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Brian Roberts | last post by:
I have a command line Python program that sometimes takes a bit (several minutes) to run. I want to provide an optional method for an impatient user (me!) to check the status of the program. The...
2
by: Joe Bloggs | last post by:
I have a Windows form application that has a start button, a progress bar and also writes information to the status bar the code is C#. My problem is that when you hit the start button and the...
3
by: prakashsha | last post by:
HI, I've an asp.net page which has a button. On click of the button the program queries the database and gets the result and displays the same.Now assume that my program takes 2 minutes to get the...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
4
by: Chris Moltisanti | last post by:
Hi, I am executing a piece of javascript that does some intensive processing (sorting large tables). This can take a good few seconds so I have an animated gif as a progress bar to let the user...
3
by: ohnoonho | last post by:
Hello, I posted this in the C# group but thought maybe the better place would be here since it is happening with website projects so please forgive any cross posting. In my website projects I...
3
by: diego | last post by:
Greetings everyone! Is there a way to stop query analyzer from processing remaining query statements? Let's say i have the following query in query analyzer: query statements if condition...
0
by: mattcfisher | last post by:
Hi, I have two windows services running together. One is the main program, and one is an "updater" wrapper for the main. The updater service starts and stops the main one (as in you should never...
1
by: CompletelyUseless | last post by:
Hi, I'm having a lot of troubles trying to build a function to figure out the difference (in color) between two images. I've tried to build something similar with the ImageMagick API, but I've...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.