#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Shared memory between 2 processes


    Hi,
    I wonder how can I allocated memory so 2 processes can communicate. I tried the shmget() and shmat() functions but I am not able to make them work.
    Any help?
    Joćo Vieira
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    Where is the code you wrote that didn't work? Did you RTFM and Google to find working examples?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0
    Im new in C so I dont know if these are corretcly but I tried:

    key_t key_test;int shmflg; void* sh_mat;
    key_test = ftok ( "/home/joao/trusted_world/general_file",26);
    shmflg = (0 | IPC_CREAT | IPC_EXCL);
    shmid = shmget( key_test,20, shmflg );
    sh_mat = shmat(shmid, NULL, shmflg);

    //Now I think sh_mat had the shared memory address which I can write. But when I try to write it says something I dont know understand

    *sh_mat = 9;

    Maybe its a begginers mistake but I really appreciate some help

    Thnkz
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    Please read the sticky post for new users. By making it easier for us to help you you are making it a lot more likely to get useful help.

    In other words, post a complete working program in as few lines as possible inside "code" tags (all explained in the post for new users).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2228
    Originally Posted by joaorobvieira
    Maybe its a begginers mistake but I really appreciate some help

    Thnkz
    Two beginner's mistakes (three if you count not using code tags -- one if you identify the mistake as expecting us to be able to read your mind):
    1. Not providing a program illustrating the problem. If this came up as part of a larger project, then write a short test program that does nothing other than to work with shared memory. This will show us what you are trying to do.

    2. Not telling us what that error message was: "But when I try to write it says something I dont know understand". If you are doing this from the command line, then copy the message and paste it here. You may not be able to understand that message, but we might.

    3*. Not telling us what operating system you're trying this on. Because you're trying to work with shared memory, we assume that it's UNIX or Linux, but if you're using Windows that the fact that Windows doesn't support UNIX' shared memory might answer your question.

    In re 1&2, we have seen it happen before where the OP assumed the problem to be the feature they were working with (eg, shared memory), whereas the error message and code showed the problem to be in something completely different, such as mishandling of strings or of pointers. That is why we need to see that output you couldn't understand and we need to see the code that produced that output. A short compilable test program can be very helpful, because that allows us to attempt to compile and to run it in order to test it out for ourselves. And code tags are of course very important so that we can read the code.


    [* FOOTNOTE: Refer to Monty Python and the Spanish Inquisition.]

    Comments on this post

    • mitakeet agrees : Cudos for your patience!
    Last edited by dwise1_aol; July 23rd, 2012 at 01:36 PM.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2228
    Code:
        shmflg = (0 | IPC_CREAT | IPC_EXCL);
        shmid = shmget( key_test,20, shmflg );
    Admittedly, I haven't played with this IPC method. But my book (Linux Programming By Example by Kurt Wall (2000) -- out of print by now, I'm sure) says that shmflg also includes the permission bits called modes (standard rwx permissions for world, group, owner). And the man page for shmget says (abbreviated to save space):
    The value shmflg is composed of:

    IPC_CREAT

    . . .

    IPC_EXCL

    . . .

    mode_flags

    (least significant 9 bits) specifying the permissions granted to the owner, group, and world. These bits have the same format, and the same meaning, as the mode argument of open(2). Presently, the execute permissions are not used by the system.
    So it looks like you gave nobody, not even yourself, any permissions to use that shared memory. Sound like what that mysterious output was trying to tell you?

    Of course, that might not be your only mistake, so do please be forthcoming with the information we requested.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2228
    You've obviously lost interest in this question, but are you performing error checking? If shmget or shmat fails, they return a value that tells you that. Are you testing their return values before trying to use them?

    PS
    Many forums have more lurkers than participants -- a lurker is one who reads the messages on the forum, but does not post anything himself and most often is not even a forum member. Many times, a lurker will find a forum through Google or another search engine when he's searching for information on a topic, such as using shared memory; a number of new members have come to us that way, joining just in order to reply to the message that had brought them here.

    Many new members, upon finding the solution to their problem, will either simply go away or will post a "solved it; thanks" without any mention of how they had solved it. That does not help the lurkers who are reading the thread in order to find the solution to their own problem which may well be related. Also, experienced programmers who normally don't work with that feature of C may also be able to learn from your solution -- or from doing the research to help you solve it. This forum is not here just for you, but rather for the greater community.

    So that everybody can benefit from this thread, you should not only say whether you have solved the problem, but you should also share that solution.
    Last edited by dwise1_aol; July 24th, 2012 at 01:46 PM.

IMN logo majestic logo threadwatch logo seochat tools logo