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

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

问题描述

我有一个有点问题与我的项目之一。

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 success.

基本上情形是,当用户开始该程序,我需要两个值存储在共享存储器:的current_path这是一个char *和file_name中这也是字符*

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.

有与例如code(如果可能的话),您可以直接我?共享内存的一个很好的教程

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

谢谢,
bleepzter

Thanks, bleepzter

推荐答案

该名男子页 shmget的有你需要开始pretty太多的一切。

The man page for shmget has pretty much everything you need to get started.

如果不是,这里 你有例子说明。

If not, here you have an explanation with examples.

shmget的办法是有点过时,但。使用 MMAP accompannying等设施是一个聪明的做法。

The shmget approach is kinda outdated, though. Using mmap accompannying other facilities is a smarter approach.

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

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