Solved

Multi-tasking in C

Posted on 1998-12-24
8
257 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
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!

 

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
White board coding practice 3 98
How to convert Structure to vector of byte and vice versa 5 37
why doesn't assert terminate here? 2 16
keep track of class structure 1 50
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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