Shmget: Invalid argument. Why i get this error?

本文解决了一个关于使用POSIX共享内存接口时出现的“shmget: Invalid argument”错误的问题。通过引用manual page中的相关信息,指出问题可能源于尝试创建的共享内存段大小与已存在的段不匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question:

I get a "shmget: Invalid argument error" while i try to execute this part of code

  int *nFS, *spb, *cell1, shmid;
  key_t key = 5768; 

  //i need a shared memory segment in which i can put 3 ints
  if ((shmid = shmget(key, (sizeof(int) * 3), IPC_CREAT | 0666)) < 0 ) {
             perror("shmget");
             exit(1);
       } 
 if ((spb = (int)shmat(shmid, NULL, 0))== -1 ){  
            perror("shmat");
            exit(1);
      }
  cell1= spb + 1 ;
  nFS= cell1 + 1;
  //i try to assign here 7 to  nFS
  *nFS=7;

there is something wrong here but i can't figure out what. Can you help me?

Answer:

From the shmget(1) man page:

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.

You should check whether you still have an segment for this key using ipcs and remove it with ipcrm.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值