43
Interprocess Communication (IPC) For Windows Muhamad Hesham 9-JAN-2012

NTP Software Jan 2012 Monthly Meeting IPC Presentation

Embed Size (px)

DESCRIPTION

Interprocess Communication mechanisms in Windows

Citation preview

Page 1: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Interprocess Communication

(IPC)For Windows

Muhamad Hesham9-JAN-2012

Page 2: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Agenda

• Choosing IPC method criteria

• Windows provided IPC mechanisms

• Pipes

• Mailslots

Page 3: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Choosing IPC Method Criteria

• Communication Scope

Local or over Networks ?

Page 4: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Choosing IPC Method Criteria

• Communication Scope

• Communicating parties compatibility

16-bit Windows, UNIX, etc…

Page 5: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Choosing IPC Method Criteria

• Communication Scope

• Communicating parties compatibility

• Locating the other party

User driven or implicitly ?

Page 6: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Choosing IPC Method Criteria

• Communication Scope

• Communicating parties compatibility

• Locating the other party

• Performance requirements

All IPC methods have performance overhead! But …

Page 7: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Choosing IPC Method Criteria

• Communication Scope

• Communicating parties compatibility

• Locating the other party

• Performance requirements

• Application type

GUI, console, windows service, etc…

Page 8: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy

• File Mapping

• RPC

• Windows Sockets

• Pipes

• Mailslots

• And others

OMG! Did MS test all these?!

Page 9: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard• Data Copy

• File Mapping

• RPC

• Windows Sockets

• Pipes

• Mailslots

• All applications have access to the clipboard

• User-driven method used in: Cut, Copy and Paste operations

Page 10: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy• File Mapping

• RPC

• Windows Sockets

• Pipes

• Mailslots

Use WM_COPYDATA message to pass data to another application.

The following structure is passed to the received application as read-only data:

typedef struct tagCOPYDATASTRUCT { ULONG_PTR dwData;

DWORD cbData;

PVOID lpData;

} COPYDATASTRUCT, *PCOPYDATASTRUCT;

Page 11: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy• File Mapping

• RPC

• Windows Sockets

• Pipes

• Mailslots

Sender A:

COPYDATASTRUCT myCopyData;

// Initialize myCopyData

SendMessage( hwndOther, WM_COPYDATA, (WPARAM)(HWND) hwndSelf, (LPARAM) (LPVOID) &myCopyData);

Receiver B:

case WM_COPYDATA:

pMyCopyData = (PCOPYDATASTRUCT)lParam;

switch(pMyCopyData->dwData)

{

case ID_FOO: Foo(pMyCopyData->lpData);

}

Page 12: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy

• File Mapping• RPC

• Windows Sockets

• Pipes

• Mailslots

Four possible methods of implementing a program to reverse the order of all the bytes in a file:

Method 1: One File, One Buffer

Method 2: Two Files, One Buffer

Method 3: One File, Two Buffers

Method 4: …

Page 13: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy

• File Mapping• RPC

• Windows Sockets

• Pipes

• Mailslots

Implementing a program to reverse the order of all the bytes in a file:

Method 1: One File, One Buffer

Method 2: Two Files, One Buffer

Method 3: One File, Two Buffers

Method 4: One File, Zero Buffers

Page 14: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy

• File Mapping• RPC

• Windows Sockets

• Pipes

• Mailslots

Implementing a program to reverse the order of all the bytes in a file:

Method 1: One File, One Buffer

Method 2: Two Files, One Buffer

Method 3: One File, Two Buffers

Method 4: One File, Zero Buffers

Simply call _tcsrev to reverse the data in the file because it is usable simply as an in-memory text string!!

Power of Memory-Mapped Files

Page 15: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy

• File Mapping• RPC

• Windows Sockets

• Pipes

• Mailslots

C:\Data.file

Map View

LPTSTR szBuffer;

szBuffer = // Create Map View_tcsrev(szBuffer);// Close Map View

Windows Memory Manager

szBuffer

….

….

Page 16: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy

• File Mapping

• RPC• Windows Sockets

• Pipes

• Mailslots

RPC Cycle

Developed as an extension to OSF RPC

Page 17: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Windows Provided IPC Mechanisms

• Clipboard

• Data Copy

• File Mapping

• RPC

• Windows Sockets• Pipes

• Mailslots

• Protocol-independent interface

• Based on the sockets first popularized by Berkeley Software Distribution (BSD) (UNIX).

Page 18: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Anonymous Pipes - Properties

• Unidirectional (2 handles: read-only and write-only)

2 instances to the same pipe with different access rights

Page 19: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Anonymous Pipes - Properties

• Unidirectional (2 handles: read-only and write-only)

• Byte-stream oriented

Useful for character-based data transfer

Page 20: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Anonymous Pipes - Properties

• Unidirectional (2 handles: read-only and write-only)

• Byte-stream oriented

• Narrow scope (related processes)

Since it is unnamed, only its handle can be shared

Page 21: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Anonymous Pipes - Properties

• Unidirectional (2 handles: read-only and write-only)

• Byte-stream oriented

• Narrow scope (related processes)

• Implemented with Named Pipes

Can be treated as a named pipe

Page 22: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Anonymous Pipes - Properties

• Unidirectional (2 handles: read-only and write-only)

• Byte-stream oriented

• Narrow scope (related processes)

• Implemented with Named Pipes

• Asynchronous I/O NOT supported

ReadFile/WriteFile OVERLAPPED structure is ignored

Page 23: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Anonymous Pipes - CreatePipe

• Use ReadFile/WriteFile to communicate over the pipe

• Read operation block if buffer empty

• Write operation block if buffer full

• Write operation does not return until the buffer is Read

Page 24: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Anonymous Pipes

I/O redirection using anonymous pipes

Page 25: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Big Picture

Clients and Servers using Named Pipes

Page 26: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Properties

• Bidirectional (1 handle for read and write)

Read and Write using the same handle simultaneously

Page 27: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Properties

• Bidirectional (1 handle for read and write)

• Message and Byte-stream oriented

A very big advantage over anonymous pipes

Page 28: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Properties

• Bidirectional (1 handle for read and write)

• Message and Byte-stream oriented

• Networked and local

Pipe operations are transparent to user

Page 29: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Properties

• Bidirectional (1 handle for read and write)

• Message and Byte-stream oriented

• Networked and local

• Asynchronous I/O supported

Can perform asynchronous pipe read, write and connect

Page 30: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - CreateNamedPipe

• \\[servername]\pipe\[pipename]

• User provided buffer sizes is advisory

• Buffer sizes considerations and downsides

• Client connects using CreateFile or CalledNamedPipe

Page 31: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Server/Client Sync

Server/Client race conditions can happen

Server says:

Client says:

Page 32: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Connection Sequence

Multithreaded pipe server use multi pipe instances

Single-Threaded Pipe Server says:

Page 33: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Named Pipes - Connection Sequence

Clients can use Mailslots to locate server pipes

Client says:

Page 34: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots- Big Picture

Multiple writers single reader model

Send@ \\*\mailslot\mhesham

Maislot Client C (Writer)

Maislot Server (Reader)

Maislot Client A (Writer)

Maislot Client B (Writer)

Page 35: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots - Properties

• Unidirectional

One way communication line, i.e Broadcasting

Page 36: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots - Properties

• Bidirectional

• Multiple readers/writers capable

Typically, one writer many readers and vice versa

Page 37: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots - Properties

• Bidirectional

• Multiple readers/writers capable

• No reception confirmation

Writers do not hear from the readers any response

Page 38: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots - Properties

• Bidirectional

• Multiple readers/writers capable

• No reception confirmation

• Can be located over network domain

As well as local on the same machine

Page 39: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots - Properties

• Bidirectional

• Multiple readers/writers capable

• No reception confirmation

• Can be located over network domain

• Limited message length

MS say 424 byte! Unicode string of size 212 character

Page 40: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots - CreateMailSlot

• When creating a mailslot: \\.\mailslot\[mailslotname]

• When accessing a mailslot: \\[.|*|Domain|Computer]\mailslot\[mailslotname]

Page 41: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Mailslots - Example

Page 42: NTP Software Jan 2012 Monthly Meeting IPC Presentation

References

• MSDN article: Interprocess Communication

• RPC overview blog post

• Windows via C/C++ book 5th edition

• Windows System Programming book 4th edition

Page 43: NTP Software Jan 2012 Monthly Meeting IPC Presentation

Thanks…

Questions?Muhamad HeshamQFS [email protected]