当前位置: 技术问答>linux和unix
linux中的共享内存问题
来源: 互联网 发布时间:2016-05-28
本文导语: 我在开如定义了一个发下结构,用于读写、写入共享内存; strcut shm_com { int flag; char some_text[TEXT_SZ];// text_sz = 2048; }; int main() { int shmid ; void *share_memory; struct shm_com share_stuff; shmid = shm...
我在开如定义了一个发下结构,用于读写、写入共享内存;
strcut shm_com
{
int flag;
char some_text[TEXT_SZ];// text_sz = 2048;
};
int main()
{
int shmid ;
void *share_memory;
struct shm_com share_stuff;
shmid = shmget((key_t)1234,TEXT_SZ,0660|IPC_CREAT);
if(shmid == -1)
{ printf("shmid Failedn");
exit(EXIT_FAILURE);
}
share_memory = shmat(shmid,(void *)0,0);
if(share_memory == (void *) -1)
{
printf("shmat Failedn");
exit(EXIT_FAILURE);
}
share_stuff = (struct shm_com *)share_memory;
share_sutff->flag = 23;
fgets(share_stuff->some_text,TEXT_SZ,stdin);
printf("flag was %dn",share_stuff->flag);
printf("some_text was %sn",share_stuff->some_text);
}
在编译的时候通不过,主要问题在红色标记的地方,请赐教?
strcut shm_com
{
int flag;
char some_text[TEXT_SZ];// text_sz = 2048;
};
int main()
{
int shmid ;
void *share_memory;
struct shm_com share_stuff;
shmid = shmget((key_t)1234,TEXT_SZ,0660|IPC_CREAT);
if(shmid == -1)
{ printf("shmid Failedn");
exit(EXIT_FAILURE);
}
share_memory = shmat(shmid,(void *)0,0);
if(share_memory == (void *) -1)
{
printf("shmat Failedn");
exit(EXIT_FAILURE);
}
share_stuff = (struct shm_com *)share_memory;
share_sutff->flag = 23;
fgets(share_stuff->some_text,TEXT_SZ,stdin);
printf("flag was %dn",share_stuff->flag);
printf("some_text was %sn",share_stuff->some_text);
}
在编译的时候通不过,主要问题在红色标记的地方,请赐教?
|
share_stuff = *(struct shm_com *)share_memory; //此处由指针赋值给结构
share_stuff.flag = 23; //此处拼写错误,另外'->'应该为'.'下同
fgets(share_stuff.some_text,TEXT_SZ,stdin);
printf("flag was %dn",share_stuff.flag);
printf("some_text was %sn",share_stuff.some_text);
share_stuff.flag = 23; //此处拼写错误,另外'->'应该为'.'下同
fgets(share_stuff.some_text,TEXT_SZ,stdin);
printf("flag was %dn",share_stuff.flag);
printf("some_text was %sn",share_stuff.some_text);
|
#include
#include
#include
#include
#define TEXT_SZ 2048
struct shm_com
{
int flag;
char some_text[TEXT_SZ];// text_sz = 2048;
};
int main()
{
int shmid ;
void *share_memory;
struct shm_com share_stuff;
shmid = shmget((key_t)1234,TEXT_SZ,0660|IPC_CREAT);
if(shmid == -1)
{ printf("shmid Failedn");
exit(EXIT_FAILURE);
}
share_memory = shmat(shmid,(void *)0,0);
if(share_memory == (void *) -1)
{
printf("shmat Failedn");
exit(EXIT_FAILURE);
}
share_stuff = *(struct shm_com *)share_memory;
share_stuff.flag = 23;
fgets(share_stuff.some_text,TEXT_SZ,stdin);
printf("flag was %dn",share_stuff.flag);
printf("some_text was %sn",share_stuff.some_text);
return 0;
}