共享内存IPC机制和API/系统调用调用之间的区别
我正在研究操作系统(Silberscatz、Galvin 等人)。我的编程经验仅限于偶尔对编程文本或算法文本中给出的练习问题进行编码。换句话说,我没有适当的应用程序编程或系统编程经验。我认为我的以下问题是由于缺乏上述经验而导致缺乏背景的结果。
我是专门研究IPC机制的。在阅读有关共享内存(SM)的内容时,我无法想象进程使用 SM 进行通信的现实生活场景。上传了对我的 linux(ubuntu) 机器上附加到同一 SM 段的进程的检查(在小型 shell 脚本中使用“ipcs”)这里
应用程序的大部分共享似乎都是与 X 守护程序进行的。据我所知,X 是负责为我提供 GUI 的进程。我推断这些应用程序(主要是位于我的任务栏上的小程序)与 X 共享有关其外观和显示值需要更改的数据。这是合理的推论吗??
如果是这样, 我的问题是,我的应用程序通过共享内存段与“X”通信与我的应用程序调用“X”提供的某些 API 并与“X”通信有关刷新其外观的需要之间有什么区别? 我的意思是,为什么不使用后面的方法?
这不是用户进程和内核通信的方式吗?当应用程序想要读取文件、通过系统调用的参数传达文件名和其他相关信息时,它会调用系统调用吗?
另外,您能否为我提供一些常规使用的应用程序的示例,这些应用程序利用共享内存和消息传递进行通信?
编辑 我已经把问题说得更清楚了。我已将编辑部分的格式设置为粗体
I am studying about operating systems(Silberscatz, Galvin et al). My programming experiences are limited to occasional coding of exercise problems given in a programing text or an algorithm text. In other words I do not have a proper application programming or system programming experience. I think my below question is a result of a lack of experience of the above and hence a lack of context.
I am specifically studying IPC mechanisms. While reading about shared memory(SM) I couldn't imagine a real life scenario where processes communicate using SM. An inspection of processes attached to the same SM segment on my linux(ubuntu) machine(using 'ipcs' in a small shell script) is uploaded here
Most of the sharing by applications seem to be with the X deamon. From what I know , X is the process responsible for giving me my GUI. I infered that these applications(mostly applets which stay on my taskbar) share data with X about what needs to change in their appearances and displayed values. Is this a reasonable inference??
If so,
my question is, what is the difference between my applications communicating with 'X' via shared memory segments versus my applications invoking certain API's provided by 'X' and communicate to 'X' about the need to refresh their appearances?? BY difference I mean, why isn't the later approach used?
Isn't that how user processes and the kernel communicate? Application invokes a system call when it wants to, say read a file, communicating the name of the file and other related info via arguments of the system call?
Also could you provide me with examples of routinely used applications which make use of shared memory and message-passing for communication?
EDIT
I have made the question more clearer. I have formatted the edited part to be bold
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,由于X服务器只是另一个用户空间进程,它不能使用操作系统的系统调用机制。即使通信是通过 API 完成的,如果是在用户空间进程之间,该 API 背后也会存在某种进程间通信 (IPC) 机制。其中可能是共享内存、套接字或其他。
当涉及大量数据时,通常会使用共享内存。也许有很多数据需要多个进程访问,每个进程都有自己的副本会浪费内存。或者很多数据需要在进程之间进行通信,如果通过另一种 IPC 机制进行流式传输,一次一个字节,速度会更慢。
对于图形,程序保留包含图像、窗口甚至整个屏幕的像素图的缓冲区并不少见,然后需要定期将其复制到屏幕。有时速度非常高...每秒 30 次或更多。我怀疑这就是 X 在可能的情况下使用共享内存的原因。
First, since the X server is just another user space process, it cannot use the operating system's system call mechanism. Even when the communication is done through an API, if it is between user space processes, there will be some inter-process-communication (IPC) mechanism behind that API. Which might be shared memory, sockets, or others.
Typically shared memory is used when a lot of data is involved. Maybe there is a lot of data that multiple processes need to access, and it would be a waste of memory for each process to have its own copy. Or a lot of data needs to be communicated between processes, which would be slower if it were to be streamed, a byte at a time, through another IPC mechanism.
For graphics, it is not uncommon for a program to keep a buffer containing a pixel map of an image, a window, or even the whole screen that then needs to be regularly copied to the screen. Sometimes at a very high rate...30 times a second or more. I suspect this is why X uses shared memory when possible.
不同之处在于,使用 API,作为开发人员,您可能无法访问这些函数内部发生的情况,因此不一定会共享内存。
共享内存主要是两个应用程序都可以写入和读取的特定内存区域。当然,这要求对该内存的访问是同步的,这样就不会被损坏。
使用某人的 API 并不意味着您与他们共享内存,该进程只会执行您所要求的操作,并且可能会将该操作的结果返回给您,但这不一定通过共享内存。尽管可以,但一如既往,这取决于情况。
我想说,对其中一种的偏好取决于特定应用程序的规范以及它正在做什么以及需要共享什么。我可以想象某种类型的大数据集将由共享内存共享,但将文件名传递给另一个应用程序可能只需要 API 调用。然而,很大程度上取决于我想说的要求。
The difference is that with an API you as a developer might not have access to what is happening inside these functions, so memory would not necessarily be shared.
Shared Memory is mostly a specific region of memory to which both apps can write and read from. This off course requires that access to that memory is synchronized so things don't get corrupted.
Using somebody's API does not mean you are sharing memory with them, that process will just do what you asked and perhaps return the result of that operation to you, however that doesn't necessarily go via shared memory. Although it could, it depends, as always.
The preference for one over another I'd say depends on the specifications of the particular application and what it is doing and what it needs to share. I can imagine that a big dataset of some kind or another would be shared by shared memory, but passing a file name to another app might only need an API call. However largely dependent on requirements I'd say.