stop an executing C program and finish executing additonal line of code

stop an executing C program and finish executing additonal line of code

This is a discussion on stop an executing C program and finish executing additonal line of code within the C Programming forums, part of the General Programming Boards category; Hi all: This is for a program I wrote for my master's thesis. The program executes an infinite while loop ...

stop an executing C program and finish executing additonal line of code

Hi all: This is for a program I wrote for my master's thesis. The program executes an infinite while loop and at each pass, it acquires data from an aquisition board. Because I don't know how long the acquisition is each time, I just hit ctrl+c at the end. This is suboptimal because I'd like to execute more lines of code to free memory and save the states of the system. I tried to McGuyver something together using my limited knowledge of C programming as well as search google, but I can't find a solution. If anyone can help, I'd really appreciate it. Thanks!

quzah, thanks for the reply. I think I didn't explain the situation correctly. Your code is basically what I'm doing, except that the for loop is a while(1) loop. It goes on forever, because I have no way of knowing how long i'm going to acquire the data.

Right now to stop my program, I just hit ctrl+c. I would like a way of stopping from a user input and also execute some chunk of code (in a function possibly?) before ending the program.

I was thinking of using the wait time between acquisition to listen for some key. But I don't know if that will possibly work and how to start.

The fread function will return 0 if it's reached the end of the file or an error. Do your read functions have useful return values? Are you able to poll the device to see if something is ready / waiting to be read? (Such as the select function, with sockets? Surely it has some form of API or documentation you can use to check when it's ready to be read from?

That makes no sense. Pressing CTRL-C amid a data transaction any ole time you feel like it is one way to ensure that everything is either not sent correctly, or not processed correctly. Data transactions between computers just don't work that way.

If the client has no way of knowing when the transaction ends then the server must send an end of transaction signal to the client. This could be done by having the client fire off a process to listen to a certain port indefinitely. The server would then send some type of data indicating end of transaction to the client on this port. Once the client received the end of transaction signal, it would then communicate to the parent process that the data transaction is complete and it can now begin to process the data. At this time, the listening process would go idle. Once the client was done processing the data and needed more, it would tell the server, and the listening process/thread would go active, etc, etc.

The way you have described it means it's impossible for the client to know when it has received all of the data and when/if it can begin processing the received data. It's also impossible for the server to know if the data has been received or if it's just sending data down a port to the client ad hoc regardless if the client is listening, receiving, or eating lunch. This in turn means the server can never serve another client because it is always serving just one indefinitely. At most it would serve several clients indefinitely in sep threads, however, at some point in time the server is going to run out of resources due to each thread being in an infinite loop.

It has to work something along these lines:

Client needs data

Client asks server for data

Server responds with either a wait signal due to resources, or responds with header indicating data transaction parameters.

Client either goes into WAIT mode (listening to a port for the go ahead) or process header information and sets up the data transaction buffer using the length specified by the server in the header.

Client indicates it is now ready to receive data

Server either responds with WAIT (prob not since now serving this client) or the requested data

At end of transaction, client responds to server with OK meaning data length in header matches data length received or NOT OK meaning some data was lost in which transaction must be repeated.

Well as Bubba said, when you deal with I/O issues signals are almost mandatory, if you are in a Linux environment , try man signal and you will see the start
If you want to apply a kind of brute force and no go into signal I/O topics, you can always set a timer that, after some specifided time, ends the program or transfer the execution to another place, doing so you avoid the anti-cosmetic CTRL+C. But it&#161;s still a poor solution, let's consider a quick workaound

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

bubba: The client/server do not have complex acknowledge, ready, send, recieve signals unfortunately. The client just sends data (think of it as sending UDP packets) without acknowledging anything. The server picks up the packet whenever correct timing is achieved. So the control is all in the server side.

dwks: I tried your suggestion, but the function is available only to Borland C++. And Linux replacements require some kind of installation which I do not have admin rights.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

quzah, thanks for the reply. I think I didn't explain the situation correctly. Your code is basically what I'm doing, except that the for loop is a while(1) loop. It goes on forever, because I have no way of knowing how long i'm going to acquire the data.

Right now to stop my program, I just hit ctrl+c. I would like a way of stopping from a user input and also execute some chunk of code (in a function possibly?) before ending the program.

I was thinking of using the wait time between acquisition to listen for some key. But I don't know if that will possibly work and how to start.

You say you are writing a master thesis!! Not in a computing related subject one would hope!!

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell