gpt4 book ai didi

c - 将结构放入共享内存

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:13:25 24 4
gpt4 key购买 nike

我创建了两个程序 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;
}

客户端.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

最佳答案

您没有显示任何删除共享内存段的代码。

如果您查看 shmget() 的 POSIX 规范,您将看到您正在报告的 EINVAL 错误可以在以下情况下给出:

[EINVAL]
A shared memory segment is to be created and the value of size is less than the system-imposed minimum or greater than the system-imposed maximum.
[EINVAL]
No shared memory segment is to be created and a shared memory segment exists for key but the size of the segment associated with it is less than size.

我认为您可能遇到了第二种情况;您正在尝试创建一个比已经存在的共享内存段更大的共享内存段。

  • 修改您的代码以自行清理(shmdt()shmctl())。
  • 使用ipcrm删除现有的共享内存段。

此外,正如我在 comment 中指出的那样,您应该确保客户端和服务器程序就共享内存中的结构大小达成一致。任何其他事情都是灾难的根源。您应该将结构定义放入 header 中,并且您的两个程序都应该使用该 header ,并且当您更改 header 的定义时都应该重新编译。

关于c - 将结构放入共享内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33858427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com