2016-12-02 1121 views
0
int shmid; 
int* locat; 

//create shared memory segment 
shmid = shmget(6666, size, 0666); 
if (shmid < 0) { 
    perror("shmget"); 
    exit(1); 
} 

locat = (int *) shmat(shmid, NULL, 0); 
if (locat == (int *) -1) { 
    perror("shmat"); 
    exit(1); 
} 

我设立共享内存作为这样的,但我不断收到此错误:shmget: No such file or directory共享内存在C:shmget的问题

此代码工作正常,不知道为什么现在这个发生。

+0

您的共享内存创建失败,'shmget'返回'-1'。看看http://stackoverflow.com/questions/7495326/understanding-shared-memory-using-c – MrKiwi

回答

1

As the man says

IPC_CREAT

Create a new segment. If this flag is not used, then shmget() will find the segment associated with key and check to see if the user has permission to access the segment.

您必须添加IPC_CREATshmget通话

shmid = shmget(6666, size, IPC_CREAT | 0666); 

您也可以使用IPC_EXCL,以确保该段为新建

IPC_EXCL

This flag is used with IPC_CREAT to ensure that this call creates the segment. If the segment already exists, the call fails.

1

有是两件事情:

  1. 当你想初始化一个共享内存(对应于一个特定的键值)时,你必须用IPC_CREAT或者权限号。

就像

shmget(6666 , size , 0666|IPC_CREAT); 
  • 当要附加相同的段(由密钥值来标识),以另一种方法,IPC_CREAT不是强制性的作为共享内存已经创建了逻辑地址空间。