【发布时间】:2016-02-24 19:33:06
【问题描述】:
我创建了两个程序 server.c 和 client.c。我有一个包含年龄的结构。我已经让程序一起工作以读取共享内存并更改共享内存,但这仅在使用结构中的一个变量时才有效。一旦结构中有多个变量,就会出现分段错误。
Server.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct People
{
int age;
int isDone;
} Person;
int main()
{
Person aaron;
Person *p_aaron;
int id;
int key = 5432;
p_aaron = &aaron;
(*p_aaron).age = 19;
(*p_aaron).isDone = 0;
if ((id = shmget(key,sizeof(aaron), IPC_CREAT | 0666)) < 0)
{
perror("SHMGET");
exit(1);
}
if((p_aaron = shmat(id, NULL, 0)) == (Person *) -1)
{
perror("SHMAT");
exit(1);
}
(*p_aaron).age = 19;
printf("Shared Memory Age: %d\n", (*p_aaron).age);
*p_aaron = aaron;
while ((*p_aaron).age == 19)
{
sleep(1);
}
printf("Shared Memory Age Turned To: %d", (*p_aaron).age);
return 0;
}
Client.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
typedef struct People
{
int age;
} Person;
int main()
{
Person aaron;
Person *p_aaron;
int id;
int key = 5432;
p_aaron = &aaron;
id = shmget(key,sizeof(aaron), IPC_CREAT | 0644);
p_aaron = shmat(id, NULL, 0);
printf("%d", (*p_aaron).age);
(*p_aaron).age = 21;
return 0;
}
来自 Server.c 的错误消息
SHMGET: Invalid argument
RUN FINISHED; exit value 1; real time: 0ms; user: 0ms; system: 0ms
【问题讨论】:
-
符号
p_aaron->age是有充分理由发明的;使用它而不是(*p_aaron).age。 -
客户端和服务器之间似乎没有任何同步。我不认为共享内存是这样工作的。
-
您应该让客户端和服务器代码就结构的大小达成一致。这应该在两个程序都使用的标题中定义。其他任何事情都是灾难的根源。
标签: c linux struct shared-memory