我想我需要在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实现的,但对于这个问题的回答只是“适当的分配”,我不知道这是什么意思。