如何在 C 中与 Linux 一起使用共享内存 [英] How to use shared memory with Linux in C

查看:26
本文介绍了如何在 C 中与 Linux 一起使用共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个项目有点问题.

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

我一直试图找到一个有据可查的示例,该示例将共享内存与 fork() 一起使用,但没有成功.

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

基本上的情况是,当用户启动程序时,我需要在共享内存中存储两个值:current_path,它是一个 char* 和一个 file_name 也是 char*.

Basically the scenario is that when the user starts the program, I need to store two values in shared memory: current_path which is a char* and a file_name which is also char*.

根据命令参数,使用 fork() 启动一个新进程,该进程需要读取和修改存储在共享内存中的 current_path 变量,而file_name 变量是只读的.

Depending on the command arguments, a new process is kicked off with fork() and that process needs to read and modify the current_path variable stored in shared memory while the file_name variable is read only.

是否有关于共享内存的很好的教程和示例代码(如果可能),您可以指导我使用吗?

Is there a good tutorial on shared memory with example code (if possible) that you can direct me to?

推荐答案

有两种方法:shmgetmmap.我将谈论 mmap,因为它更现代和灵活,但你可以看看 man shmget (或本教程),如果您更愿意使用旧式工具.

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.

mmap() 函数可用于分配具有高度可定制参数的内存缓冲区,以控制访问和权限,并在必要时使用文件系统存储支持它们.

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 <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

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);
}

以下是使用上面定义的函数来分配缓冲区的示例程序.父进程会写一条消息,fork,然后等待它的子进程修改缓冲区.两个进程都可以读写共享内存.

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 <string.h>
#include <unistd.h>

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
", shmem);
    memcpy(shmem, child_message, sizeof(child_message));
    printf("Child wrote: %s
", shmem);

  } else {
    printf("Parent read: %s
", shmem);
    sleep(1);
    printf("After 1s, parent read: %s
", shmem);
  }
}

这篇关于如何在 C 中与 Linux 一起使用共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆