I'm creating a client-server program that uses sockets to connect, once a client has connected to the server a new process is created for the client (required for project). I have a linked list in the server that stores client structs holding info about them.
The issue that this has created is that when 2 clients connect for example, each client can only view there version of the linked list which holds just there details. So i'm trying to create a shared memory segment that holds the linked list, but i'm new to shared memory and am struggling to get it to work.
node_t *client_list = NULL;
int main() {
int shmid;
key_t key;
key = 1234;
shmid = shmget(key, SHMSZ, IPC_CREAT | 0666);
client_list = (node_t *) shmat(shmid, (void *)0, 0);
while(1) {
pid_t current_process;
if ((current_process = fork()) == 0) {
node_add(client_list, &client);
node_print(client_list);
pthread_create(&thread[client_count], NULL, network(client.ID), (void *) &client);
}
return 0;
}
(I have left socket connection code etc off to make it more readable). I am probably doing something very wrong in this current version but my understanding was I am creating a shared memory segment for 'node_t *client_list' which is a linked list, and then after a new process is created from connection a client node added to it. Any help would be greatly appreciated.