程序之间如何通信? [英] How do programs communicate with each other?

查看:343
本文介绍了程序之间如何通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过程如何相互通信?到目前为止,我已经学会了编程的全部知识,无法解释套接字,文件系统以及其他在程序之间发送消息的工作方式.

How do procceses communicate with each other? Using everything I've learnt to fo with programming so far, I'm unable to explain how sockets, file systems and other things to do with sending messages between programs work.

如果您要添加任何特定于OS的操作系统,我将使用基于Linux的操作系统.提前致谢.这个问题困扰了我好久了.我还猜测内核与它有关.

Btw I use a Linux based OS if your going to add anything OS specific. Thanks in advance. The question's been bugging me for ages. I'm also guessing the kernel has something to do with it.

推荐答案

对于大多数IPC(进程间通信)机制,您的问题的一般答案是:进程A调用内核将指针传递给包含数据的缓冲区要转移到进程B,进程B调用内核(或在调用内核时已被阻塞),并传递一个指向缓冲区的指针,该缓冲区将被进程A的数据填充.

In case of most IPC (InterProcess Communication) mechanisms, the general answer to your question is this: process A calls the kernel passing a pointer to a buffer with data to be transferred to process B, process B calls the kernel (or is already blocked on a call to the kernel) passing a pointer to a buffer to be filled with data from process A.

对于套接字

This general description is true for sockets, pipes, System V message queues, ordinary files etc. As you can see the cost of communication is high since it involves at least one context switch.

信号构成了一种异步IPC机制,其中一个进程可以向该机制发送简单的通知另一个进程触发由第二个进程注册的处理程序(或者不执行任何操作,如果没有注册任何处理程序,则停止或终止该进程,具体取决于信号).

Signals constitute an asynchronous IPC mechanism in which one process can send a simple notification to another process triggering a handler registered by the second process (alternatively doing nothing, stopping or killing that process if no handler is registered, depending on the signal).

要传输大量数据,可以使用系统V共享内存两个进程可以访问主内存的相同部分.请注意,即使在这种情况下,也需要采用一种同步机制,例如系统V信号量 ,这也会导致上下文切换.

For transferring large amount of data one can use System V shared memory in which case two processes can access the same portion of main memory. Note that even in this case, one needs to employ a synchronization mechanism, like System V semaphores, which result in context switches as well.

这就是为什么当进程需要经常通信时,最好让它们在单个进程中成为线程.

This is why when processes need to communicate often, it is better to make them threads in a single process.

这篇关于程序之间如何通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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