473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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 2895
RRick
463 Recognized Expert Contributor
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
cypherzero
15 New Member
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 New Member
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
smalpani
29 New Member
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
smalpani
29 New Member
exit() in stdlib.h is much better than a ctrl + c
Sep 5 '07 #6
smalpani
29 New Member
Also , DOS is a UGLY OS
Sep 5 '07 #7
goron
4 New Member
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
smalpani
29 New Member
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
2163
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 type and amount of status information doesn't fit nicely into a --verbose or logger -- either too little or too much information at different points. I think an optional web page would be convenient interface. The Python program would listen...
2
17669
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 program executes then the application window does not get refreshed so you just see an ugly gray rectangle until the application ends. The status and progress bar do get updated but still if you toggle to another application and then toggle back to...
3
2472
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 results.In the mean time if the user press Esc button to stop the process. The page will stop processing but my database connection will be trying to retrieve the records. How to solve this ??? Thanks in Adavance.
1
2830
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. Despite entering the required lines to "my.ini" (the new my.cnf) through notepad AND MySQL Administrator, the cache does not work. So, today I took a peek at the 'Health' tab in MySQL Administrator.
4
8051
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 know that something is happening. However, the GIF does not animate when the javascript is processing. Anyone know why? I am aware that animated GIFs stop animating when a page is loading, but I am not loading the page here.
3
1403
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 can set a breakpoint, run the app, the breakpoint gets hit, and I can step through or check some variables or what not, and if I hit stop it appears that the rest of the method (and any calling method) continues to execute. I know this because...
3
2348
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 begin query statements stop the query
0
2198
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 start or stop main service manually, only updater). On most computers it works great, the updater's onStop() can start and stop the main service perfectly. On some computers, though, the updater hangs when it tries to stop the main service. It...
1
2790
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 had many troubles compiling a DLL from the libraries. I am also unfamiliar with the .net libraries, but I've been reading up on MSDN. I know how the basic flow of the program should go (I think), I've been having trouble executing the steps. ...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.