【问题标题】:Ada. How to set task priorities at run-time?艾达。如何在运行时设置任务优先级?
【发布时间】:2016-12-08 13:21:58
【问题描述】:

首先让我们解决这个问题。我是 Ada 的初学者,我希望能够做到这一点的原因是因为我想编写优先级反转。

我已经加入了,

with Ada.Task_Identification; 

我也做了一个任务类型:

task type tasktype1 is
      pragma PRIORITY (20);
      entry gotosleep; 
end tasktype1;

我已经宣布了一项任务:

High : tasktype1;

现在我想将任务“高”的优先级更改为其他优先级。

我试过写:

High.Prority(1); 

我会把它放在 main 的 begin 块中。

并声明了一个Task_ID。

 A : Task_Id;

然后尝试使用A := Current_Task; 获取当前任务

然后将Priority(3,A); 放在电源开始处。

这是我所有的代码供参考:

with Ada.Text_IO, Ada.Integer_Text_IO, System, Ada.Task_Identification; 
use Ada.Text_IO, Ada.Integer_Text_IO; 

procedure Main is

task type tasktype1 is
      pragma PRIORITY (20);
      entry gotosleep; 
end tasktype1;

pragma PRIORITY (3);   -- This is the priority for the main program

High : tasktype1;

  A : Task_Id;

task body tasktype1 is
   begin

accept gotosleep do 
     Put("Cow is not sleeping"); 

 end gotosleep; 
end tasktype1;




begin

   A := Current_Task; 
    Priority(3, A); 

   Put_Line("This is an example of use of a task type");
   Put_Line("This is an example of use of a task type");
   Put_Line("This is an example of use of a task type");
   Put_Line("This is an example of use of a task type");
   Put_Line("This is an example of use of a task type");

end Main;

【问题讨论】:

  • 您可以在运行时使用Ada.Dynamic_Priorities. Set_Priority 设置任务优先级 - 请参阅ARM D.5.1(注意,没有您期望的配置文件Priority 的子程序!)

标签: task ada


【解决方案1】:

见:http://www.adaic.org/resources/add_content/standards/05rm/html/RM-D-5-1.html


TLDR; (如果您希望设置/获取当前任务的优先级)

  1. 将包“与”到您的 Ada 源:

    with Ada.Dynamic_Priorities;
    
  2. 调用 Set_Priority 过程

    Ada.Dynamic_Priorities.Set_Priority(1);
    

如果你想知道当前的优先级,你可以调用

Ada.Dynamic_Priorities.Get_Priority;

【讨论】:

    【解决方案2】:

    首先,关于任务优先级,你必须检查你的操作系统能力。

    例如,在 Linux 或 Solaris 上,您只能以 root 用户身份使用优先级。否则,无论您在代码中设置什么,操作系统都会为每个任务提供相同的优先级。 在 Windows 上,我没有检查可用的任务策略是什么,但默认情况下我会说所有任务都具有相同的优先级。

    完成后,下面的解决方案应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多