From: Vandana on
Hello All,

I would like to know how to allocate memory for a struct within a
shared memory. In my example code shown below, I am sharing a struct
between 2 process (shared_mem) . In shared_mem I have another struct
emp

Can someone please tell me how to allocate memory for the emp struct
within the shared mem? Please note that struct emp has a pointer to
struct dob, so I want to allocate memory such that emp, and dob are
also allocated sufficinet memory.


struct dob {
int month;
int date;
int year;
}

struct emp {
int id;
struct {
struct dob* dob;
} addr;
}

struct shared_mem {
int group;
struct emp *emp;
pthread_spinlock_t mylock;

.....
};


int main() {
struct shared_mem *shm;

if ((shmid = shmget(2041, sizeof(struct emp), IPC_CREAT | 0666) < 0) {
printf("Error in shmget\n");
exit(1);
}

shm = (struct shared_mem*) shmat (shmid, NULL, 0);


Thanks,
Vandana
From: David Given on
On 20/03/10 20:07, Vandana wrote:
[...]
> Can someone please tell me how to allocate memory for the emp struct
> within the shared mem? Please note that struct emp has a pointer to
> struct dob, so I want to allocate memory such that emp, and dob are
> also allocated sufficinet memory.
[...]
> shm = (struct shared_mem*) shmat (shmid, NULL, 0);

I don't think you can do this if you want to be portable --- shmat()
won't necessarily return you the same pointer in each process, so your
pointers won't be valid. You can tell shmat() to map the memory at a
specific address but then of course you have to know details of your
platform's memory layout.

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
│ --- Conway's Game Of Life, in one line of APL
From: Vandana on
On Mar 20, 2:42 pm, David Given <d...(a)cowlark.com> wrote:
> On 20/03/10 20:07, Vandana wrote:
> [...]
>
> > Can someone please tell me how to allocate memory for the emp struct
> > within the shared mem? Please note that struct emp has a pointer to
> > struct dob, so I want to allocate memory such that emp, and dob are
> > also allocated sufficinet memory.
> [...]
> > shm = (struct shared_mem*) shmat (shmid, NULL, 0);
>
> I don't think you can do this if you want to be portable --- shmat()
> won't necessarily return you the same pointer in each process, so your
> pointers won't be valid. You can tell shmat() to map the memory at a
> specific address but then of course you have to know details of your
> platform's memory layout.
>
> --
> ┌─── dg@cowlark.com ─────http://www.cowlark.com─────
> │
> │ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
> │ --- Conway's Game Of Life, in one line of APL

Thanks for your reply.
How do I make shmat to map the memory at a specific address? I am
using centos (fedora) platform for programming.
Thanks for your time.

From: David Given on
On 20/03/10 22:31, Vandana wrote:
[...]
> How do I make shmat to map the memory at a specific address? I am
> using centos (fedora) platform for programming.

Pass in a pointer as the second parameter; see the man page.

But, of course, figuring out what pointer to use is a whole other parcel
of complicated.

Shared memory segments are inherited across fork(). You could have the
parent process attach the shared memory segment, fork(), and then all
the children will have the same shared memory segment at the same
address --- but that will only work if you can adjust your program to
fit that structure, which may not be possible.

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
│ --- Conway's Game Of Life, in one line of APL
From: Vandana on
Hello,

Continuing with my previous example, can I do the following?

struct dob {
int month;
int date;
int year;

}

struct emp {
int id;
struct {
struct dob* dob;
} addr;

}

struct shared_mem {
int group;
struct emp *emp;
pthread_spinlock_t mylock;

.....

};

int main() {
struct shared_mem *shm;

if ((shmid = shmget(2041, sizeof(struct shared_mem)+sizeof(struct emp)
+sizeof(struct dob), IPC_CREAT | 0666) < 0) {
printf("Error in shmget\n");
exit(1);

}

shm = (struct shared_mem*) shmat (shmid, NULL, 0);

if ((shmid = shmget(2041, sizeof(struct emp)+sizeof(struct dob),
IPC_CREAT | 0666) < 0) {
printf("Error in shmget\n");
exit(1);

}

shm->emp = (struct emp*) shmat (shmid, NULL, 0);

if ((shmid = shmget(2041, sizeof(struct dob), IPC_CREAT | 0666) < 0) {
printf("Error in shmget\n");
exit(1);

}

shm->emp->dob = (struct dob*) shmat (shmid, NULL, 0);


By doing the above, am I allocating memory for pointer to emp &
pointer to dob within the shared memory?


Thanks for your help.
Vandana
On Mar 21, 4:31 am, David Given <d...(a)cowlark.com> wrote:
> On 20/03/10 22:31, Vandana wrote:
> [...]
>
> > How do I make shmat to map the memory at a specific address? I am
> > using centos (fedora) platform for programming.
>
> Pass in a pointer as the second parameter; see the man page.
>
> But, of course, figuring out what pointer to use is a whole other parcel
> of complicated.
>
> Shared memory segments are inherited across fork(). You could have the
> parent process attach the shared memory segment, fork(), and then all
> the children will have the same shared memory segment at the same
> address --- but that will only work if you can adjust your program to
> fit that structure, which may not be possible.
>
> --
> ┌─── dg@cowlark.com ─────http://www.cowlark.com─────
> │
> │ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
> │ --- Conway's Game Of Life, in one line of APL