Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multi-tasking in C

Posted on 1998-12-24
8
Medium Priority
?
268 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

604 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