代码之家  ›  专栏  ›  技术社区  ›  Adam

Ada中的信号量

  •  8
  • Adam  · 技术社区  · 7 年前

    然而,我已经实现了 Semaphore.adb 并在 producerconsumer_sem.adb

    我得到了如下输出。

    1. 我不确定我的信号量初始化是否正确 S: CountingSemaphore(1,1);

    2. 我不知道我在哪里打电话给 S.wait S.Signal 现在我随机调用 S、 等等 X := I; 之后 X:=I; . 这是正确的方式吗?

    生产者消费者问题 producerconsumer.adb 实现不可靠的实现- 生产者-消费者问题,其中数据可能会丢失。在里面 下面,您将使用三种不同的通信机制来 实现生产者消费者问题的可靠实施。

    信号量

    然而,信号量可以通过受保护的对象来实现。在文件信号量中创建包规范信号量。广告 以及文件中相应的包体 Semaphores.adb 那个

    使用信号量包实现生产者的可靠实现- 生产者消费者。亚洲开发银行 并保存 最终代码为 . 为了使用信号量 producerconsumer_sem。亚洲开发银行 . 然后可以通过

    with Semaphores; use Semaphores;

    输出: 1. 1. 2. 3. 4. 6. 7. 7. 8. 9 9 9 10 11 11 12 13 13 13 15 16 17 18 18 20 21 22 22 24 24 24 26 27 27 29 30 31 33 33 34 35 36 37 38 38 38 40 40

    package Semaphores is
       protected type CountingSemaphore(Max: Natural; Initial: Natural)  is
          entry Wait;
          entry Signal;
       private
          Count : Natural := Initial;
          MaxCount : Natural := Max;
       end CountingSemaphore;
    end Semaphores;
    

    semaphores.adb .

    package body Semaphores is
       protected body CountingSemaphore is
       entry Wait when Count > 0 is
        begin
        Count := Count - 1;
    
        end Wait;
          entry Signal when Count < MaxCount is
        begin
        Count := Count + 1;
    
        end Signal;
       end CountingSemaphore;
    end Semaphores;
    

    producerconsumer_sem。亚洲开发银行

    with Ada.Text_IO;
    use Ada.Text_IO;
    
    with Ada.Real_Time;
    use Ada.Real_Time;
    
    with Ada.Numerics.Discrete_Random;
    
    with Semaphores;
    use Semaphores;
    
    procedure ProducerConsumer_sem is
    
       X : Integer; -- Shared Variable
       N : constant Integer := 40; -- Number of produced and comsumed variables
    
       S: CountingSemaphore(1,1);
       --S1: CountingSemaphore(1,1);
    
       pragma Volatile(X); -- For a volatile object all reads and updates of
                           -- the object as a whole are performed directly
                           -- to memory (Ada Reference Manual, C.6)
    
       --Random Delays
       subtype Delay_Interval is Integer range 50..250;
       package Random_Delay is new Ada.Numerics.Discrete_Random
       (Delay_Interval);
       use Random_Delay;
       G : Generator;
    
       task Producer;
    
       task Consumer;
    
       task body Producer is
          Next : Time;
       begin
          Next := Clock;
          for I in 1..N loop
             -- Write to X
             S.Wait;
             X := I;
             S.Signal;
             --Next 'Release' in 50..250ms
             Next := Next + Milliseconds(Random(G));
             Put_Line(Integer'Image(X));
             delay until Next;
          end loop;
       end;
    
       task body Consumer is
          Next : Time;
       begin
          Next := Clock;
          for I in 1..N loop
             -- Read from X
             S.Wait;
             Put_Line(Integer'Image(X));
             S.Signal;
             Next := Next + Milliseconds(Random(G));
             delay until Next;
          end loop;
       end;
    
    begin -- main task
    
    
       null;
    end ProducerConsumer_sem;
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Simon Wright    7 年前

    在macOS上,使用FSF GCC 7.1.0和GNAT GPL 2017,我更改了您的 Put_Line s至 Put 得到了你在问题中给出的答案。

    问题是要创造 Semaphore.ads .adb . 这可以在Windows上运行,也可以在macOS上运行,但在Linux上不起作用,因为GNAT文件命名约定(参见 this ; 养成使用小写文件名的习惯是个好主意)。

    如果要确保只有一个任务可以访问 X Wait , Signal 电话,尽管当我把一个 delay 0.1 Producer ,第一个值输出为151619216(因为 十、

    • 使用计数0(最大值为1)初始化信号量。这使其成为二进制信号量。
    • 在里面 Consumer 仅(即移除 )
    • 制作人 信号 仅(即移除 ). 此外,拆下