Thread: Exit from program using timer

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    Exit from program using timer

    Hello to all,

    I am new to C and am learning on a daily basis. The code below will return a weight from a scales and output the result to a file. The program will only exit from a keyboard execution (Esc key). Is it possible to time out the program after a specified time (eg 5 seconds). Any pointers would be greatly appreciated.


    Code:
    #include <bios.h>
    #include <stdio.h>
    #include <conio.h>
    
    #define COM1       0
    #define DATA_READY 0x100
    #define TRUE       1
    #define FALSE      0
    
    #define SETTINGS ( 0xE0 | 0x18 | 0x04 | 0x02)
    /* MODE COM        9600   EVEN   2      7 */
    
    int main(void)
    {
       int status, DONE = FALSE,flag=0,j=0,n;
       char rname[10000],in,out;
       FILE *fp=NULL;
       bioscom(0, SETTINGS, COM1);
             cprintf("... BIOSCOM [ESC] to exit .../n");
             fp=fopen("c:\\myfile.txt","w");
             while (flag!=1  || out=='$')
             {
                    status = bioscom(3, 0, COM1);
                    status = status & DATA_READY;
                    if (status & DATA_READY)
                    /*if (status!=0)*/
                    {
                     out = bioscom(2, 0, COM1);
                     out = (out & 0x007f);
    
                     if(out!='$')
                     {
                            printf("%c",out);
                            rname[j]=out;
                            j++;
                     }
                    }
                    if (kbhit())
                     {
                            if ((in = getch()) == 27)
                                     flag = 1;
                     }
             }
             n=j;
            for(j=0;j<n;j++)
             {
            fputc(rname[j],fp);
             }
             fclose(fp);
             return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Check your compiler options, you should have a sleep(NumberOfSeconds), or Sleep(NumberOfMilliseconds), or possibly one of these, prepended by an underline: _sleep(), that you can use.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You can use time() function from time.h
    Example:
    Code:
    #include <time.h>
    
    #define TIMEOUT_VALUE 5
    
    time_t startTime = time(NULL);
    do
    {
      // instructions
    } while (time(NULL) < startTime + TIMEOUT_VALUE);

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Thankyou. time_t works perfectly.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    #include <bios.h>
    #include <stdio.h>
    #include <conio.h>

    OK, so you're using a fossil compiler, what is your real operating system?

    If you're on say XP or newer, you can skip ahead about 20 years and join the rest of us in this millennium.

    Download one of these fine compilers.
    Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
    smorgasbordet - Pelles C
    Code::Blocks
    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
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Yes Salem. Jumped in the Tardis and went back to the late 80's. I've downloaded MS Visual Studio Express and will be back to you soon...........

  7. #7
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Code:
    #include <signal.h>
    #include <unistd.h>
    int main(void)
    {
      sleep(3);
      raise(9);
      for (;;)
        ;  /* infinite loop*/
      return 0;
    }
    </fun>
    Last edited by \007; 07-12-2011 at 06:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exit program
    By Teardrop3903 in forum C Programming
    Replies: 7
    Last Post: 03-11-2011, 01:06 AM
  2. -1 to exit program ?
    By sharris in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2010, 09:14 PM
  3. Before your program does exit(0);
    By ycode in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 11:47 PM
  4. How to exit the program?
    By bigben in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 09:06 AM
  5. exit program
    By spentdome in forum C Programming
    Replies: 4
    Last Post: 04-22-2002, 02:08 PM