shmget() 给出 EINVAL 错误 [英] shmget() gives EINVAL error

查看:106
本文介绍了shmget() 给出 EINVAL 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时以来,我一直在使用 shmget 遇到问题,但我似乎无法弄清楚.每次我尝试拨打电话时都会收到 shmget() 它总是返回一个 EINVAL:"Invalid Argument" 错误.

代码的重要部分如下:

key_t generate_ipc_key(){key_t ipc_key = ftok(__FILE__, KEY);如果(ipc_key <0){perror("生成IPC密钥失败");退出(IPC_FAILURE);}返回ipc_key;}int shared_memory_create(size_t memory_size){int shm_key = shmget(generate_ipc_key(), memory_size, IPC_CREAT | 0666);如果(shm_key <0){perror("创建共享内存密钥失败");退出(CREATE_FAILURE);}返回 shm_key;}

函数被调用:

shm_key = shared_memory_create(sizeof(data_t));

其中 data_t 是一个结构体:

typedef struct {int hCount;int oCount;int bCount;} 数据_t;

另外,如果它有助于共享内存的值是:

kernal.shmmax = 33554432内核.shmall = 2097152内核.shmmni = 4096

我对 C 相当陌生,所以我确定这是我遗漏的一些简单的东西,但我似乎无法弄清楚.任何帮助表示赞赏!

我不确定它到底是什么,ipcs 没有显示共享段,我重新启动,现在它可以工作了.

解决方案

手册页说:

<块引用>

EINVAL 将创建一个新段并且大小 

因此,请检查这些是否适用.您可以运行 ipcs 工具列出现有的共享内存段,并查看您的程序是否与其中任何一个发生冲突.请注意,共享内存段在您的进程结束后仍然存在.

I've been having a problem with shmget for a few hours now that I can't seem to figure out. Every time I try to make a call got shmget() it always returns with an EINVAL: "Invalid Argument" error.

The important parts of the code are as follows:

key_t generate_ipc_key()
{
    key_t ipc_key = ftok(__FILE__, KEY);
    if(ipc_key < 0) {
        perror("Failed to Generate IPC Key");
        exit(IPC_FAILURE);
    }

     return ipc_key;
}

int shared_memory_create(size_t memory_size)
{
    int shm_key = shmget(generate_ipc_key(), memory_size, IPC_CREAT | 0666);
    if(shm_key < 0) {
        perror("Failed to Create Shared Memory Key");
        exit(CREATE_FAILURE);
    }

    return shm_key;
}

The function is being called with:

shm_key = shared_memory_create(sizeof(data_t));

Where data_t is a struct:

typedef struct {
    int hCount;
    int oCount;
    int bCount;
} data_t;

Also in case it helps the values for shared memory are:

kernal.shmmax = 33554432
kernal.shmall = 2097152
kernal.shmmni = 4096

I'm fairly new to C so I'm sure it's something simple I'm missing however I can't seem to figure it out. Any help is appreciated!

I'm not sure exactly what it was, ipcs showed no shared segments and I rebooted and now it works.

解决方案

The man page says:

EINVAL A new segment was to be created and size < SHMMIN or 
size > SHMMAX, or no new segment was to be created, a 
segment with given key existed, but size is greater than 
the size of that segment.

So, check if any of these applies. You can run the ipcs tool to list existing shared memory segments, and see if your program clashes with any of these. Note that the shared memory segments persists after your process ends.

这篇关于shmget() 给出 EINVAL 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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