Thread: Shared memory and malloc!

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    Shared memory and malloc!

    Hi,

    i have a structure named A. I use shmget and shmat to create shared memory first, then assign my object to it. After that i do some forks. So they heritage this id and pointer, which shmget and shmat gave me back. Here's a deal:

    there is an array of char pointers in the structure A, which has been created in shared memory. Every process made with that fork can (or could would be a better word...) put some strings into that shared char pointers array. But to do that i must use malloc. And the question is is that malloc allocating memory in the shared memory area or what? I cannot see strings which has been put to that array by another process but comparing the adressess of every char pointer in array - they're the same! What's more, they interact some way with each other so when i put some data using one, the other gets the same data that he has put but a little disorted. So what's going on? Two different data written in the same adress? I'm running both from the same machine! How is that possible?


    Thanks in advance,
    Grzegorz

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's my understanding that shared memory is off in its own segment, and you must have all shared memory only reference other shared memory. So if you need to make a new string that everyone sees, you make it in a shared memory segment, not with malloc.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't just put a pointer into shared memory. You need to put ALL of your shared data into the shared memory space.
    malloc does not allocate memory that can be accessed by anything but the program that called it. Every process gets a whole separate virtual address space. A pointer value of say 0x12345678 in one process has no relation to a pointer value of 0x12345678 in a different process. When the OS switches processes, the address space is switched too. Each process is isloated largely as if it were running on a different machine.
    The whole reason you need APIs for shared memory is that one program can't just access the memory of another process. When you use the proper APIs, what they do is map a portion of real memory into more than one virtual address space.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    So what i might do is making simple two-dimensional char array like char X[A][B] where A is the number of strings which i'm gonna have in it (10, so it's not a problem) and B is the length of each one? And the other way, what is much more unpleasant to do, but still gives some, let's say, flexibility is to use that two functions mentioned (IPC) instead of malloc, yes? What would you do in my boots?

    Thanks guys!

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'd probably allocate one single large enough chunk of shared memory and serialise the stuff I want shared into there. If it needs to be modified by more than one process then I'd control that with a machine-wide mutex of some kind. When wanting to access the data I'd deserialise it.
    If it's just a 2D array then perhaps I'd just access the shared memory directly without the serialisation/deserialisation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    I did it with 2-dim array. I think it's okay enough. There is one more thing. Let's suppose i forked my parent process several times, and before that fork statement there was a table of 2 int's named descr, which was used to assign a upipe by pipe(descr). The question is: when i write to this pipe from one process will it be accessible for all the other processes?

    Thank you!
    Last edited by kbzium; 12-31-2011 at 02:56 AM.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    How can i make one child-server process write message to all the other sibling processes in a real-time? That's why i thought about pipes...

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sorry, I haven't written code using pipes, and haven't programmed on anything but Windows for some time now either. Perhaps someone else can help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you need pipes if you have a block of shared memory? Use a spot in your shared memory to be a flag that your one guy sets if there is new info. Then use another spot in your shared memory to actually have the new info.
    Code:
    write to new memoryspot
    set flag as having new stuff
    Meanwhile, at all the other processes:
    Code:
    if( flag is set as having new stuff )
        do something with new data

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Quzah i thought about it at first, setting a flag is easy but what about unsetting it? Who would do that?

    I did it by "brute force": i added checksum to every last message so all the clients compare their last checksum with the one which is in shared memory area (along with string). Not so smart but it works well.

    Yet i have encountered another problem. How can i get info on who the server is connecting with? I need to know if someone is using telnet...

    Thanks! Happy new year

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared Memory
    By schrei in forum C Programming
    Replies: 2
    Last Post: 12-07-2011, 11:02 AM
  2. Shared memory IPC Help!!!
    By liudaisuda in forum Linux Programming
    Replies: 3
    Last Post: 09-21-2011, 04:14 PM
  3. ICP shared memory
    By cavemandave in forum C Programming
    Replies: 1
    Last Post: 11-20-2007, 06:08 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM
  5. using shared memory in c
    By flawildcat in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 12:25 PM