为什么我要使用共享内存和单独的进程而不是std ::线程?

发布于 2025-01-26 03:29:15 字数 968 浏览 3 评论 0原文

当讲师提到的shmget作为反学通信的一个示例时,我正在观看有关操作系统的最新讲座系列。这个问题是关于“共享内存”和shmget的亲戚。

什么是共享记忆

? “ rel =“ nofollow noreferrer”>此网页:

共享内存是附加到某些的额外记忆 地址空间供所有者使用。结果,所有这些 流程[,父母和孩子]共享相同的内存段 访问它。

与线程相比

线程已经具有共享的内存空间。以下是一个示例:

#include <iostream>
#include <thread>

struct SomeData {
    int a;
    int b;
    int c;
};

void PrintC(SomeData* data_ptr) {
    std::cout << (*data_ptr).c << "\n";
}


int main() {

    SomeData data{ 4, -2, 18 };

    std::thread c_printer(PrintC, &data);

    std::cout << data.a << "\n";
    c_printer.join();

    return 0;
}

问题

如果程序员需要共享内存,为什么他们会使用单独的进程而不是单独的线程?

是共享内存(如shmget之类的函数创建)只是实现线程的一种方法(例如,std :: thread)?

是否有C ++标准库函数我应该期望在引擎盖下使用共享内存,或者仅适用于fork新进程时共享内存?

I was watching a recent lecture series on Operating Systems when the lecturer mentioned shmget as an example of interprocess communication. This question is about "shared memory" and relatives to shmget.

What is shared memory?

According to this webpage:

A shared memory is an extra piece of memory that is attached to some
address spaces for their owners to use. As a result, all of these
processes[, parent and child,] share the same memory segment and have
access to it.

Compared to threads

Threads already have shared memory space. Here is an example:

#include <iostream>
#include <thread>

struct SomeData {
    int a;
    int b;
    int c;
};

void PrintC(SomeData* data_ptr) {
    std::cout << (*data_ptr).c << "\n";
}


int main() {

    SomeData data{ 4, -2, 18 };

    std::thread c_printer(PrintC, &data);

    std::cout << data.a << "\n";
    c_printer.join();

    return 0;
}

The Questions

If the programmers needs shared memory, why would they use separate processes instead of separate threads?

Is shared memory (as created by functions like shmget) just a way to implement threads (e.g., std::thread)?

Are there C++ standard library functions I should expect to use shared memory under the hood, or is shared memory exclusively for when you fork new processes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

背叛残局 2025-02-02 03:29:15

为什么我要使用共享内存和单独的进程而不是std :: thread?

使用子处理代替线程的优点是可以封装单独过程的内存。这适合隐私问题(考虑一种您正在处理包含私人信息的Web请求的情况),并且也出于正确的关注,因为一个过程的记忆不能由另一个过程任意修改。当然,如果您选择使用它,则无法完全实现此好处。请注意,共享内存不是相互处理通信的唯一形式。

线程的优点是:

  • 可以通过标准API使用。
  • 他们的创造可能会降低成本。成本差异从轻微(Linux)到重要(窗口)不等。虽然,意义是相对的。在运行恒定的过程数量的长期运行过程中,这并不重要。

是共享内存(如shmget这样的函数创建)只是实现线程的一种方法(例如,std :: thread)?

在某些系统上,这可能是实现细节。但是从抽象的角度来看,这些是不同的概念。 C ++语言中没有“共享内存”或“过程”的概念。

Why would I use shared memory and separate processes instead of std::thread?

An advantage of using subprocesses instead of threads is that the memory of separate processes can be encapsulated. This is good for privacy concerns (consider a case where you are handling a web requests that contains private information), and also for correctness concerns since the memory of one process cannot be arbitrarily modified by another. Of course, this benefit is not fully realised for the part of shared memory if you choose to use it. Note that shared memory is not the only form of inter process communication.

Advantages of threads are:

  • They can be used through a standard API.
  • Their creation has potentially less cost. The difference in cost varies from slight (Linux) to significant (Windows). Although, significance is relative. It won't matter for a long running process that runs constant number of processes.

Is shared memory (as created by functions like shmget) just a way to implement threads (e.g., std::thread)?

On some systems, this may be true as an implementation detail. But from abstract perspective, these are distinct concepts. There is no concept of "shared memory" or "process" in the C++ language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文