Solved

Multi-tasking in C

Posted on 1998-12-24
8
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:thankyouall
ID: 1180986
I still dun get it...
0
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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