• C Shared Memory and 2D Arrays

    BrittneTabor Member

    C Shared Memory and 2D Arrays
    So here is my code for the client:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define SHMSZ 27
    
    int main(int argc, char* argv[])
    {
    
       /* Application Step-through
          1. Create shared memory
          2. Init array
       */
       
       char   c;
       int      shmid;
       key_t   key;
       int   *shm, *s;
       int i, row, col;
    
       int      data_store[5][2];   
       /* Created Shared Memory */
       
       key = 0xFFFFFFFF;   /* Identifier Key for shared memory segment */
       
       if((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)   /* shmget has failed */
       {
          perror("shmget");
          _Exit(1);
       }
       printf("[+] Shared Mem Segment Created. shmid: %d\n", shmid);
       
       /* Attatch segment to data space */
       if((shm = shmat(shmid,NULL,0)) == (char *)-1)
       {
          perror("shmat");
          _Exit(1);
       }
       printf("[+] Segment Attatched\n");
    
       int pid;
       
       pid = fork();
       if(pid < 0)
       {
          perror("fork");
          _Exit(1);
       }
       else if(pid == 0) /* Child Process */
       {
          while(1)
          {
             sleep(1);
          }
       }
       else         /* Parent Process */
       {
          printf("[+] Process ID: %d\n", pid);
                                           
          /* data_store ID, PID, PRI */            /*               0=PID 1=PRI
                                           data_store[0][0] */
          /* Write PID and PRI to data_store array */
          data_store[0][0] = pid;
          data_store[0][1] = 1;
          
          data_store[1][0] = 12345;
          data_store[1][1] = 2;
          
          /* DEBUG: Check Array is full */
          for(i=0; i <= 1; i++)
          {
             printf("%d\n", data_store[i][0]);
          }
          
          /*Write data_store to memory seg*/
          s = shm;
    
          for(row = 0; row <= 1; row++)
             for(col=0; col <=1; col++)
                s[row*2 + col] = 1;
                
          *s = (int)NULL;
          
       
       }
    
       return 0;
    }
    

    I hope this *should* write my array to the shared memory location.

    How do I go about reading it from the Server? This is my current code;

    #include 
    #include 
    #include 
    #include 
    
    #define SHMSZ 27
    
    /* This bianry will simulate de-spooling */
    
    
    int main(int argc, char* argv[])
    {
       char c;
       int shmid;
       key_t key;
       int *shm, *s;
    
       /* Key for shared memory segment */
       key = 0xFFFFFFFF;
    
    
       if((shmid = shmget(key, SHMSZ, 0666)) < 0)
       {
          perror("shmget");
          _Exit(1);
       }
       printf("[+] Segment Found - shmid = %d\n",shmid);
       
       if((shm = shmat(shmid,NULL,0)) == (char *)-1)
       {
          perror("shmat");
          _Exit(1);
       }
       printf("[+] Segment Attatched\n");
       
       for(s=shm; *s != (int)NULL; s++)
          printf("%d",*s[0][0]);
          
       return 0;
    }
    
  • Abhey Member

    I am assuming that your question is how to read from the shared memory concurrently, without worrying about the data being corrupt by a writing process. First of all, you need some sort of locking mechanism. In the case of multiple processes trying to access a piece of data simultaneously, I do so through a monitor.

    http://en.wikipedia.org/wiki/Monitor_(synchronization)

    The monitor will basically ensure that you can have many simultaneous readers, but writers have exclusive access to a specific datum. I hope this answers the question.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish