Solved

Multi-tasking in C

Posted on 1998-12-24
8
250 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

777 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