0% found this document useful (0 votes)
17 views

70 - Process, Thread, Interprocess Communication

Uploaded by

leniha7603
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

70 - Process, Thread, Interprocess Communication

Uploaded by

leniha7603
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Threads, Processes and

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

 Entity within a process that can be scheduled for execution.


 All threads of a process share its virtual address space and system resources. In
addition, each thread maintains exception handlers, a scheduling priority, thread
local storage, a unique thread identifier, and a set of structures the system will
use to save the thread context until it is scheduled.
 A thread lives in one and only one process. A process may have one or more
threads.
 Pointers are process specific, so threads can share pointers.
Process vs Thread
 A process never executes anything per se; it is simply
a container for threads.
 Threads run in the context of a process. Each process
has at least one thread.
 A thread represents a path of execution that has its
own call stack and CPU state.
 Threads are confined to context of the process that
created them.
 A thread executes code and manipulates data within
its process’s address space.
 All threads of a process share its virtual address
space. A thread can execute any part of the program
code, including parts currently being executed by
another thread.
Thread scheduling

 Threads are the basic unit of scheduling in Windows.


 Each thread is scheduled to run for some brief time period before another
thread is given control of CPU.
 A thread may be in one of three possible states:
 Running
 Blocked or Suspended
 Ready to run
Thread states
 A running thread typically enters blocked or suspended state if it blocked on
waiting for some system resource, or its time slice expires.
 Blocked/Suspended threads become ready to run when the resource they wait
on becomes available or when their sleep interval has expired.
Thread Features
 Managed Threads: .NET provides a nice interface to schedule and access threads. It
is a nice wrapper over the threads provided by the OS. These are called managed
threads.
 Priority: Threads have priority. Higher priority threads are preferred while
scheduling.
 Foreground and Background Threads: A managed thread is either a background
thread or a foreground thread. Background thread does not keep the managed
execution environment running. Once all foreground threads have been stopped in a
managed process, the system stops all background threads and shuts down.
 Thread Pool: provides an application with a pool of worker threads that are managed
by the system, allowing you to concentrate on application tasks rather than thread
management.
Multithreading
 Benefits:
 Concurrency: code can run in parallel when needed.
 Improve UI responsiveness: UI can be responsive while underlying functional processing runs.
 Multicore friendly: Utilizes multiprocessor architecture where available.

 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.

• Serialized Message Passing:


• Requires the clients to serialize data into bytes before they are shared across processes.
• Serialization is the technique of converting the state of an object into a form that can be persisted or
transported. See here.
• A few technologies where you would use serialization for IPC are: Named pipes, Sockets, Udp/Tcp clients
IPC: Considerations

 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)

You might also like