尝试附加共享内存的已用地址时出错

发布于 2024-12-13 04:22:35 字数 1146 浏览 0 评论 0原文

使用 shmget 且第二个参数不为 NULL 时收到消息“Invalid argument”。

它编译正常,但是在执行时,我收到此错误消息。

我一整天都被这个问题困住了。希望你能帮助我! :)

#include <sys/ipc.h>        
#include <sys/shm.h>        
#include <stdlib.h>     
#include <stdio.h>          


int main()
{
    int idSharedMem;
    int *varSharedMem1;
    int *varSharedMem2;

    /* Create the shared memory */
    idSharedMem = shmget((key_t) 0001, sizeof(int), IPC_CREAT | 0666);

    if (idSharedMem == -1)
    {
        perror("shmget");
    }

    /* Allocate a memory address and attached it to a variable */
    varSharedMem1 = shmat(idSharedMem, NULL, 0);

    if (varSharedMem1 == (int *) -1)
    {
        perror("shmat1");
    }

    /* Sign a value to the variable */
    *varSharedMem1 = 5;

    /* Attach an existing allocated memory to another variable */
    varSharedMem2 = shmat(idSharedMem, varSharedMem1, 0);

    if (varSharedMem2 == (int *) -1)
    {
        /* PRINTS "shmat2: Invalid argument" */
        perror("shmat2");
    }

    /* Wanted it to print 5 */
    printf("Recovered value %d\n", *varSharedMem2);

    return(0);
}

Receiving the message "Invalid argument" when using shmget with the second parameter not being NULL.

It compiles ok, but when executing, I get this error message.

I've been stuck on this all day long. Hope you can help me! :)

#include <sys/ipc.h>        
#include <sys/shm.h>        
#include <stdlib.h>     
#include <stdio.h>          


int main()
{
    int idSharedMem;
    int *varSharedMem1;
    int *varSharedMem2;

    /* Create the shared memory */
    idSharedMem = shmget((key_t) 0001, sizeof(int), IPC_CREAT | 0666);

    if (idSharedMem == -1)
    {
        perror("shmget");
    }

    /* Allocate a memory address and attached it to a variable */
    varSharedMem1 = shmat(idSharedMem, NULL, 0);

    if (varSharedMem1 == (int *) -1)
    {
        perror("shmat1");
    }

    /* Sign a value to the variable */
    *varSharedMem1 = 5;

    /* Attach an existing allocated memory to another variable */
    varSharedMem2 = shmat(idSharedMem, varSharedMem1, 0);

    if (varSharedMem2 == (int *) -1)
    {
        /* PRINTS "shmat2: Invalid argument" */
        perror("shmat2");
    }

    /* Wanted it to print 5 */
    printf("Recovered value %d\n", *varSharedMem2);

    return(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-12-20 04:22:35

使用 shmat(idSharedMem, varSharedMem1, 0);,您尝试将段附加到 varSharedMem1 的位置。但是,您之前已在该位置附加了一个段,这将导致 EINVAL。 Linux 提供了 SHM_REMAP 标志,您可以使用它来替换以前映射的段。

shmat 联机帮助页:

(Linux 特定的)SHM_REMAP 标志可以在 shmflg 中指定
指示该段的映射应替换任何现有的
映射范围从 shmaddr 开始并继续
段的大小。 (如果此地址范围中已存在映射,通常会导致 EINVAL 错误。)
这种情况下,shmaddr 不能为 NULL。

With shmat(idSharedMem, varSharedMem1, 0); you're trying to attach the segment at the location of varSharedMem1. However you have previously attached a segment at that location which will result in EINVAL. Linux provides a SHM_REMAP flag you can use to replace previously mapped segments.

shmat manpage:

The (Linux-specific) SHM_REMAP flag may be specified in shmflg to
indicate that the mapping of the segment should replace any existing
mapping in the range starting at shmaddr and continuing for
the size of the segment. (Normally an EINVAL error would result if a mapping already exists in this address range.) In this
case, shmaddr must not be NULL.

独孤求败 2024-12-20 04:22:35

来自 shmat 手册页:

If shmaddr isn't NULL and SHM_RND is specified in shmflg, the attach occurs at the
address equal to shmaddr rounded  down  to  the nearest multiple of SHMLBA. 
Otherwise shmaddr must be a page-aligned address at which the attach occurs.

更多来自 shmat 手册页:

Using shmat() with shmaddr equal to NULL is the preferred, portable way of 
attaching a shared memory segment.

要进行第二个附加,只需:

varSharedMem2 = shmat(idSharedMem, NULL, 0);

From shmat man page:

If shmaddr isn't NULL and SHM_RND is specified in shmflg, the attach occurs at the
address equal to shmaddr rounded  down  to  the nearest multiple of SHMLBA. 
Otherwise shmaddr must be a page-aligned address at which the attach occurs.

more from shmat man page:

Using shmat() with shmaddr equal to NULL is the preferred, portable way of 
attaching a shared memory segment.

To make a second attach, just:

varSharedMem2 = shmat(idSharedMem, NULL, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文