【问题标题】:Can not increase Size of Shared Memory无法增加共享内存的大小
【发布时间】:2015-12-26 13:52:55
【问题描述】:

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

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

sysctl -w kernel.shmmax=2147483648

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

这是我的代码:

 #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;
}

提前非常感谢您...

【问题讨论】:

  • shmget 失败时使用perror。另请阅读并喜欢使用shm_overview(7)sem_overview(7)shmget 的 SysV 共享内存可能已被弃用,kernel.shmmax 未涵盖

标签: c shared-memory


【解决方案1】:

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

【讨论】:

  • ipcs -l 返回: ------ 共享内存限制 -------- 最大段数 = 4096 最大段大小 (kbytes) = 32768 最大总共享内存 (kbytes ) = 8388608 min seg size (bytes) = 1 ------ Semaphore Limits -------- max number of arrays = 128 max semaphores per array = 250 max semaphores systemwide = 32000 max ops per semop call = 32 信号量最大值 = 32767 ------ 消息限制 -------- 系统范围的最大队列 = 992\n 消息的最大大小(字节)= 8192 队列的默认最大大小(字节)= 16384
猜你喜欢
  • 2011-01-03
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 2013-03-04
相关资源
最近更新 更多