LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2011, 04:23 PM   #1
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
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?
 
Old 12-15-2011, 05:34 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
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.
 
Old 12-16-2011, 03:58 AM   #3
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Original Poster
Rep: Reputation: 18
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?
 
Old 12-16-2011, 04:58 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by golden_boy615 View Post
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);
 
1 members found this post helpful.
Old 12-16-2011, 05:36 AM   #5
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Original Poster
Rep: Reputation: 18
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?
 
Old 12-16-2011, 12:27 PM   #6
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Original Poster
Rep: Reputation: 18
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;

}
 
1 members found this post helpful.
Old 04-25-2018, 12:57 PM   #7
radaw
LQ Newbie
 
Registered: Apr 2018
Posts: 1

Rep: Reputation: Disabled
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 View Post
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;

}
 
Old 04-25-2018, 02:34 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Use gdb to find the bug in your code.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does /dev/shm reduce memory available for non-shared memory segments? mightyscotchpine Linux - Server 1 09-22-2009 06:58 PM
dynamic memory across shared libraries shiprat Programming 4 06-02-2009 08:36 PM
Dynamic shared memory user777 Linux - General 6 02-16-2009 07:36 AM
Linux shared memory segment access problem and x86 Virtual Memory layout. regmee Linux - Kernel 1 08-23-2008 12:11 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration