Solved

Multi-tasking in C

Posted on 1998-12-24
8
247 Views
Last Modified: 2010-04-02
How can I program a timer in C? I would like to have a timer which will be activated say every 2 micro-second or so and
perform a particular task, say TASK 1. So this timer will keep on running in the background and whatever code that has been executed in "main()" will not slow down TASK 1 or has no effect on it.
0
Comment
Question by:thankyouall
8 Comments
 

Author Comment

by:thankyouall
ID: 1180983
I'm using Turbo C++ v3.0 for DOS
0
 
LVL 2

Expert Comment

by:harrys
ID: 1180984
You might hook your routine into INT 0x1c (the ticker interrupt), but you cant call any DOS functions from an interrupt routine...
0
 
LVL 1

Expert Comment

by:TheMadManiac
ID: 1180985
the code in main will always note a small slowdown. This is however transparent to main, and main does not have to think about it (interrupts take over control of the cpu for a small amount of time).

The 0x1c interrupt is to slow to do a 2ms timer. For this you might want to hook to the timer interrupt (9 or 8, one of them is the timer, the other the keyboard.. i always mix those.. ;-)

Once you have grabbed this irq, you can then increase the timer-intervall time, and call the old irc-handler at 18.2 times/second. it's pretty straightforward
0
 

Author Comment

by:thankyouall
ID: 1180986
I still dun get it...
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 11

Expert Comment

by:alexo
ID: 1180987
There is absolutely no built-in multitasking support in the C language, you have to use operating system support for that.
In your case it is a bit difficult because DOS is not a multitasking OS but you can fake it using Interrupt Service Routines.  You can hook the timer interrupt and provide your own service routine for it.  Beware of reentrancy issues.
0
 
LVL 1

Expert Comment

by:_Canidae_
ID: 1180988
Every 2 MICROseconds???  Wow, you'll need a super-efficient timer for that ;o)
You'll be lucky if your main loop is even processed nearly as often as that.  ;o)
0
 
LVL 1

Accepted Solution

by:
_Canidae_ earned 50 total points
ID: 1180989
Just ignore that last comment, 'twas a slip o' the mind.  Anyway, here's an example of how to use a simple timer.  This sure won't help if you really did want a timer with microsecond-precision, but otherwise perhaps it will.  BTW:  This compiled fine under TC3 :o)      Also:  sorry if the indentation is a little strange here, I tried to fix it.

#include <stdio.h>
#include <conio.h>

void main()
{
      unsigned long far *clock;
      clock = (unsigned long far *) 0x0040006cL;
      int go=1, running=0;
      unsigned long start, now;
      float sofar=0.0;
      printf("Press G to run, S to stop, C to clear or ESC to quit\n\n");
      while (go)
      {
            if (kbhit())
            {
                  int c=getch();
                  switch(c)
                  {
                        /*if ESC pressed*/
                        case 27:      go=0;
                              break;
                        case 'g':
                        case 'G':      if (!running)
                              {
                                            running=1;
                                    start=*clock;
                              }
                              break;
                        case 's':
                        case 'S':      running=0;
                              break;
                        case 'c':
                        case 'C':      start=*clock;
                              sofar=0.0;
                              break;
                        default:      putchar(7); //invalid key, so beep
                              break;
                  }
            }
            if (running)
            {
                  now=*clock;
                  unsigned long elapsed=now-start;
                  sofar=elapsed/18.2;
            }
            printf("\r%8.2f Seconds", sofar);
      }
}
0
 

Author Comment

by:thankyouall
ID: 1180990
Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now