gpt4 book ai didi

embedded - Ada 中的信号量

转载 作者:行者123 更新时间:2023-12-04 15:18:24 25 4
gpt4 key购买 nike

这是一项任务,我被要求在 Ada 中实现信号量,如下所述。

但是我已经实现了 Semaphore.adb并在 producerconsumer_sem.adb 中调用此信号量我创建的。

我得到一些输出,如下所示。

  • 我不确定我的信号量初始化是否正确S: CountingSemaphore(1,1); .
  • 我不知道我在哪里打电话 S.waitS.Signal现在我随机调用了S.wait在生产者将项目放入缓冲区之前X := I;S.SignalX := I; 之后.
    这是正确的方法吗?

  • Producer-Consumer Problem The program producerconsumer.adb implements a non-reliable implemen- tation of the producer-consumer problem, where data is likely be lost. In the following, you will use three different communication mechanisms to achieve a reliable implementation of the producer-consumer problem.

    Semaphore

    The Ada language does not directly provide library functions for a semaphore. However, semaphores can be implemented by means of a protected object. Create a package specification Semaphore in the file Semaphores.ads and the corresponding package body in the file Semaphores.adb that implements a counting semaphore. Skeletons for the package are available on the course page.

    Use the semaphore package for a reliable implementation of the producer- consumer problem. Modify the file producerconsumer.adb and save the final code as producerconsumer_sem.adb. In order to use the semaphore package it shall be installed in the same directory as producerconsumer_sem.adb. It can then be accessed by

    with Semaphores; use Semaphores;



    输出:

    OutPut: 1 1 1 2 2 3 4 4 5 6 6 7 7 8 9 9 9 10 11 11 11 12 12 13 13 13 14 15 15 16 16 17 18 18 18 19 20 20 21 21 22 22 23 24 24 24 24 25 25 26 27 27 28 29 29 30 30 31 31 32 32 33 33 33 34 35 35 35 36 36 37 37 37 38 38 38 39 40 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.adb
    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;

    最佳答案

    在 macOS 上,使用 FSF GCC 7.1.0 和 GNAT GPL 2017,我更改了您的 Put_Line转至 Put s 并得到了您在问题中陈述的几乎所有答案。

    问题说要创建 Semaphore.ads , .adb .这适用于 Windows,可能适用于 macOS,但不适用于 Linux,因为 GNAT 的文件命名约定(见 this 的结尾;养成使用小写文件的习惯是个好主意名称)。

    如果您想确保只有一项任务可以访问 X一次,我不认为你的 Wait 有什么问题, Signal电话,虽然当我把 delay 0.1 Producer 的开头,输出的第一个值是 151619216(因为 X 未初始化)。然而!如果重点是一次向 X 传达一个更新(正如名称生产者/消费者所暗示的那样),您应该

  • 初始化信号量,计数为 0(最大值为 1)。这使它成为一个二进制信号量。
  • Consumer , Wait仅(即删除 Signal )
  • Producer , Signal仅(即删除 Wait )。此外,删除 Put以免混淆!
  • 关于embedded - Ada 中的信号量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46211114/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com