GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Dec-2005, 01:01
expi12 expi12 is offline
New Member
 
Join Date: Dec 2005
Posts: 2
expi12 is on a distinguished road

Running several Programs at the same time


I have tried to search the tutorial to find a function for time but was unable to find it.I hope that someone will be able to help me in this forum.
Although i am new in C i want to make a simple program (using C) that can do the foll.:

I want to continuously rotate a horizontal line (at 90 degrees) at a specific time interval and i want to flicker a dot at different time intervals + move a circle every pixel upward. All this must be done simultaneously.

To illustrate this:
Pseudocode:

Code:
for(i=0;i<10;i++) { color(BROWN); draw horizontal line delay this for 2 seconds color(BLACK) draw black line on previous line draw vertical line delay this for 2 seconds draw horizontal line } for(i=0;i<1000;i++) { color(RED); draw dot delay red dot for 4 seconds color(BLACK) draw black dot at previous position } for(i=0;i<10000;i++) { color(GREEN) draw circle delay circle for 5 seconds color(BLACK) draw black circle at previous position draw circle at new position(a pixel upward) }

I have been told that i can use the time function to achieve the above.

If there is a mean of using the inner clock of the computer to do this that would probably be easier.
This means i could use a function from C porably found in the <time.h> library to set the time as what it is.
E.g it is 14:01 23 seconds. I see the horizontal line, dot and circle.
At 14:01 25sec vertical line drawn.
At 14:01 27sec horizontal line drawn AND dot flicker
At 14:01 28sec circle goes one pixel upward
At 14:01 29sec horizontal line drawn again

This should continue for a long time. How will i be able to tell the computer that it should know the time and for every interval of 2 4 and 5 seconds it should do these program.

As a conclusion if this program do work, I will be able to see a line rotating at 90 degrees, a circle moving upward and a dot flickering.

If ever this work...

I thought about making the computer count a very large number but this stop only ONE thing at a time (the dot flicker and when it finishes ,if ever, the cirlcle moves up and then finally the line rotating).

Thanks.
Last edited by LuciWiz : 29-Dec-2005 at 22:04. Reason: Edited tags
  #2  
Old 30-Dec-2005, 13:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,619
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Running several Programs at the same time


Quote:
Originally Posted by expi12
I have tried to search the tutorial to find a function for time but was unable to find it.I hope that someone will be able to help me in this forum.

The standard C library function time() returns a value that is equal to the number of seconds since midnight, Jan 1, 1970. Since its value changes every second, some people use it to create a program delay of about a second.

Now, if you are really going to do big-time graphics, you probably won't limit yourself to standard C functions, but will probably use some graphics program interface, such as Microsoft Windows, X-Windows, GL, FLTK, or others which occasionally make their way to the surface on this and other forums. Many of these higher-level APIs (Applications Programming Interfaces) have timer functions that processes can start, and which may give finer granularity than one second. Some implementations of C have functions like sleep() or usleep() that have some advantages over just continuously calling time() until the value changes.

However, sticking to what you started with, here is a program that uses a function to wait about a second before returning. The advantage to using this is, say you want to simulate some alarm system that turns on some security light at 8:00 pm every day and turns it off at 6:00 am, except for Saturday and Sunday, in which case it turns the light on at 5:00 pm and turns it off at 8:00 am. You really don't want to wait a week or three to run the program in real time, so you make your simulation interval much shorter.
That is, the entire sequence can be simulated quickly by using a "null" function (one that has no explicit delay --- it just returns immediately).

CPP / C++ / C Code:
#include <stdio.h>
#include <time.h>

int main()
{
  void wait_a_tick(void);

  int simtime = 0;

  int i;
  const int number_of_intervals = 60;

  for (simtime = 0; simtime < number_of_intervals; simtime++) {
    printf("%3d\n", simtime);
    wait_a_tick();
  }
  return 0;
}

void wait_a_tick(void)
{
  static time_t old_time = 0;
  time_t now;
  do {
    now = time(0);
  } while (now == old_time);
  old_time = now;
}

In this case, the program takes about a minute to run, and simply prints integer values 0 through 59 with one value printed about every second.

Now, let's address graphics. I'm not going to do anything with graphics, since there are no standard C library functions the deal with that subject. I'll just make a program that prints out the current values of things when anything changes.

Suppose there is one object that we want to manipulate: A dot. The dot has two states (color == red and color == black). This would be a good place to define and use an enumerated data type, but to keep things simple, I will let dot_state be an int, and I will define the "red" state to correspond to an integer value of 0 and the "black" state to be an integer value of 0.

Now, suppose I want to start the dot with a value ov "red" and I want to make it change state every 2 seconds (here I will define a "tick" to be a second, and I will use the wait_a_tick() function from the previous example.

Furthermore, suppose I want the color to change values every two seconds until 10 seconds have elapsed, after which time no more changes will occur.

CPP / C++ / C Code:
#include <stdio.h>
#include <time.h>

int main()
{
  void wait_a_tick(void);

  int dot(void);

  int dot_state = 1; /* 0 ==> red; 1 ==> black */
  int dot_changed;

  int simtime;

  int i;
  const int number_of_intervals = 10;

  for (simtime = 0; simtime <= number_of_intervals; simtime++) {
    dot_changed = dot();

    if (dot_changed) {
      dot_state = 1 - dot_state;
    }

    if (dot_changed) {
      printf("%3d: dot = %s\n", simtime, dot_state?"black":" red ");
    }
    wait_a_tick();
  }
  return 0;
}

void wait_a_tick(void)
{
  static time_t old_time = 0;
  time_t now;
  do {
    now = time(0);
  } while (now == old_time);
  old_time = now;
}

/* 
 * this returns 1 when it's time to for the dot to change state; 
 * otherwise it returns 0;
 */
int dot()
{
  const int state_change_time = 2;
  static int counter = -1; /* make sure it returns '1' the first time */
  int retval;

  counter = (counter + 1) % state_change_time;
  if (counter == 0) {
    retval = 1;
  }
  else {
    retval = 0;
  }

  return retval;
}

Here's my output:
Code:
0: dot = red 2: dot = black 4: dot = red 6: dot = black 8: dot = red 10: dot = black

The value for simulated time 0 is the initial value.
The other values represent time and new value for changes.

Now, I could have made it a little simpler or a little more complicated. In fact, I would have a separate function to take care of the actual state and a separate function to print the state, rather that putting in the main program. (Since I will be having more than one object, it probably would be a cleaner program to modularize the functionality. Furthermore, I probably would have made a struct for the dot object, having a member that is the state change period and another member for the state. Then I could pass the address of the struct to the various (evaluation and display functions).

Now, it's time for you to contribute. Add functions for the line and the circle to tell when to change state. Add code to change their values and change the print statement to print out state values of all of the objects whenever one or more of the objects has changed.

So, the main loop could look something like the following:
CPP / C++ / C Code:
  for (simtime = 0; simtime <= number_of_intervals; simtime++) {
    dot_changed = dot();
    line_changed = line();
    circle_changed = circle():

    if (dot_changed) {
      dot_state = 1 - dot_state;
    }
    if (line_changed) {
      /* do whatever you need to do to change the line */
    }
    if (circle_changed) {
      /* do whatever you need to do to change the circle */
    }
    if (dot_changed || line_changed || cicle_changed) {
      /* print values for dot, line and circle) */
    }
    wait_a_tick();
  }

There are lots (and lots) of ways to do things like this. This is a way to get started.

It might also motivate you to learn the complexities of Windows programming (with callback functions and graphics object manipulations) to do anything more complicated than a line, a dot, and a circle.

Regards,

Dave
  #3  
Old 02-Jan-2006, 00:30
expi12 expi12 is offline
New Member
 
Join Date: Dec 2005
Posts: 2
expi12 is on a distinguished road

Re: Running several Programs at the same time


Thanks for your help!
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simulation Problem wu_weidong CPP / C++ Forum 7 12-Mar-2005 22:56
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
time Problem zuzupus MySQL / PHP Forum 9 24-Jul-2003 07:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.