我得到了以下代码并要求实现一个信号量。
with Ada.Text_IO; use Ada.Text_IO;
with Id_Dispenser;
with Semaphores; use Semaphores;
procedure Philos is
No_of_Philos : constant Positive := 5;
Meditation : constant Duration := 0.0;
type Table_Ix is mod No_of_Philos;
Forks : array (Table_Ix) of Binary_Semaphore (Initially_Available => True);
package Index_Dispenser is new Id_Dispenser (Element => Table_Ix);
use Index_Dispenser;
task type Philo;
task body Philo is
Philo_Nr : Table_Ix;
begin
Dispenser.Draw_Id (Id => Philo_Nr);
Put_Line (“Philosopher” & Table_Ix’Image (Philo_Nr) & “ looks for forks.”);
Forks (Philo_Nr).Wait; delay Meditation; Forks (Philo_Nr + 1).Wait;
Put_Line (“Philosopher” & Table_Ix’Image (Philo_Nr) & “ eats.”);
Forks (Philo_Nr).Signal; Forks (Philo_Nr + 1).Signal;
Put_Line (“Philosopher” & Table_Ix’Image (Philo_Nr) & “ dropped forks.”);
end Philo;
Table : array (Table_Ix) of Philo; pragma Unreferenced (Table);
begin
null;
end Philos;
该任务需要一个Semaphores
包和包Id_Dispenser
。我对 Ada 很陌生,但是包是什么意思?这是否意味着规范和主体,或者仅意味着其中之一,我该如何实现这一点?
至于“什么是包”,请查看Ada Wikibook 中的包部分.
所有包装都有规格部分。大多数也有一个主体(如果规范承诺有一个主体,例如通过声明一个子程序,那么实际上必须有一个主体)。
您可以在 Wikibook 中找到有关实现信号量的讨论任务分配部分,包括代码Semaphore_Protected_Type
.
在你的情况下,你需要
package Semaphores is
protected type Binary_Semaphore (Initially_Available : Boolean) is
entry Wait;
procedure Signal;
private
Available : Boolean := Initially_Available;
end Binary_Semaphore;
...
end Semaphores;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)