进程间通讯和线程间通讯的常用机制简介(转发,英文)

原创 2006年05月17日 16:09:00

Introduction

This session is designed to familiarize with available IPC methods by comparing and contrasting techniques so that you may effectively decide which ones to use. I will also illustrate strengths and weaknesses of each method and provide starting points for implementing each method.

Because of time limitations, this session will consist of only a few examples.

Types of IPC

Local

Local IPC is for communication among process on a single machine. Local IPC is useful for data sharing, signaling, and synchronization.

Mutexes, semaphores, and events can be named, or unnamed (identified by handle) and can be protected by NT permissions.

Atoms

Atoms are globally available strings or integer values identified by a handle. Atoms were typically used for DDE. The global atom table is also limited to 37 strings total. I do not recommend the use of atoms as they seem to be outdated. Instead, other more up to date IPC mechanisms should be used.

Shared Memory

Shared memory is a block o f memory which multiple processes can access. Under Win16 all memory was accessible by any process so pointers could be just passed between processes. Win32 has memory protection mechanisms, and a processes memory is virtually mapped to that process. Win32 however can map a block of memory into multiple processes virtual memory spaces. Even though the shared block of memory may appear to be at different locations to each process, each will point to the same physical location in memory. In addition, this mechanism allows you to control which processes have access to shared memory blocks. Win32 can also map a disk file into virtual memory. Because of this, when shared memory is used, it is called a memory mapped file.

Win32 also does this to the code segment of executables. When multiple instances of an executable are loaded, only one copy of the code is loaded into physical memory, while it is mapped into multiple virtual spaces.

Mutexes

A mutex (Mutual Exclusion) is an object which can be owned, but only owned by a single thread at a time. Any other thread trying to gain ownership, will be blocked and queued in order of requests. Mutexes are used to control exclusive access to any global object, or non reentrant code. Mutexes can be used to control access to shared memory, hardware, or other single access items.

Semaphores

A semaphore is like a mutex, except that a specified number of threads can own a specific semaphore at one time. When the semaphore is created, this number is specified. Semaphores are typically used to limit access to resources. An example of this might be to prevent "thrashing" of a resource by setting the maximum number of threads which can concurrently access the object.

Critical Sections

Critical sections are similar to mutexes, however mutexes differ from critical sections in that they can be accessed across processes and threads, while a critical section, can only be access across threads from within the same process. Critical sections are also lighter weight, cannot be named, or have security attributes assigned to them.

Events

An event is an object which threads can wait for to be triggered. Any free thread may then trigger the event, which will then release the other waiting threads. An event can be used to hold threads from accessing something that has not been initialized or ready yet. They can also be used to signal that input is available, or a device is ready to accept more data.

Messages

Messages can be used to send a one way queued instructions/status to a single thread. You can send messages to an existing windows message queue, and filter them as they come in, or create your own queue within a secondary thread. Messages are good for non time critical communications, or to serialize handling. Messages can contain only a limited amount of data however. Each message can contain only one 16 bit integer, and one 32 bit integer. When communicating between threads, pointers may be passed in the 32 bit parameter. However when communicating between processes pointers may not be passed, even when the pointer identifies shared memory as each process may see the shared memory at a different location in its virtual space. Object handles (Mutexes, semaphores, events) cannot be passed in the message data, however such handles can be duplicated, and the duplicated handles may be passed via the message data. (See DuplicateHandle)

DDE

DDE (Dynamic Data Exchange) was the common way to externally control applications in the 16 bit world. DDE is still used occasionally, however it has been strategically replaced by COM. You should use Com instead of DDE, unless the application you need to communicate with only understands DDE. DDE also tends to be slow, and the built in DDE in Delphi/C++ Builder is "less than perfect".

Clipboard

The clipboard can also be used for IPC, however in most situations it is not advisable as it is a global resource.

OLE/Com

OLE (Object Linking & Embedding) has evolved into COM (Component Object Model). COM allows you to do many things. With COM you can create language independent objects, expose interfaces from applications to allow external control, and much more. COM allows you to expose interfaces. An interface is a defined set of methods and properties which allow you to proxy calls to any code or object that you wish. An interface is like a contract. Any time that the interface is implemented, all of its members must be implemented.

Network

Network IPC is for communication between processes on different machines.

Network Protocols

Network protocols allow you the most flexibility, however they bind you to a single protocol, and require a lot of extra programming. Most times you do not need this flexibility and it is better to use a higher level protocol. Network protocols are also lighter weight.

Some common protocols used for network communication are IPX/SPX (Novell), NetBEUI (Microsoft), and TCP/IP (Internet).

NetBios is also a protocol, but it is a slightly higher protocol than the others. It abstracts some of the features of the other network protocols while still providing you with most of their advantages. NetBioss main feature is that is does not bin you to a particular protocol which your network configuration and/or hardware may dictate. NetBios commonly runs on top of NetBEUI or TCP/IP.

If you do decide you need to work at the network protocol level, TCP/IP is probably your best bet. It is very flexible, in widespread use (The internet runs on it), works with any operating system, has tons of existing services, and has become the de facto standard.

Mailslots

Mailslots are like messages except that they can be sent across a network. Mailslots also differ from messages in that they are not arbitrarily limited in the amount of data they can contain, and mailslots can be broadcast to a group of receivers. Mailslots however are sent as datagrams, and therefore are not guaranteed to arrive at their destination. Mailslots are similar to TCP/IPs UDP mechanism, but at a slightly higher level. If delivery must be guaranteed, another method should be used.

Pipes

A pipe is a communication channel with two endpoints. Pipes are similar to TCP connections. Pipes however can be optionally named, and contain security attributes. Pipes can also be one or two way. Pipes are used both for network communication, and locally. For example, the console is a pipe. If you want to open a DOS session / command window, you can swap a pipe for its standard in and standard out, thus giving your application full control of the window.

RPC

RPC (Remote Procedure Calls) allow you to make function/procedure calls in which the code actually executes on another system. RPCs are typically used with older systems, mainframes, or for implementing distributed object systems.

Distributed Objects

Distributed objects are used for communication among objects. These objects can be local or remote. This easily allows you to distribute your programs with complete abstraction of location. The two most common distributed object standards are DCom and Corba.

Without distributed object systems, developers need to implement their own protocols and also decide upon and program for a network transport. In many cases they also tie their protocol to a specific language, and even a platform. Distributed object systems abstract the user from all this and provide a high level interface which is platform independent, language independent, and network protocol independent.

DCom

DCom is just a distributed version of COM (Component Object Model). DCom is controlled by Microsoft and therefore is tied heavily to Win32 and other Microsoft technologies. The specification is available for other platforms, but is rarely used. For all intents and purposes, DCom is for WinTel only. DCom is also heavily tied to NT security. If you have lots of users that do not have NT accounts, this may pose a major headache for you. NT security can be bypassed by using the Midas socket server (Ive been told you do not need a Midas license to merely use this single component).

Corba

Corba (Common Object Request Broker Architecture) is a platform independent distributed object protocol. Corba is also implemented by many vendors including Inprise/Visigenic. This allows you to choose your implementation. The Corba standard is controlled by the OMG (Object Management Group). You can find tons of Corba information at their web site http://www.omg.org

原文出处:http://www.hower.org/Kudzu/Articles/AdvancedIPC/

java进程间通讯机制代码

•socket •RMI远程调用 •共享内存 •管道
  • blueangle17
  • blueangle17
  • 2014年06月12日 13:08
  • 1450

进程间通讯与线程间通讯

进程间通信类型:(1)环境变量、文件描述符 一般Unix环境下的父进程执行fork(),生成的子进程拥有了父进程当前设置的环境变量以及文件描述符;由于通信是一个单向的、一次性的通信,随后的父进程以及子...
  • jenus1
  • jenus1
  • 2008年02月26日 13:58
  • 970

进程间、线程间通信方式小结

之前一直对进程间和线程间的通信方式搞混,今天对它们总结一下。 一、进程间的通信方式 # 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用。...
  • Alexlee1986
  • Alexlee1986
  • 2014年03月14日 10:40
  • 52753

管道流实现两个线程间通信

Java中实现多线程间的通信主要通过两种方式:共享变量和管道流。共享变量一般通过加锁的方式来保证线程的安全性,在之前的多篇博客中已提供了多个例子,这里不赘述。知道在使用锁的过程中,注意锁作用的对象以及...
  • lanxiangru
  • lanxiangru
  • 2016年12月11日 22:38
  • 866

Winform 进程之间通讯的几种方法

进程之间通讯的几种方法: 在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯。常用的方法有: 使用内存映射文件 通过共享内存DLL共享内存 使用SendMessage向另一进程...
  • fwj380891124
  • fwj380891124
  • 2012年02月15日 10:56
  • 4413

线程通信和进程通信区别(线程进程区别)

前言:腾讯笔试中多道选择题考到这个问题,这里总结一下。学习Java的童鞋可能对于线程的理解要比学php好很多。本文参考于线程通信与进程通信的区别 。 进程和线程的区别:对于进程来说,子进程是父进程的...
  • Return_True_hang
  • Return_True_hang
  • 2017年04月08日 10:37
  • 835

进程间通信方式总结——管道(一)

Linux/Unix系统IPC是各种进程间通信方式的统称,但是其中极少能在所有Linux/Unix系统实现中进行移植。随着POSIX和Open Group(X/Open)标准化的推进呵护影响的扩大...
  • tf_apologize
  • tf_apologize
  • 2017年04月13日 16:28
  • 488

Android 进程间通讯四种方式

android系统中应用程序之间不能共享内存,在android SDK中提供了4种用于跨进程通讯的方式。这4种方式正好对应于android系统中4种应用程序组件:Activity、Content Pr...
  • hedong316
  • hedong316
  • 2016年01月20日 14:43
  • 9540

Java—进程间通讯的有几种方法

进程间通信的方法主要有以下几种:   (1)管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖先的进程之间进行通信。   (2)命名管道(named pipe):...
  • weiguishan
  • weiguishan
  • 2016年02月11日 17:17
  • 1142

Java 并发:线程间通信与协作

线程与线程之间不是相互独立的个体,它们彼此之间需要相互通信和协作,最典型的例子就是生产者-消费者问题。本文首先介绍 wait/notify 机制,并对实现该机制的两种方式——synchronized+...
  • justloveyou_
  • justloveyou_
  • 2017年02月08日 17:43
  • 3296
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:进程间通讯和线程间通讯的常用机制简介(转发,英文)
举报原因:
原因补充:

(最多只允许输入30个字)