Ç - fork()和内存共享 [英] C - fork() and sharing memory

查看:194
本文介绍了Ç - fork()和内存共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的父母和孩子的过程既能够读写(的类型为int)相同的变量,因此它是全球性的两个进程之间。

I need my parent and child process to both be able to read and write the same variable (of type int) so it is "global" between the two processes.

我假定这将使用某种跨进程通信,并有一个变量在一个过程中被更新。

I'm assuming this would use some sort of cross-process communication and have one variable on one process being updated.

我做了一个快速谷歌和工控机和各种技术来了,但我不知道这是最适合我的情况。

I did a quick google and IPC and various techniques come up but I don't know which is the most suitable for my situation.

那么,什么方法是最好的,你可以为它提供了一个链接到一个菜鸟教程。

So what technique is best and could you provide a link to a noobs tutorial for it.

感谢。

推荐答案

由于使用fork()的提,我认为你是生活在* nix系统内

Since you are mentioning using fork(), I assume that you are living on a *nix-System

Unix.com

共享之间传输数据的主要方法
  使用UNIX IPC的流程是:

The primary way to share data between processes using UNIX IPCs are:

(1)共享内存;

(2)插座:

有其他UNIX的IPC,包括

There are other UNIX IPCs including

(3)消息队列。

(4)信号灯;

(5)信号。

您最好的选择(是IPC)是使用
  共享内存段,根据您的
  帖子。您可能需要使用信号灯
  以确保该共享存储​​器
  操作是原子的。

Your best bet (for IPCs) is to use shared memory segments, based on your post. You might need to use semaphores to insure that the shared memory operations are atomic.

这是派生一个教程和共享内存是开发棚:

A tutorial on forking and shared memory is on dev shed:

<一个href=\"http://forums.devshed.com/c-programming-42/posix-semaphore-example-using-fork-and-shared-memory-330419.html\" rel=\"nofollow\">http://forums.devshed.com/c-programming-42/posix-semaphore-example-using-fork-and-shared-memory-330419.html

另一个更深入的使用多线程(如果appilcable您的应用程序)的说明可以在这里找到:

another more in-depth description of using multithreading (if appilcable for your application) can be found here:

https://computing.llnl.gov/tutorials/pthreads/

这篇关于Ç - fork()和内存共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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