The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 06-28-2009
BeNdErR BeNdErR is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Problems with shared memory and lists

Hi, I need to put in shared memory a list made with object of this structure:

Code:
typedef struct Obj{
char objname[20];
struct Obj *nextObj;
}Object
I've filled my list with (for example) 10 elements, but when i try to put it in shared memory to be read by another process i get segmentation fault after first read record..
I've read in another thread (here -> dynamic shared memory )that malloc gives sone problems with shared memory
Quote:
If you called malloc to create linked_list_header->ptr, then the address returned by malloc is private to the calling (the process that calls malloc) process. Same with NODE->buffer. You will have to allocate these things in shared "by hand" without calling malloc.
but i don't understand what i have to do now and how to solve my problem.

i tried to share object in this way:
Code:
Obj *head;     //pointer to the head of my FILLED list
int shmid;
Obj *sh;
...
shmid = shmget(SHMKEY,(sizeof(Obj)*10),0666|IPC_CREAT|IPC_EXCL);
...
sh = (Obj *)shmat(shmid, 0, 0666);
...
*sh = *head;
probably I miss something or maybe I'm totally wrong... how can I allocate Obj in shared "by hand" without calling malloc?

Thanks for your support, best regards.