Linux C共享内存无法在结构中获取字符串数组

时间:2018-08-17 03:07:24

标签: shared-memory

我将结构中的动态数组推送到共享内存。客户端链接了此共享内存,我无法在共享内存中检索数组的内容。

Server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

#define MAX_CHAR 65

typedef struct motcptable{
    char **Address;
    char **Value;
    int Count;

    int ThreadExit;
    int ThreadId;

    char Motcp_Ip[20];
    int Motcp_Port;
    int Motcp_Id;
    int Motcp_Timeout_Sec;
    int Motcp_Sleep;
    int Motcp_Retry;
    int Motcp_Start_Address;
    int Motcp_Coil_Read_Max;
    int Motcp_Coil_Write_Max;
    int Motcp_Holding_Read_Max;
    int Motcp_Holding_Write_Max;
} MotcpTable;

struct motcptable Table[64];

void iniTable(void) {
    for(int i = 0; i < 64; i++) {
        Table[i].Address = NULL;
        Table[i].Value = NULL;
        Table[i].Count = 0;
        Table[i].ThreadExit = 0;
        Table[i].ThreadId = i;
        Table[i].Motcp_Timeout_Sec = 3;
        Table[i].Motcp_Sleep = 500;
        Table[i].Motcp_Retry = 10;
    }
}

void Add(int Index, char *Address_buf) {
    Table[Index].Address = (char **) realloc(Table[Index].Address, ((Table[Index].Count + 1) * sizeof(char *)));
    Table[Index].Value = (char **) realloc(Table[Index].Value, ((Table[Index].Count + 1) * sizeof(char *)));
    Table[Index].Address[Table[Index].Count] = NULL;
    Table[Index].Value[Table[Index].Count] = NULL;
    for(int i = 0; i <= Table[Index].Count; i++) {
        Table[Index].Address[i] = (char *) realloc(Table[Index].Address[i], MAX_CHAR * sizeof(char));
        Table[Index].Value[i] = (char *) realloc(Table[Index].Value[i], MAX_CHAR * sizeof(char));
    }

    sprintf(Table[Index].Address[Table[Index].Count], "%s", Address_buf);
    sprintf(Table[Index].Value[Table[Index].Count], "%s", Address_buf);

    Table[Index].Count++;
}

void Dispose(void) {
    for(int i = 0; i < 63; i++) {
        for(int j = 0; j < Table[i].Count; j++) {
            free(Table[i].Address[j]);
            free(Table[i].Value[j]);
        }
        if(Table[i].Count > 0) {
            free(Table[i].Address);
            free(Table[i].Value);
        }
    }
}

int main(int argc, char* argv[])
{
    int shmid;
    key_t key;
    struct motcptable *shm;

    char s;
    int i, size;
    char ch2[65];
    char* ch1;


    key = 1000;

        iniTable();

        sprintf(Table[0].Motcp_Ip,"192.168.200.199");
        Table[0].Motcp_Port = 502;
        Table[0].Motcp_Id = 1;
        Table[0].Motcp_Start_Address = 1;
        Table[0].Motcp_Timeout_Sec = 3;
        for(i = 0; i < 2; i++) {
            sprintf(ch2, "%d", i + 10000);
            Add(0, ch2);
        }

    size = size + (sizeof(char **) * Table[0].Count);
    shmid = shmget(key, size, IPC_CREAT | 0666);
    if (shmid < 0) {
        perror("shmget");
        exit(1);
    }

    shm = (struct motcptable*) shmat(shmid, NULL, 0);

    shm[0].Address = (char **) realloc(shm[0].Address, ((Table[0].Count + 1) * sizeof(char *)));
    shm[0].Value = (char **) realloc(shm[0].Value, ((Table[0].Count + 1) * sizeof(char *)));
    shm[0].Address[0] = Table[0].Address[0];
    shm[0].Address[1] = Table[0].Address[1];
    shm[0].Count = Table[0].Count;
    printf("\t Asssign Address : %s\n", shm[0].Address[0]);
    if (shm == (struct motcptable *)-1) {
        perror("shmat");
        exit(1);
    }

    while (shm[63].ThreadExit != 1) {
        sleep(1);
    }

    Dispose();

    shmctl(shmid, IPC_RMID, NULL); //destory shared memory
    return 0;
}

这是client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

typedef struct motcptable{
    char **Address;
    char **Value;
    int Count;

    int ThreadExit;
    int ThreadId;

    char Motcp_Ip[20];
    int Motcp_Port;
    int Motcp_Id;
    int Motcp_Timeout_Sec;
    int Motcp_Sleep;
    int Motcp_Retry;
    int Motcp_Start_Address;
    int Motcp_Coil_Read_Max;
    int Motcp_Coil_Write_Max;
    int Motcp_Holding_Read_Max;
    int Motcp_Holding_Write_Max;
} MotcpTable;

struct motcptable Table[64];
int main(int argc, char* argv[])
{
    int shmid;
    key_t key;
    struct motcptable* shm;

    int size;
    key = 1000;
    size = sizeof(Table) + (sizeof(char **) * 2);
    shmid = shmget(key, size, 0666);
    if (shmid < 0) {
        perror("shmget");
        exit(1);
    }

    shm = (struct motcptable*) shmat(shmid, NULL, 0);

    if (shm == (struct motcptable *)-1) {
        perror("shmat");
        exit(1);
    }

    printf("\n\t %s \n", shm[0].Address[0]);    // Segmentation fault (core dumped)

    printf("\n\t %d\n",shm[0].Count); 

    shm[63].ThreadExit = 1;

    return 0;
}

除了字符串数组“地址”和“值”之外,我还可以读写数组中结构的其他成员。我认为通过shmat获得的客户端不包括服务器端内存分配结构。因此,如何使用分配的内存获取shmat。

0 个答案:

没有答案