[GAP] Periodic task

Jean-Pierre Rosen rosen at adalog.fr
Fri Sep 15 08:46:17 CEST 2006


Daniel Lewandowski a écrit :
> Hello,
> 
> I'm trying to write a periodic task. It has to send a UDP packet for
> every 10 ms time slice. But an obvious solution:
> 
> 	declare
> 		Slice : Time_Span := Milliseconds (10);
> 		Slot : Time := Clock;
> 
> 	...	
> 
> 	loop
> 		-- send packet
>            	Send_Socket (Socket, Token, Last);
> 
> 		-- wait until next slot time
> 		Slot := Slot + Slice;
> 	        delay until Slot;
>         end loop;
> 
> doesn't work properly. Time period between sending packet is 7 - 20
> millisconds.
> 
> How can I make a properly working periodic task?
> 
Taking longer cannot be avoided: it depends on what else the machine is 
doing. Only if you are running on an embedded target, without other 
tasks, can you expect to have (almost) exact delay.

Taking *less* than 10 ms can be explained if Send_Socket takes a 
variable time, which is not neglectible compared to the period. For example:

T=0 enter Send_Socket
T=6 Packet sent
sleep until T = 10
T=10 enter Send_Socket
T=13 Packet sent

Bingo! you think the delay was wrong.
It all depends if you want to include the time for Send_Socket in the 
period, which will guarantee an interval of 10 ms on the average, or if 
you want to make sure that there are at least 10 ms between two 
messages. In the latter case, you should use:
    delay until Clock+Slice;

-- 
---------------------------------------------------------
            J-P. Rosen (rosen at adalog.fr)
Visit Adalog's web site at http://www.adalog.fr


More information about the GAP mailing list