[comp.lang.c] MSC delay

azarian@hpcc01.HP.COM (Randy Azarian) (03/10/90)

Does anyone have a delay routine written in C?

I don't need something very accurate.  If I say delay(3) I want it to 
pause execution for approximately 3 seconds.

(Anything more accurate would be suitable).


Here is what I have.  It works, but it isn't too consistant. 
If I say delay(1), sometimes I get a delay for one second, and sometimes I 
get a microsecond delay.

delay(n)
int n;
{
  struct dostime_t time1, time2;
  int i;
  unsigned long one,two;

  _dos_gettime(&time1); time2=time1;
  one = (time1.hour*3600) + (time1.minute*60) + time1.second;
  two = (time2.hour*3600) + (time2.minute*60) + time2.second;
  while ((two - one) < n) {
    _dos_gettime(&time2);
    two = (time2.hour*3600) + (time2.minute*60) + time2.second;
  }
}

ping@cubmol.bio.columbia.edu (Shiping Zhang) (03/13/90)

In article <1280001@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
>Does anyone have a delay routine written in C?

How about sleep()?
 
>Here is what I have.  It works, but it isn't too consistant. 
>If I say delay(1), sometimes I get a delay for one second, and sometimes I 
>get a microsecond delay.

[...]
>  struct dostime_t time1, time2;
[...]
>  _dos_gettime(&time1); time2=time1;
                         ^^^^^^^^^^^
You can't assign a struct like that. You must assign the elements
of a struct individually.


-ping

will@kfw.COM (Will Crowder) (03/14/90)

In article <1990Mar13.041457.17217@cubmol.bio.columbia.edu> 
ping@cubmol.bio.columbia.edu (Shiping Zhang) writes:
>[...]
>>  struct dostime_t time1, time2;
>[...]
>>  _dos_gettime(&time1); time2=time1;
>                         ^^^^^^^^^^^
>You can't assign a struct like that. You must assign the elements
>of a struct individually.

No, you don't have to assign the elements of the struct individually.  We
just went through this.  You can do anything with a struct you can do
with any other object, including assign it, pass it by value, etc. etc.
All ANSI C compilers and almost all UNIX C compilers now handle this.
In the dark old days, you couldn't do that, but that is no longer the case.

(Don't you read this group before you post?)

Will

dan@kfw.COM (Dan Mick) (03/14/90)

In article <1990Mar13.041457.17217@cubmol.bio.columbia.edu> ping@cubmol.bio.columbia.edu (Shiping Zhang) writes:
|In article <1280001@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
|>Does anyone have a delay routine written in C?
|
|How about sleep()?
| 
|>Here is what I have.  It works, but it isn't too consistant. 
|>If I say delay(1), sometimes I get a delay for one second, and sometimes I 
|>get a microsecond delay.
|
|[...]
|>  struct dostime_t time1, time2;
|[...]
|>  _dos_gettime(&time1); time2=time1;
|                         ^^^^^^^^^^^
|You can't assign a struct like that. You must assign the elements
|of a struct individually.


#define FLAME

OF COURSE YOU CAN ASSIGN A STRUCT LIKE THAT!  Post answers *after* you know
what you're talking about, dammit!  EVERY C COMPILER I'VE USED ALLOWS STRUCT
ASSIGNMENT, *MUCH LESS* Turbo C!!!!!

#undef FLAME

anigbogu@loria.crin.fr (Julian ANIGBOGU) (03/14/90)

In article <1990Mar13.041457.17217@cubmol.bio.columbia.edu> ping@cubmol.bio.columbia.edu (Shiping Zhang) writes:
>In article <1280001@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
>>Does anyone have a delay routine written in C?
[deleted]
>>  struct dostime_t time1, time2;
>[...]
>>  _dos_gettime(&time1); time2=time1;
>                         ^^^^^^^^^^^
>You can't assign a struct like that. You must assign the elements
>of a struct individually.
>
>
>-ping

You can perfectly and legally assign structs as long as they have the
same type as above. time1 and time2 are both of type dostime_t;

About the original question, how about defining a macro or a function
which takes as input the sleep time required in seconds, calls 'time'
the first time to deliver the current time and then looping with calls
to 'difftime' and 'time' until the sleep time is passed. Of course,
this is approximative but wouldn't matter too much if the sleep time
is big.

time_t ovalue,nvalue;

time(&ovalue);
while(difftime((nvalue = time( (time_t *) 0)), ovalue) < sleeptime) continue;

I hope that this helps, although I haven't tried it, it looks logical
unless ... Note that 'difftime' returns type double, so you have to
either pass sleeptime as double or cast it to double in above
statement. In any case, I know the dos gurus would have a neat
solution to your problem. 

Julian


-- 
				---------------------------------------
e-mail:	anigbogu@loria.crin.fr 	| All opinions expressed here are      |
				|  naturally mine. However ...         |
				----------------------------------------

darcy@druid.uucp (D'Arcy J.M. Cain) (03/16/90)

In article <1990Mar13.041457.17217@cubmol.bio.columbia.edu> ping@cubmol.bio.columbia.edu (Shiping Zhang) writes:
>In article <1280001@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
>>Does anyone have a delay routine written in C?
>
>How about sleep()?
> 
>>Here is what I have.  It works, but it isn't too consistant. 
>>If I say delay(1), sometimes I get a delay for one second, and sometimes I 
>>get a microsecond delay.
>
>[...]
>>  struct dostime_t time1, time2;
>[...]
>>  _dos_gettime(&time1); time2=time1;
>                         ^^^^^^^^^^^
>You can't assign a struct like that. You must assign the elements
>of a struct individually.
>
>
>-ping

Don't you read this newsgroup?  We just went through this discussion a
couple of weeks ago.  Of course you can assign a struct to a struct.  I
think it's time you got rid of your K&R1 and replaced it with K&R2.
-- 
D'Arcy J.M. Cain (darcy@druid)     |   Thank goodness we don't get all 
D'Arcy Cain Consulting             |   the government we pay for.
West Hill, Ontario, Canada         |
(416) 281-6094                     |