fork download
  1. --Cyclic scheduler with a watchdog:
  2.  
  3. with Ada.Calendar;
  4. with Ada.Text_IO, Ada.Float_Text_IO;
  5. with Ada.Numerics.Float_Random;
  6.  
  7. use Ada.Calendar;
  8. use Ada.Text_IO, Ada.Float_Text_IO;
  9. use Ada.Numerics.Float_Random;
  10.  
  11. -- add packages to use random number generator
  12.  
  13.  
  14. procedure cyclic_wd is
  15. Message: constant String := "Cyclic scheduler with watchdog";
  16. -- change/add your declarations here
  17. d: Duration := 1.0;
  18. Start_Time: Time := Clock;
  19. Start_Test: Time;
  20. i: Integer := 0;
  21. d2: Duration := 0.5;
  22. Rounded_Time: Float := 0.0;
  23. d3: Duration := 0.0;
  24. -- Random Number Generator
  25. G: Generator;
  26. Rand_Num: Float;
  27.  
  28.  
  29. procedure f1 is
  30. Message: constant String := "f1 executing, time is now";
  31. begin
  32. Put(Message);
  33. Put_Line(Duration'Image(Clock - Start_Time));
  34. end f1;
  35.  
  36. procedure f2 is
  37. Message: constant String := "f2 executing, time is now";
  38. begin
  39. Put(Message);
  40. Put_Line(Duration'Image(Clock - Start_Time));
  41. end f2;
  42.  
  43. procedure f3 is
  44. Message: constant String := "f3 executing, time is now";
  45. begin
  46. Reset(G);
  47. Rand_Num := Random(G);
  48. delay Duration(Rand_Num);
  49. Put(Message);
  50. Put_Line(Duration'Image(Clock - Start_Time));
  51. -- add a random delay here
  52. -- Put("Random Time Float Delay is");
  53. --Put_Line(Float'Image(Rand_Num));
  54. delay Duration(Rand_Num);
  55. end f3;
  56.  
  57. task Watchdog is
  58. -- add your task entries for communication
  59. entry deadline;
  60. end Watchdog;
  61.  
  62. task body Watchdog is
  63. begin
  64. loop
  65. -- add your task code inside this loop
  66.  
  67. Rounded_Time := Float'Ceiling(Float(Clock - Start_Time));
  68. d3 := Duration(Rounded_Time - Float(Clock - Start_Time));
  69. select
  70. accept deadline;
  71. or
  72. delay Duration(Clock - Start_Time) + 0.5;
  73. -- Put_Line(Duration'Image(Clock - Start_Time));
  74. Put_Line("The deadline has passed.");
  75. end select;
  76. delay d3;
  77. end loop;
  78. end Watchdog;
  79.  
  80. begin
  81. loop
  82. -- change/add your code inside this loop
  83. if i mod 2 = 0 then
  84. f1;
  85. f2;
  86. Watchdog.deadline;
  87. f3;
  88. i := i + 1;
  89. delay d2;
  90. else
  91. f1;
  92. f2;
  93. i := i + 1;
  94. delay d;
  95. end if;
  96. end loop;
  97. end cyclic_wd;
Time limit exceeded #stdin #stdout 5s 25800KB
stdin
Standard input is empty
stdout
f1 executing, time is now 0.000059000
f2 executing, time is now 0.000072000
f3 executing, time is now 0.668021000
f1 executing, time is now 1.836060000
f2 executing, time is now 1.836080000
The deadline has passed.
f1 executing, time is now 2.836197000
f2 executing, time is now 2.836223000
f3 executing, time is now 3.867820000
f1 executing, time is now 4.735313000
f2 executing, time is now 4.735327000
f1 executing, time is now 5.735417000
f2 executing, time is now 5.735431000
f3 executing, time is now 6.655352000
f1 executing, time is now 8.075343000
f2 executing, time is now 8.075357000
f1 executing, time is now 9.075445000
f2 executing, time is now 9.075458000
f3 executing, time is now 9.501139000