【发布时间】:2012-12-11 11:02:12
【问题描述】:
以下代码总是返回-1 用于创建共享内存。我不知道它的原因。据我所知,我的代码是正确的。 Perror returns not such file or directory。我不知道它指向什么,但是这个文件和标题在同一个目录中。代码如下:
#include "MyShared.h"
int main()
{
struct MyShared *obj;
int shmid,i,childpid;
shmid=shmget(MySharedKey,sizeof(struct MyShared),PERM);
if(shmid==-1)
printf("Failed to create shared mem\n");
obj=(struct MyShared*)shmat(shmid,NULL,0);
obj->ReadFromBuf=0;
....
}
这是头文件。 My Shared.h
#ifndef MYSHARED_H_INCLUDED
#define MYSHARED_H_INCLUDED
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PERM (S_IRWXU | S_IRGRP)
#define MySharedKey 564732
#define SIZE 512 // 512 bytes
struct MyShared
{
char buf[SIZE];
int ReadFromBuf,WriteToBuf,readbytes;
};
#endif
为什么这段代码不能创建共享内存?
我使用的是 ubuntu 10.04。
我正在关注Unix System programming by Stevens,它没有说明任何内容或共享内存的创建权限。
问候
【问题讨论】:
-
哪个系统调用得到错误返回?得到错误返回的errno是什么?
标签: c unix ubuntu shared-memory