Shared Memory

This is a discussion on Shared Memory within the C Programming forums, part of the General Programming Boards category; ddHi, I'm looking at shared memory in C and I seem to have encountered a bit of a problem. I'm ...

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

    Shared Memory

    ddHi, I'm looking at shared memory in C and I seem to have encountered a bit of a problem. I'm fairly sure that I may be misunderstanding something here. I'm trying to send an array of structs to another process via shared memory. When the other process goes to read it, it just reads nothing.

    I'm using ubuntu.

    Process that creates it:

    Code:
    //create shared memory segment
    key_t key;
    key = 12;
    int shmid = shmget(key, 4096, IPC_CREAT | 0666);
    
    //Attach the shared memory segement
    
    
    //Create aux file
    
    FILE * pFile;
    pFile = fopen("part4.aux", "w+");
    fprintf(pFile, "%s", "Here are the first 10 strings that were transmitted.\n");
    
    
    
    //Write 10,000 random strings to the shared memory segment
    //Also write to file to prove what has been transmitted
    
    Data sharedmem[10000];
    
    int i = 0;
    
    for(i = 0; i < 10000; i++){
        Data d;
        d.c1 = alpha[rand()%(sizeof(alpha)-1)];
        d.c2 = alpha[rand()%(sizeof(alpha)-1)];
        d.c3 = alpha[rand()%(sizeof(alpha)-1)];
        d.c4 = alpha[rand()%(sizeof(alpha)-1)];
        
    
        if(i < 10){
        fprintf(pFile, "\n");
        fprintf(pFile, "%c", d.c1);
        fprintf(pFile, "%c", d.c2);
        fprintf(pFile, "%c", d.c3);
        fprintf(pFile, "%c", d.c4);
        }
    
        sharedmem[i] = d;
        }
    
    Data* pointer = (Data*)shmat(shmid,0,0);
    fprintf(pFile, "\n%s\n", "Production complete.");
    
    pointer = sharedmem;
    
    fclose(pFile);
    shmdt(pointer);
    Process attempting to read it.
    Code:
    //Find shared memory segment
    key_t key;
    key = 12;
    int shmid = shmget(key, 4096, 0666);
    
    //Attach the shared memory segement
    Data* shared_memory = (Data*) shmat(shmid, 0, 0);
    
    //Open aux file
    
    FILE * pFile;
    pFile = fopen("part4.aux", "a+");
    fprintf(pFile, "%s", "Here are the strings that were received.\n");
    
    //Read strings from shared memory
    
    printf("%s\n", "Writing to file");
    
    int i;
    
    for(i = 0; i < 10; i++){
       fprintf(pFile, "%c", shared_memory[i].c1);
       fprintf(pFile, "%c", shared_memory[i].c2);
       fprintf(pFile, "%c", shared_memory[i].c3);
       fprintf(pFile, "%c", shared_memory[i].c4);
       fprintf(pFile, "\n");
     //fputc(shared_memory[i].c1, pFile);
     //printf("%c", shared_memory[i].c1);
     
    }
    
    
    //fprintf(pFile, "%s", shared_memory);
    
    printf("%s\n", "Closing file");
    fclose(pFile);
    shmdt(shared_memory);
    If anybody could shed any light on where I'm going wrong, that would be much appreciated.

    Other notes:
    - When reading the memory seg and printing to file, it doesn't like being printed as "%s" because it thinks the data is an int.
    - It doesn't mind "%c" however, but it makes the file unreadable. However, when ran in cent os, the file is readable but displays (null) (null) (null) (null)

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Code:
    Data* pointer = (Data*)shmat(shmid,0,0);
    fprintf(pFile, "\n%s\n", "Production complete.");
     
    pointer = sharedmem;
     
    fclose(pFile);
    shmdt(pointer);
    No offence, but you make a few fairly rookie mistakes (and the sort that will not end well) - are you new to programming in general, or C? If so, you might want to get a good book and go through it before attacking IPC, which can be awkward! Also, you're not checking any of your return values. You should always check all your return values where they can indicate failure, and do something appropriate for any failure.

    Also, you are using shmget() to get a memory segment 4096 bytes long, and you create 10000 data structures! That's a total of (10000 * sizeof(Data)) bytes your memory segment needs to be if you want to place that many Data structures into it.

    Another problem is the code I quoted above - you change some pointers, but you don't actually move any values into/out of the shared memory segment. If you're determined to press on, have a look at memcpy() - you need to do something like:

    Code:
    // Get pointer to shared memory segment.
    Data *pointer = shmat(shmid, 0, 0);
    
    // Check return value.
    
    // Copy some bytes from wherever 'sharedmem' points to to wherever 'pointer' points to.
    memcpy(pointer, sharedmem, /* No. bytes to copy - you have too much at the moment! */);
    Your data will then be available in the shared memory segment.
    Last edited by JohnGraham; 12-07-2011 at 10:04 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    it looks like you are not actually copying anything to the shared memory. "pointer = sharedmem;" doesn't copy anything. it just changes the value in 'pointer'. which also will result in an error in the shmdt call and the shared memory is not release. should you be doing 'memcpy(pointer,sharemem,size)'?
    note: if 'Data' was a structure, then '*pointer = *sharedmem' would copy the data. but you have an array of 'Data' so that won't work in this case.

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. ICP shared memory
    By cavemandave in forum C Programming
    Replies: 1
    Last Post: 11-20-2007, 05:08 AM
  3. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 01:29 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21