How to use shared memory with Linux in C

前端 未结 5 1492
悲&欢浪女
悲&欢浪女 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条回答
  •  时光取名叫无心
    2020-11-22 09:11

    There are two approaches: shmget and mmap. I'll talk about mmap, since it's more modern and flexible, but you can take a look at man shmget (or this tutorial) if you'd rather use the old-style tools.

    The mmap() function can be used to allocate memory buffers with highly customizable parameters to control access and permissions, and to back them with file-system storage if necessary.

    The following function creates an in-memory buffer that a process can share with its children:

    #include 
    #include 
    #include 
    
    void* create_shared_memory(size_t size) {
      // Our memory buffer will be readable and writable:
      int protection = PROT_READ | PROT_WRITE;
    
      // The buffer will be shared (meaning other processes can access it), but
      // anonymous (meaning third-party processes cannot obtain an address for it),
      // so only this process and its children will be able to use it:
      int visibility = MAP_SHARED | MAP_ANONYMOUS;
    
      // The remaining parameters to `mmap()` are not important for this use case,
      // but the manpage for `mmap` explains their purpose.
      return mmap(NULL, size, protection, visibility, -1, 0);
    }
    

    The following is an example program that uses the function defined above to allocate a buffer. The parent process will write a message, fork, and then wait for its child to modify the buffer. Both processes can read and write the shared memory.

    #include 
    #include 
    
    int main() {
      char parent_message[] = "hello";  // parent process will write this message
      char child_message[] = "goodbye"; // child process will then write this one
    
      void* shmem = create_shared_memory(128);
    
      memcpy(shmem, parent_message, sizeof(parent_message));
    
      int pid = fork();
    
      if (pid == 0) {
        printf("Child read: %s\n", shmem);
        memcpy(shmem, child_message, sizeof(child_message));
        printf("Child wrote: %s\n", shmem);
    
      } else {
        printf("Parent read: %s\n", shmem);
        sleep(1);
        printf("After 1s, parent read: %s\n", shmem);
      }
    }
    

提交回复
热议问题