C语言 如何在共享内存中分配结构体内部的链表

yftpprvb  于 2022-12-26  发布在  其他
关注(0)|答案(2)|浏览(216)

我在C中的一个结构体中有一个链表,或者我认为是这样的。

//Structure of the domain list
typedef struct domains *domain_list;
    struct domains{
    char *domain;
    domain_list next;
}domains_node;

//Structure of the configuration of the server
typedef struct{
    int n_threads;
    domain_list domain_list;
    char* local_domain;
    char* named_pipe_statistics;
}server_config;

我尝试将它们输入共享内存中,我确信结构体是正确的,但我不知道链表是否正确(使用了全局变量):

//Initialize shared memory
if((config_shmid = shmget(IPC_PRIVATE, sizeof(server_config), IPC_CREAT|0777)) < 0){
    perror("Error in config shmid\n");
    exit(1);
}
if((config = (server_config*)shmat(config_shmid, NULL, 0)) == (server_config *)-1){
    perror("Error in config shmat\n");
    exit(1);
}

if((config_domain_shmid = shmget(IPC_PRIVATE, sizeof(struct domains), IPC_CREAT|0777)) < 0){
    perror("Error in domain_list config shmid\n");
    exit(1);
}
if((config->domain_list = (domain_list)shmat(config_domain_shmid, NULL, 0)) == (domain_list)-1){
    perror("Error in domain_list config shmat\n");
    exit(1);
}

这是为了进程通信。我需要一个动态的(不是固定的)链表,在一个结构体中,在共享内存中。所以,我需要的是一种方法来分配内存空间给我创建的新节点,以及如何链接它们。我现在它不是用malloc的,但关于这个问题的答案只是“充分分配”,我不知道它是什么。

pkwftd7m

pkwftd7m1#

您没有这样说,但我猜您使用共享内存是为了让多个进程可以同时或顺序访问同一个链表。
在这种情况下,您可以创建一个共享内存段,其中包含一个节点池和一些控制数据。
整个信息必须包含在该段中,其他进程才能看到它,因此,您的domain成员应该是一个char缓冲区,而不是指向内存中其他地方的字符串的指针。
所有非空的节点指针值都将是池中的地址,但是共享内存段可能会Map到不同进程的不同地址,因此节点不能有绝对的next指针,但是它们可以在共享节点池中保留一个相对索引,这同样适用于列表的headsm。
在链接列表代码中,应该用自定义函数替换mallocfree,这些函数可以从池中获取节点或将其放回池中,因为池的大小是固定的,所以自定义malloc可以返回NULL,您应该检查它。
下面的代码实现了一个简单的链表,它有一个固定大小的池,包含在一个共享内存段中。它将所有可见的数据保持为相对大小,但是对节点指针进行操作,可以使用dnode获得节点指针,用于局部迭代。因为0是一个有效的池索引,所以有一个特殊的值DNULL,它通过size_t来描述空指针。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>


typedef struct DNode DNode;
typedef struct DList DList;

#define MAX_DNODE 32            // Max. domain string length
#define MAX_DLEN 64             // Max. number of list nodes

#define DNULL (MAX_DLEN + 1)    // NULL value

struct DNode {
    char domain[64];
    size_t next;
};

struct DList {
    DNode pool[MAX_DNODE];      // fixed-size space for nodes
    size_t npool;               // used space in pool
    size_t pfree;               // pointer to re-use freed nodes
    size_t head;                // global list head
};

DList *dlist;

DNode *dnode_alloc(void)
{
    if (dlist->pfree != DNULL) {
        DNode *node = dlist->pool + dlist->pfree;

        dlist->pfree = dlist->pool[dlist->pfree].next;
        return node;
    } else {
        if (dlist->npool < MAX_DNODE) return &dlist->pool[dlist->npool++];
    }

    return NULL;
}

void dnode_free(DNode *node)
{
    if (node) {
        node->next = dlist->pfree;
        dlist->pfree = node - dlist->pool;
    }
}

DNode *dnode(size_t index)
{
    return (index == DNULL) ? NULL : dlist->pool + index;
}

DNode *dnode_next(const DNode *node)
{
    return dnode(node->next);
}

DNode *dnode_push(size_t *head, const char *str)
{
    DNode *node = dnode_alloc();

    if (node) {
        strncpy(node->domain, str, sizeof(node->domain));
        node->next = *head;
        *head = node - dlist->pool;
    }

    return node;
}

void dnode_pop(size_t *head)
{
    if (*head != DNULL) {
        size_t next = dlist->pool[*head].next;

        dnode_free(&dlist->pool[*head]);
        *head = next;
    }
}


int main(int argc, char* argv[])
{
    int shmid;

    shmid = shmget(IPC_PRIVATE, sizeof(DList), IPC_CREAT | 0660);
    if (shmid < 0) exit(1);

    dlist = shmat(shmid, NULL, 0);
    if (dlist == (void *) (-1)) exit(1);

    dlist->head = DNULL;
    dlist->pfree = DNULL;
    dlist->npool = 0;

    dnode_push(&dlist->head, "Alpha");
    dnode_push(&dlist->head, "Bravo");
    dnode_push(&dlist->head, "Charlie");
    dnode_push(&dlist->head, "Delta");
    dnode_push(&dlist->head, "Echo");

    while (dlist->head != DNULL) {
        puts(dnode(dlist->head)->domain);
        dnode_pop(&dlist->head);
    }

    shmdt(dlist);

    return 0;
}
mctunoxg

mctunoxg2#

这是非常有帮助的,感谢张贴。一些调整和小提琴:
1.当dlist-〉pfree!= DNULL时,dnode_alloc需要递增npool(增加了行list-〉npool++;刚好在return语句之前)。
1.类似地,dnode_free需要在返回之前递减npool(添加了行list-〉npool--;就在返回之前)。
1.在dnode_alloc中,(dlist-〉npool〈MAX_DNODE)部分应为(dlist-〉npool〈MAX_DLEN)

相关问题