How to use shared memory with Linux in C

前端 未结 5 1430
悲&欢浪女
悲&欢浪女 2020-11-22 08:13

I have a bit of an issue with one of my projects.

I have been trying to find a well documented example of using shared memory with fork() but to no suc

5条回答
  •  旧时难觅i
    2020-11-22 09:14

    try this code sample, I tested it, source: http://www.makelinux.net/alp/035

    #include  
    #include  
    #include  
    
    int main () 
    {
      int segment_id; 
      char* shared_memory; 
      struct shmid_ds shmbuffer; 
      int segment_size; 
      const int shared_segment_size = 0x6400; 
    
      /* Allocate a shared memory segment.  */ 
      segment_id = shmget (IPC_PRIVATE, shared_segment_size, 
                     IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); 
      /* Attach the shared memory segment.  */ 
      shared_memory = (char*) shmat (segment_id, 0, 0); 
      printf ("shared memory attached at address %p\n", shared_memory); 
      /* Determine the segment's size. */ 
      shmctl (segment_id, IPC_STAT, &shmbuffer); 
      segment_size  =               shmbuffer.shm_segsz; 
      printf ("segment size: %d\n", segment_size); 
      /* Write a string to the shared memory segment.  */ 
      sprintf (shared_memory, "Hello, world."); 
      /* Detach the shared memory segment.  */ 
      shmdt (shared_memory); 
    
      /* Reattach the shared memory segment, at a different address.  */ 
      shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0); 
      printf ("shared memory reattached at address %p\n", shared_memory); 
      /* Print out the string from shared memory.  */ 
      printf ("%s\n", shared_memory); 
      /* Detach the shared memory segment.  */ 
      shmdt (shared_memory); 
    
      /* Deallocate the shared memory segment.  */ 
      shmctl (segment_id, IPC_RMID, 0); 
    
      return 0; 
    } 
    

提交回复
热议问题