Shmget:参数无效。为什么我会收到此错误?

时间:2011-12-08 10:27:37

标签: c memory pointers shared

当我尝试执行这部分代码时,我得到了“shmget:无效的参数错误”

  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;

这里有问题,但我无法弄清楚是什么。你能帮助我吗?

谢谢,Alex。

1 个答案:

答案 0 :(得分:12)

从shmget(1)手册页:

  

EINVAL将创建一个新细分并且尺寸< SHMMIN或尺寸&gt;   SHMMAX,或者没有新的细分市场                 已创建,具有给定键的段,但大小大于该段的大小。

您应该使用ipcs检查是否仍有此密钥的细分,并使用ipcrm将其删除。

相关问题