首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Ada中使用Linux将任意字符串写入共享内存并从中读取?

如何在Ada中使用Linux将任意字符串写入共享内存并从中读取?
EN

Stack Overflow用户
提问于 2018-08-07 00:56:37
回答 1查看 177关注 0票数 2

我是Ada的初学者,网上的大部分资源都是用C语言编写的,我很难把它翻译成Ada。

我应该将POSIX与shmget和shmat一起使用,还是应该将SysV shm与mmap和shm_open一起使用?

你能给我一个使用这两个过程(写,然后读)的Ada程序的例子吗?例如,假设我想写,然后读取字符串"Butterflies“。

万分感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-08-07 04:23:07

有几种方法可以做到这一点。也许最简单的是内存覆盖。假设您为$33FF保留了一块内存$3300,您可以使用$3300处的字节来指示字符串的长度,使用$3301..$33FF作为字符串的内容。

代码语言:javascript
复制
With Interfaces;
    Package ShortString is
        Type String( Length : Interfaces.Unsigned_8 ) is private;

        -- Convert a shortstring to a standard string.
        Function "+"( Input : String ) Return Standard.String;

        -- Convert a standard string to a short-string.
        Function "+"( Input : Standard.String ) Return String
          with Pre => Input'Length <= Positive(Interfaces.Unsigned_8'Last);

        Private

        -- Declare a Positive subtype for a byte.
        Subtype Positive is Interfaces.Unsigned_8 range 1..Interfaces.Unsigned_8'Last;

        -- Use the byte-sized positive for indexing the short-string.
        Type Internal is Array(Positive range <>) of Character;

        -- Declare a varying-length record for the short-string implementation.
        Type String( Length : Interfaces.Unsigned_8 ) is record
            Data : Internal(1..Length);
        end record;

        -- We must ensure the first byte is the length.
        For String use record
          Length at 0 range 0..7;
        end record;

        Function "+"( Input : String ) Return Standard.String is
          ( Standard.String(Input.Data) );

        Function "+"( Input : Standard.String ) Return String is
          ( Length => Interfaces.Unsigned_8(Input'Length),
            Data   => Internal( Input )
          );
    End ShortString;

然后对于内存覆盖:

代码语言:javascript
复制
Overlayed_String : ShortString.String(255)
  with Import, Address => System.Storage_Elements.To_Address( 16#3300# );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51712397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档