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

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    14

    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!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Doesn't it come with some documentation? What are you using to read from it? Post a very simple bit of code illustrating your attempt:
    Code:
    #include<stdio.h>
    #include "whateveryourboardrequires.h"
    
    int main( void )
    {
       //simple stuff needed for a single read, no unnecessary crap
    
        for(;;)
        {
            //an illustration of your read function call such as:
            read( foo, bar );
        }
        return 0;
    }
    OS, compiler, board make and model, etc would be helpful also.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    14
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Does your board have some way of letting you know when data is ready to read? Or when it's no longer ready? Consider one way of reading the contents of a file:
    Code:
    while( fread( tohere, blocksize, numblocks, file )  > 0 )
    {
        ...we've got some stuff...
    }
    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?

    [edit]
    Need more coffee. Thinking fgets, writing fread. Too many edits later... :P
    [/edit]

    Quzah.
    Last edited by quzah; 08-30-2006 at 10:19 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well you could start by saying which OS and compiler you're using.
    Yes you can do stuff with ctrl-c, but how is specific.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I tried to McGuyver something together using my limited knowledge of C programming
    There's only one McGuyver.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    • Server receives OK and ends transaction/thread and frees resources, or NOT OK and begins transaction process again.
    • Client now either processes data, asks for more, or ends transaction by closing socket and terminating transaction


    This is a very simple list and most transactions are much more involved. But it always requires two-way communication between server and client or the transaction is dead duck.
    Last edited by VirtualAce; 08-31-2006 at 12:31 AM.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    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

    man signal
    man raise

    -----------------------------------
    http://www.uberum.com
    Last edited by sisovich; 08-31-2006 at 05:00 AM.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well 'signal' in my description meant a specific data standard accepted by both client and server to denote important events in the transaction.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could use kbhit() if your compiler supports it. The following loop would exit when a key is pressed:
    Code:
    while(!kbhit()) {
        /* ... */
    }
    It's non-standard, but then so is trapping CTRL-C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "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


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    14
    First of all, thanks for all the replies. They're great help!

    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.

    http://www.gerald-friedland.de/frac...-conio-1.02.tgz

    I'm thinking this is way too much work. :-)

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    BTW, your link is invalid. It has too many '.'s in it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "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


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by darkwalk
    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!!

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not knowing hoe to trap CTRL-C isn't a big deal. I don't know how to do it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "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


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by dwks
    Not knowing hoe to trap CTRL-C isn't a big deal. I don't know how to do it.
    Well I have used signal handling in C before, I think he just needs to trap sigint or something like that, and I have no qualifications, (well not in computing anyway).
    Last edited by esbo; 08-31-2006 at 01:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM