无法增加共享内存的大小 [英] Can not increase Size of Shared Memory

查看:84
本文介绍了无法增加共享内存的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我吗?我无法增加 Sherd Memory 的大小.代码是在 Linux 上用 C 编写的.

Could you please help me? I can not increase the size of my Sherd Memory. The code is written in C on Linux.

我需要 65536 个字节,但似乎只允许 49152 个字节...如果我增加它,shmget 将失败...(在我的代码中:shmid <0)我试图找出我的最大共享内存大小并通过以下方式增加它:

I need 65536 bytes, but just 49152 seem to be allowed... If I increase it, shmget fails...(in my code: shmid < 0) I tried to find out my maximum shared memory size and increased it with:

sysctl -w kernel.shmmax=2147483648

但这无济于事,初始化再次失败.

But that doesn't help, the initialization again fails.

这是我的代码:

 #define SHM_KEY                 9877

 #define SHM_SIZE                65536

int SHM_init (int shmid, char** shm, key_t key, long int size) {

    /* Create a new (System V) shared memory segment of the specified size */
    shmid = shmget(key, SHM_SIZE, IPC_CREAT|0777);

    /* Check if SHM creation was successful */
    if (shmid < 0) {
        /* DBG: Debug message to show which point of the program has been passed */
        DBG_PRINT("C\n");

        /* Check if creation failed because of already existing SHM */
        if (EEXIST == errno) {
            /* DBG: Debug message to show which point of the program has been passed */
            DBG_PRINT("CC\n");
            /* Delete already existing SHM with shmctl */
            shmctl(shmid, IPC_RMID, NULL);
        } else {
            /* DBG: Debug message to show which point of the program has been passed */
            DBG_PRINT("CCC\n");
        }

        /* Creation and initialization of SHM failed */
        return -1;
    }
    /* Attach the SHM data pointer to the previously created SHM segment */
    *shm = shmat(shmid, NULL, 0);

    if(*shm == (char *) -1) {
        /* Attaching failed */
        return -1;
    }
    DBG_PRINT("Shared Memory Initialization successful\n");
    /* Creation and initialization of shared memory was successful */
    return 0;
}

在此先感谢您...

推荐答案

本主题 可能会有所帮助.如果使用 sysctl -w kernel.shmmax=2147483648 增加 shmmax,ipcs -l 会返回什么?

This topic might help. What does ipcs -l return if you increase the shmmax with sysctl -w kernel.shmmax=2147483648 ?

这篇关于无法增加共享内存的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆