简体   繁体   English

将结构放入共享内存

[英]Putting a Struct into Shared Memory

I have created two programs a server.c and a client.c. 我创建了两个程序server.c和client.c。 I have a struct that holds an age. 我有一个年龄的结构。 I have got the programs working together to read the shared memory and to change the shared memory, however this only works when using one variable in the struct. 我已经让程序一起工作以读取共享内存并更改共享内存,但是,这仅在结构中使用一个变量时有效。 As soon as i have more than one variable in the struct i get a segmentation fault. 一旦我在结构中有多个变量,我就会遇到分段错误。

Server.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 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;
}

Error message from Server.c 来自Server.c的错误消息

SHMGET: Invalid argument

RUN FINISHED; exit value 1; real time: 0ms; user: 0ms; system: 0ms

You don't show any code that deletes the shared memory segment. 您没有显示任何删除共享内存段的代码。

If you look at the POSIX specification for shmget() , you will see that the EINVAL error you are reporting can be given if: 如果查看shmget()的POSIX规范,则会看到在以下情况下可以给出正在报告的EINVAL错误:

[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. 将创建一个共享内存段,并且size的值小于系统施加的最小值或大于系统施加的最大值。
[EINVAL] [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. 没有要创建的共享内存段,并且存在用于密钥的共享内存段,但是与其关联的段的大小小于大小。

I think you may be running into the second case; 我认为您可能会遇到第二种情况; you're trying to create a bigger shared memory segment than the one that already exists. 您正在尝试创建一个比现有内存段更大的共享内存段。

  • Modify your code to clean up behind itself ( shmdt() , shmctl() ). 修改您的代码以清理自身( shmdt()shmctl() )。
  • Use ipcrm to remove the existing shared memory segment. 使用ipcrm删除现有的共享内存段。

Also, as I noted in a comment , you should make sure that the client and server programs agree on the size of the structure in shared memory. 另外,正如我在评论中指出的那样,您应该确保客户端程序和服务器程序在共享内存中的结构大小上达成一致。 Anything else is a recipe for disaster. 其他任何事情都是灾难的根源。 You should put the structure definition into a header, and both your programs should use that header, and both should be recompiled when you change the definition of the header. 您应该将结构定义放入标头中,并且两个程序都应使用该标头,并且在更改标头的定义时都应重新编译两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM