[SOLVED] how to allocate dynamic memory to shared memory
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hello
I want to allocate memory to a shared memory but when i do it with malloc I get segmentation fault when i want to use it in another process.
my code is :
Code:
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define DEFAULT_PROTOCOL 0
typedef struct struarr
{
int a;
int b;
}struarr;
typedef struct stru1
{
int index;
struarr *arrstruct;
}stru1;
int main()
{
stru1 *structptr;
int i=0;
int j=0;
if (fork()==0)
{
int b=0;
key_t key;
int shmid;
key=5454;
shmid=shmget(key,1024,0666|IPC_CREAT);
if (shmid<0)
{
perror("err");
}
structptr=(stru1*)shmat(shmid,(void*)0,0);
if (structptr == (stru1*)(-1))
perror("shmat1");
structptr->index=4;
structptr->arrstruct=(struarr*)malloc(structptr->index*sizeof(struarr));
printf("passed\n");
while (i<structptr->index)
{
structptr->arrstruct[i].a=b;
b++;
structptr->arrstruct[i].b=b;
b++;
i++;
}
j=0;
while (j<structptr->index)
{
printf("a:%d\tb:%d\n",structptr->arrstruct[j].a,structptr->arrstruct[j].b);
j++;
}
printf("passed 2\n");
i=0;
while (i<5)
{
i++;
sleep(1);
scanf("%d",&b);
j=0;
while (j<structptr->index)
{
sleep(1);
structptr->arrstruct[j].a=b;
b++;
structptr->arrstruct[j].b=b;
b++;
j++;
}
}
shmdt(structptr);
shmctl(shmid,IPC_RMID,0);
}
sleep(10);
key_t key2;
int shmid2;
key2=5454;
shmid2=shmget(key2,1024,0666|IPC_CREAT);
if (shmid2<0)
{
perror("err");
}
structptr=(stru1*)shmat(shmid2,(void*)0,0);
if (structptr == (stru1*)(-1))
perror("shmat1");
i=0;
while(i<30)
{
i++;
sleep(1);
j=0;
while (j<structptr->index)
{
printf("a:%d\tb:%d\n",structptr->arrstruct[j].a,structptr->arrstruct[j].b);
j++;
}
}
shmdt(structptr);
shmctl(shmid2,IPC_RMID,0);
return 0;
}
malloc() returns a memory block in your process' heap, which is private to your process. Nothing changes just because you place a pointer to it in shared memory - the memory you're accessing through that pointer is still private to you, and will either be mapped to a different page in another process, or (more likely) will not be mapped to any valid address, causing a segment violation when you try to access it.
Basically, if you're using shared memory, you have to make sure all the data you want to expose to other processes is in the shared memory segment. Leave the data at a specified offset into the memory segment, which can be fixed at compile-time or placed in a field at some known location in the shared memory segment.
Last edited by JohnGraham; 12-15-2011 at 05:36 PM.
Leave the data at a specified offset into the memory segment, which can be fixed at compile-time or placed in a field at some known location in the shared memory segment
does this - there is an implicit assumption in your parent that there is a struct of type stru1 at offset 0 into the shared memory segment. In some field of this you store the offset to the other data structure, which you can then use to access it:
Code:
// Note: You can use void* on gcc, since gcc says sizeof(void) is 1, but use char* is more portable.
char *mem = shmat(shmid2, (void*)0, 0);
// Base type is at offset 0.
stru1 *structptr = (stru1*)mem;
// Now we have a structptr, use an offset to get some other_type.
other_type *other = (other_type*)(mem + structptr->offset_of_other_type);
sorry but would you please write the exact thing that I have to do in my source because I did not get what should I do in my program:
I think my code is completely obvious why you are using example ?
Quote:
// Note: You can use void* on gcc, since gcc says sizeof(void) is 1, but use char* is more portable.
char *mem = shmat(shmid2, (void*)0, 0);
// Base type is at offset 0.
stru1 *structptr = (stru1*)mem;
// Now we have a structptr, use an offset to get some other_type.
other_type *other = (other_type*)(mem + structptr->offset_of_other_type);
is this after malloc or before malloc or instead of it?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.