Upload
muhamad-hesham
View
275
Download
2
Embed Size (px)
DESCRIPTION
Interprocess Communication mechanisms in Windows
Citation preview
Interprocess Communication
(IPC)For Windows
Muhamad Hesham9-JAN-2012
Agenda
• Choosing IPC method criteria
• Windows provided IPC mechanisms
• Pipes
• Mailslots
Choosing IPC Method Criteria
• Communication Scope
Local or over Networks ?
Choosing IPC Method Criteria
• Communication Scope
• Communicating parties compatibility
16-bit Windows, UNIX, etc…
Choosing IPC Method Criteria
• Communication Scope
• Communicating parties compatibility
• Locating the other party
User driven or implicitly ?
Choosing IPC Method Criteria
• Communication Scope
• Communicating parties compatibility
• Locating the other party
• Performance requirements
All IPC methods have performance overhead! But …
Choosing IPC Method Criteria
• Communication Scope
• Communicating parties compatibility
• Locating the other party
• Performance requirements
• Application type
GUI, console, windows service, etc…
Windows Provided IPC Mechanisms
• Clipboard
• Data Copy
• File Mapping
• RPC
• Windows Sockets
• Pipes
• Mailslots
• And others
OMG! Did MS test all these?!
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
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;
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);
…
}
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: …
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
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
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
….
….
Windows Provided IPC Mechanisms
• Clipboard
• Data Copy
• File Mapping
• RPC• Windows Sockets
• Pipes
• Mailslots
RPC Cycle
Developed as an extension to OSF RPC
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).
Anonymous Pipes - Properties
• Unidirectional (2 handles: read-only and write-only)
2 instances to the same pipe with different access rights
Anonymous Pipes - Properties
• Unidirectional (2 handles: read-only and write-only)
• Byte-stream oriented
Useful for character-based data transfer
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
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
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
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
Anonymous Pipes
I/O redirection using anonymous pipes
Named Pipes - Big Picture
Clients and Servers using Named Pipes
Named Pipes - Properties
• Bidirectional (1 handle for read and write)
Read and Write using the same handle simultaneously
Named Pipes - Properties
• Bidirectional (1 handle for read and write)
• Message and Byte-stream oriented
A very big advantage over anonymous pipes
Named Pipes - Properties
• Bidirectional (1 handle for read and write)
• Message and Byte-stream oriented
• Networked and local
Pipe operations are transparent to user
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
Named Pipes - CreateNamedPipe
• \\[servername]\pipe\[pipename]
• User provided buffer sizes is advisory
• Buffer sizes considerations and downsides
• Client connects using CreateFile or CalledNamedPipe
Named Pipes - Server/Client Sync
Server/Client race conditions can happen
Server says:
Client says:
Named Pipes - Connection Sequence
Multithreaded pipe server use multi pipe instances
Single-Threaded Pipe Server says:
Named Pipes - Connection Sequence
Clients can use Mailslots to locate server pipes
Client says:
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)
Mailslots - Properties
• Unidirectional
One way communication line, i.e Broadcasting
Mailslots - Properties
• Bidirectional
• Multiple readers/writers capable
Typically, one writer many readers and vice versa
Mailslots - Properties
• Bidirectional
• Multiple readers/writers capable
• No reception confirmation
Writers do not hear from the readers any response
Mailslots - Properties
• Bidirectional
• Multiple readers/writers capable
• No reception confirmation
• Can be located over network domain
As well as local on the same machine
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
Mailslots - CreateMailSlot
• When creating a mailslot: \\.\mailslot\[mailslotname]
• When accessing a mailslot: \\[.|*|Domain|Computer]\mailslot\[mailslotname]
Mailslots - Example
References
• MSDN article: Interprocess Communication
• RPC overview blog post
• Windows via C/C++ book 5th edition
• Windows System Programming book 4th edition
Thanks…
Questions?Muhamad HeshamQFS [email protected]