Hello all!
First time poster here. I have a problem and I'm at my wit's end on how to solve it. Basically I'm trying to put together a doubly linked list whose head is in a shared memory segment. I update its contents when the list is first initialized, and when a second node is introduced(the ->next field), but then never again, everything else is done by malloc.
My problem is, although I can run through the list on my first thread(the one that initialized the list) referencing the shared memory segment, I cannot do it on my second thread. There is some kind of pointer/address issue that's stopping my second thread from finding further nodes down the list. All I'm able to find are the contents of the first node, which are put directly into the shared memory segment. This is what doesn't make sense to me, the int values I insert there I can access, but the pointer, which was pointing to a malloc'd node, points nowhere on the second thread.
This is the code for my node insertion function:
"head" is the shared memory segment, and is of type node_linked_list:Code:void armazenar_ordenar_requisicoes (struct msgbuf_driver req,struct chs chs_passado) { node_linked_list *new,*run,*aux; if(head->exists == 0) { printf("head is null\n"); head->next = 0; head->previous = 0; head->requisicao = req; head->chs_rec = chs_passado; end = head; head->exists = 1; } else { run = head; if ( run != 0 ) { while ( run->next != 0) { run = run->next; } } /* Creates a node at the end of the list */ run->next = malloc( sizeof(node_linked_list) ); aux = run; run = run->next; if ( run == 0 ) { printf( "Out of memory" ); } run->next = 0; run->previous = aux; run->requisicao = req; run->chs_rec = chs_passado; end = run; } orderlist(); }
I run through the list on both threads(which were created thru fork() and then started running each one procedure).Code:struct node { struct node *next; struct node *previous; struct msgbuf_driver requisicao; struct chs chs_rec; int exists; }; typedef struct node node_linked_list;
This is the input I get from one thread:
data on list:13 15 24 33 45
And from the second:
data on list:13 0
Any help would be greatly appreciated.