LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to allocate dynamic memory to shared memory (https://www.linuxquestions.org/questions/programming-9/how-to-allocate-dynamic-memory-to-shared-memory-918953/)

golden_boy615 12-15-2011 04:23 PM

how to allocate dynamic memory to shared memory
 
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;

}

what is the problem?
how can I solve it?

JohnGraham 12-15-2011 05:34 PM

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.

golden_boy615 12-16-2011 03:58 AM

thanks but how should I:
Quote:

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
how?

JohnGraham 12-16-2011 04:58 AM

Quote:

Originally Posted by golden_boy615 (Post 4551415)
thanks but how should I:

how?

You've already been doing this. Your code:

Code:

structptr=(stru1*)shmat(shmid2,(void*)0,0);
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);


golden_boy615 12-16-2011 05:36 AM

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?

golden_boy615 12-16-2011 12:27 PM

Thanks I find out, this is my code:
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;
    key_t key;
    key_t key2;
    int shmid;


    key=5454;
    key2=5555;

    shmid=shmget(key,sizeof(stru1),0666|IPC_CREAT);
    if (shmid<0)
    {
        perror("err");

    }
    structptr=(stru1*)shmat(shmid,(void*)0,0);
    if (structptr == (stru1*)(-1))
        perror("shmat1");

    if (fork()==0)
    {
        int b=0;

        structptr->index=4;
        shmid=shmget(key2,(structptr->index*sizeof(struarr)),0666|IPC_CREAT);
        if (shmid<0)
        {
            perror("err2");

        }
        structptr->arrstruct=(struarr*)shmat(shmid,(void*)0,0);
        if (structptr->arrstruct == (struarr*)(-1))
            perror("shmat11");


        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++;
        }

        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(2);
        shmid=shmget(key2,(structptr->index*sizeof(struarr)),0666|IPC_CREAT);
        if (shmid<0)
        {
            perror("err2");

        }
        structptr->arrstruct=(struarr*)shmat(shmid,(void*)0,0);
        if (structptr->arrstruct == (struarr*)(-1))
            perror("shmat11");

    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(shmid,IPC_RMID,0);
    return 0;

}


radaw 04-25-2018 12:57 PM

I, it's a really old post but it was really useful for me.

I have another question.
I'm trying to share a struct like this:
Code:

struct my_structure
{
    char type;
    char *name;
};

I'm trying to allocate the shared memory using:
Code:

shmid = shmget(key + 2, [NUM OF CHAR], 0666 | IPC_CREAT);
my_structure.name= shmat(shmid,NULL, 0);


and use strcpy to copy a string on the shared memory:
Code:

strcpy(my_structure.name, my_string);

But when I run a child with execve I can print my_structure.type but the name seems empty.
How can I copy a string to a shared memory?

Thanks everyone on this thread, It was very useful.

Quote:

Originally Posted by golden_boy615 (Post 4551835)
Thanks I find out, this is my code:
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;
    key_t key;
    key_t key2;
    int shmid;


    key=5454;
    key2=5555;

    shmid=shmget(key,sizeof(stru1),0666|IPC_CREAT);
    if (shmid<0)
    {
        perror("err");

    }
    structptr=(stru1*)shmat(shmid,(void*)0,0);
    if (structptr == (stru1*)(-1))
        perror("shmat1");

    if (fork()==0)
    {
        int b=0;

        structptr->index=4;
        shmid=shmget(key2,(structptr->index*sizeof(struarr)),0666|IPC_CREAT);
        if (shmid<0)
        {
            perror("err2");

        }
        structptr->arrstruct=(struarr*)shmat(shmid,(void*)0,0);
        if (structptr->arrstruct == (struarr*)(-1))
            perror("shmat11");


        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++;
        }

        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(2);
        shmid=shmget(key2,(structptr->index*sizeof(struarr)),0666|IPC_CREAT);
        if (shmid<0)
        {
            perror("err2");

        }
        structptr->arrstruct=(struarr*)shmat(shmid,(void*)0,0);
        if (structptr->arrstruct == (struarr*)(-1))
            perror("shmat11");

    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(shmid,IPC_RMID,0);
    return 0;

}



NevemTeve 04-25-2018 02:34 PM

Use gdb to find the bug in your code.


All times are GMT -5. The time now is 02:09 AM.