Thread: Shared Memroy Indroduction Program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Shared Memroy Indroduction Program

    Hello All,

    I am having troubles creating a program that uses shared memory. As far as I can tell I am doing everything correctly but I keep getting a seg fault when I run the code (see code below for explanation). The basic thought is for a program to create a shared memory place and then fork a process that will use the shared space to sum up numbers passed in through the command line.

    Code:
            printf("in main");
            /** the identifier for the shared memory segment */
            int shared_mem_id;
    
            /** pointer to the address space */
            int *shared_mem;
    
            /** the size (in bytes) of the shared memory segment */
            int segment_size = sizeof(int);
            printf("declariation of stuff");
    
            /** generate key used in shmget() */
            key_t key = ftok(argv[2], 1);
            printf("got key");
    
            /** allocate  a shared memory segment */
            shared_mem_id = shmget(key, segment_size, S_IRUSR | S_IWUSR);
        printf("got mem_id");
    
            /** attach the shared memory segment */
            shared_mem =  shmat(shared_mem_id, NULL, 0);
            printf("created mem stuff");
    
            /** initalize shared_mem to 0 */
            /** this is where i get to before it seg faults */
            *shared_mem = 0;
            printf("created mem stuff and inialize");
    Any help would be greatly appriciated.

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need to add error checking and reporting.

    shmget() needs IPC_CREAT if you're not using IPC_PRIVATE.

    >> argv[2]
    Make sure "argc >= 3" in order for that to be valid.

    gg

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It would help immensely if you could provide compilable code, without that, I can only give you a visual eval, not recreate your problem and pinpoint it. Also, you should find some GDB tutorials (like the one we have here: Cprogramming.com - Tutorials - An Introduction to GDB), so you can learn to debug these things yourself. Here are 2 problems I see off the bat:

    1. You never check the return values of any of the shared memory/IPC functions: ftok, shmget, shmat, or presumably anything else that you may call in the rest of your code. You should check the return values, and if they fail, print a useful error message using the perror function.
    2. You use argv[2], but I don't see you use argv[1]. Remember, arrays start at 0 in C. If you call your program as "foo ftok_key", then argv[0] is "foo", argv[1] is "ftok_key" and argv[2] is NULL.
    3. Use fprintf(stderr, ...) for debug output, since stderr is unbuffered. stdin, which printf prints to, is line-buffered, so your debug statements may not show up even though you passed that point in the code before the crash. Debugging with print statements becomes a non-issue when you learn to use GDB.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Thank you Codeplug for the tip about IPC_CREAT. That turned out to fix my whole program. Also thanks for the tips about error checking the values I was getting back.

    anduril462:

    Thanks to you too. Next time I will give compilable code with my problem and also thanks for the pointer to the GDB tutorial.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL in Shared Memory
    By AlenDev in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2010, 04:04 AM
  2. simple shared memory program on c programming
    By kattythebest in forum C Programming
    Replies: 1
    Last Post: 09-09-2009, 02:39 AM
  3. Shared fd after a fork()
    By Andaluz in forum Linux Programming
    Replies: 3
    Last Post: 03-20-2009, 02:57 PM
  4. shared libraries
    By Raven Arkadon in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2005, 07:11 AM
  5. mfc as shared dll
    By zMan in forum Windows Programming
    Replies: 3
    Last Post: 09-06-2002, 01:23 PM