70 - Process, Thread, Interprocess Communication
70 - Process, Thread, Interprocess Communication
Interprocess Communication
Ramaswamy Krishnan Chittur
Process
Provides the resources needed to execute a program.
Has a virtual address space, executable code, open handles to system objects, a
security context, a unique process identifier, environment variables, a priority
class, minimum and maximum working set sizes, and at least one thread of
execution.
Is started with a single thread, often called the primary thread, but can create
additional threads from any of its threads.
Thread
Challenges:
Complex: More complicated to develop, manage and debug.
Conflict: Improper synchronization can lead to conflicting access to shared resources.
Race: In poorly written code, race conditions can occur as the order of completion is not deterministic.
Starvation: High priority threads can starve lower priority threads.
Priority inversion : a low priority thread holds a resource needed by a higher priority thread, blocking
it from running.
Deadlock: Two or more threads each own resources needed by the other, preventing either one from
running.
GUI and Worker threads
User Interface (UI) threads create windows, and process messages sent to those windows.
Worker threads receive no direct input from the user. Worker threads must not access a
window’s member functions. Instead, they need to transfer the call to the UI thread to
access/manipulate the window.
-
Thread synchronization
Multiple threads in the same program can access shared data. If access is not controlled to be
sequential, then shared data may become corrupted. The process of making access serial is
called serialization or synchronization.
There are various techniques for synchronization. The programmer may choose one or the
other of these techniques based on the use case scenario.
Interlocked Operations
Critical Sections
Mutex
Semaphore
Monitor/Lock
Events
More…
Reference: Overview of synchronization primitives | Microsoft Learn
Interprocess communication (IPC)
Techniques for facilitating communications and data sharing between
applications.
Can be between applications on the local device, or between applications
running on different devices on the network.
Typically, applications can use IPC categorized as clients or servers. A client is a
process that requests a service from some other process. A server is a process
that responds to a client request. Many applications act as both a client and a
server, depending on the situation.
One way to categorize Inter-process communication is about how the objects
interact cross process: as such we can categorize the techniques into:
Inter-Object Communication.
Serialized Message Passing.
Reference: Interprocess Communications - Win32 apps | Microsoft Learn
Inter-Object Communication
Versus
Serialized Message Passing
• Inter-Object Communication:
• Allows clients to communicate transparently with objects, regardless of where those objects are
running—in the same process, on the same computer, or on a different computer. This provides a single
programming model for all types of objects, and for both object clients and object servers. See here.
• The underlying technique here is Marshaling: It is the mechanism by which an object that is accessible
to one process can be made accessible to another process by the underlying IPC technique (unless the
user wants to override it). See here.
• A few technologies that provide inter-object communication are: .NET Remoting, COM, Remote
Procedure Call.
Benefits:
Isolation: code can be run in a different process. This is particularly helpful when you are executing 3 rd party
or untrusted code. If they were to run in-process, they could potentially compromise the security or reliability
of your process.
Concerns:
Security, Elevation of privilege: A higher privileged process passing data into a lower privileged process can
compromise the security of the data.
Security, Snooping: Data passed across processes may be listened to by unauthorized processes.
Security, Man-in-the-middle: An attacker can alter the messages while it is in transit.
Performance: IPC is typically more expensive and slower than in-process / in-memory operations.
Example
Multithreading demo: chittur/multithreading-demo: Demonstrates
multithreading and synchronization among a few other concepts.
(github.com)
Synchronization for multithreading demo: chittur/multi-threading-
synchronization: A sample project that demonstrates the need for
synchronization in multi-threading. (github.com)
GUI and worker threads demo: chittur/distributed-and-gui-demo:
Demonstrates a distributed computing software that also involves GUI
(github.com)
Interprocess Communication demo: chittur/inter-process-communication:
Sample project that demonstrates inter-object communication as well as
serialized message passing. (github.com)