IPC的共享内存和线程的共享内存有什么区别? [英] What's the difference between shared memory for IPCs and threads' shared memory?

查看:223
本文介绍了IPC的共享内存和线程的共享内存有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们使用诸如shmget()之类的POSIX共享内存-协调进程间通信的通用调用.与Linux在单个进程中实现共享内存和线程之间的同步的方式不同,如何调用shmget()和协调共享内存段上的通信.其中之一重量更轻吗?

Let's use POSIX shared memory like shmget() - a common call to coordinate inter-process communication. How is calling shmget() and coordinating communication on the shared memory segment any different than how Linux implements shared memory and synchronization between threads in a single process. Is one of them more light-weight?

推荐答案

SHM适用于多个进程中的IPC.在现代操作系统中,每个进程都无法看到彼此的内存空间.使用shmget()的公用密钥获取共享内存,并使用shmat()将共享内存页映射到每个进程内部的本地内存地址.由于不同的内存使用情况和共享库加载到每个进程空间中,因此映射的共享内存地址可能会有所不同. SHM密钥,大小是预先定义的,并且在这些过程中是固定的.

SHM is for IPC in multiple processes. In modern OS, each process cannot see each others' memory space. Using common key for shmget() to get the share memory and using shmat() to map share memory page to local memory address inside each process. The mapped shared memory address might be different due to different memory usage and shared libraries loaded into each process space. And the SHM key, size are predefined and fixed among those process.

对于线程的内存,我们可能不将其称为共享内存,因为线程全部在单个进程内存空间中寻址.他们可以在相同的处理空间中进行查看和读取/写入.

For threads' memory, we might not call it shared memory because threads are all in a single process memory space addressing. They can see and read/write in the same process space.

这篇关于IPC的共享内存和线程的共享内存有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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