linux c 进程间通讯,shmat 共享内存之后,memset段错误,求教5555

zzzzluo 2011-08-23 11:13:14
linux c 进程间通讯,shmat 共享内存之后,memset段错误,求教5555
shmat 返回的地址ok的
使用gdb调试,在memset的时候Program received signal SIGSEGV, Segmentation fault
如果去掉memset ,到了读写内存的时候也同样是段错误,求教。。谢谢
typedef struct
{
char a[16];
char b[16];
}Strutest;
typedef struct
{
int indx;
Strutest S8testData[1024];
}StruAlltest;

StruAlltest *gMemtest;
int s32Index,s32ID;
unsigned int u32Addr;

s32ID = shmget(s32Index,u32Size,IPC_CREAT|0777);
if(s32ID == -1)
{
perror("shmget error");
//printf("shmget error.\n");
return(NULL);
}

u32Addr = (unsigned int)shmat(s32ID,NULL,0);
gMemtest= (StruAlltest*)u32Addr;
memset(gMemtest,0,u32Size);

...全文
741 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
不辣 2011-08-23
  • 打赏
  • 举报
回复
试试把
memset(gMemtest,0,u32Size);

改为:memset((char*)gMemtest,0,u32Size);
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
u32Addr都从没等于-1过~
hacqing 2011-08-23
  • 打赏
  • 举报
回复
u32Addr = (unsigned int)shmat(s32ID,NULL,0);

判断u32Addr是否为-1
Upon successful completion, shmat() shall increment the value of shm_nattch in the data structure associated with the shared memory ID of the attached shared memory segment and return the segment's start address.

Otherwise, the shared memory segment shall not be attached, shmat() shall return -1, and errno shall be set to indicate the error.

zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
看了很多书本范例,都没有关于初始化的。。。创建共享内存 ,总要有个进程最先初始化吧。。
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
为了简洁,大家好看,代码贴了一部分而已。。我补充下


不好意思啊。。呵呵
s32Index = ftok("test",0);
if(s32Index == -1)
{ printf("ftok error.\n");
return(NULL);
}
jackyjkchen 2011-08-23
  • 打赏
  • 举报
回复
我觉得你把未初始化的s32Index传进去是有问题的
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
if(gMemtest== NULL) return(-1);
printf("%x\n",gMemtest);//699ce000 有打印出地址
jackyjkchen 2011-08-23
  • 打赏
  • 举报
回复
shmat是有可能失败的
qq120848369 2011-08-23
  • 打赏
  • 举报
回复
你没有检查过u32Addr是否为空就memset,于是一切皆有可能。
不辣 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 zzzzluo 的回复:]
s32ID = shmget(s32Index,u32Size * 2,IPC_CREAT|0777);//
*2 要去掉

我原来的程序

后面补上*2 能运行。。读写都ok
后面换上你最后给我的代码。。好像不用*2也OK,。。。奇怪
[/Quote]

shmget()的第一个参数就是要申请内存的大小,跟malloc类似,不必过于纠结,管理好内存就行
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
s32ID = shmget(s32Index,u32Size * 2,IPC_CREAT|0777);//
*2 要去掉

我原来的程序

后面补上*2 能运行。。读写都ok
后面换上你最后给我的代码。。好像不用*2也OK,。。。奇怪
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
谢谢了。。让我的理解更进一步。。呵呵
看来我的程序哪里有问题。。指针没转换好

sprintf(c8Buf,"%sSMemGprs",SHARTMEM_PATH); //gprs
s32size = sizeof(*gMemGprsData);
printf("size %d\n",s32size);
gMemGprsData = (StruAllGprsData *)CreatShareMem(c8Buf,sizeof(StruAllGprsData));
if(gMemGprsData == NULL) return(-1);
printf("%x\n",gMemGprsData);
memset(gMemGprsData,0,s32size);
//创建共享内存
void *CreatShareMem(char *c8PName,unsigned int u32Size)
{
int s32Index,s32ID;
char *u32Addr;
//unsigned int u32Addr;
FILE *fp;

fp = fopen(c8PName,"rb"); //创建文件
if(fp == NULL)
{ fp = fopen(c8PName,"wb");
if(fp == NULL)
{ printf("Creat file %s error.\n",c8PName);
return(NULL);
}
}
fclose(fp);

s32Index = ftok(c8PName,0);
if(s32Index == -1)
{ printf("ftok error.\n");
return(NULL);
}

s32ID = shmget(s32Index,u32Size * 2,IPC_CREAT|0777);//IPC_CREAT|IPC_EXCL|0600
if(s32ID == -1)
{
perror("shmget error");
//printf("shmget error.\n");
return(NULL);
}

u32Addr = (char*)shmat(s32ID,NULL,0);

//u32Addr += 3;
//u32Addr &= 0xFFFFFFFC;

printf("Share mem %s addr=0x%X,size=%d\r\n",c8PName,u32Addr,u32Size);

return((void *)u32Addr);
}
不辣 2011-08-23
  • 打赏
  • 举报
回复

#include <sys/shm.h>
#include <string.h>

typedef struct
{
char a[16];
char b[16];
}Strutest;

typedef struct
{
int indx;
Strutest S8testData[1024];
}StruAlltest;

int main(int argc, char **argv)
{
StruAlltest *gMemtest;
int s32Index = 0x000010221, s32ID;
// unsigned int u32Addr;
char *u32Addr;

// s32ID = shmget(s32Index,u32Size,IPC_CREAT|0777);
s32ID = shmget(s32Index, sizeof(StruAlltest) , IPC_CREAT|0777);
if(s32ID == -1)
{
perror("shmget error");
return -1;
}

u32Addr = (char*)shmat(s32ID,NULL,0);
gMemtest= (StruAlltest*)u32Addr;
memset(gMemtest,0,sizeof(StruAlltest) );
return 0;
}
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
为什么sizeof(StruAlltest)这样的大小去 ,memset有段错误。。我还是很郁闷。。
因为我在arm7 和arm9上面memset是ok的。。
不辣 2011-08-23
  • 打赏
  • 举报
回复
s32ID = shmget(s32Index, sizeof(StruAlltest) * 2, IPC_CREAT|0777);
memset(gMemtest,0,sizeof(StruAlltest) * 2);


要一致!
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
但是为什么我只要申请我定义的结构那么大的内存就不行了呢?我之前以为内存溢出,有尝试size+ 1 之类的。。好像也不行
size不能刚刚好吗?
不辣 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zzzzluo 的回复:]
楼上的,你好。。测试了你的代码。。ok的
请教下int s32Index = 0x000010221,这个值的定义有什么特殊的吗?
sizeof为什么*2?
[/Quote]

int s32Index = 0x000010221 是共享内存的KEY,用16进制,是为了方便用命令 ipcs 查到自己创建的共享内存,因为ipcs下显示的是16进制

size*2 只是随便举得例子,填自己想要申请的共享内存的大小

zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
测试了下。。只要size*2 就可以了。。难道因为是机器的问题,32位?64位?
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
如果定义的结构不一样,key就要不一样吗?size为什么放大一倍呢?
zzzzluo 2011-08-23
  • 打赏
  • 举报
回复
楼上的,你好。。测试了你的代码。。ok的
请教下int s32Index = 0x000010221,这个值的定义有什么特殊的吗?
sizeof为什么*2?
加载更多回复(3)

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧