Thread vs Process
scheduling
synchronization
The thread begins execution with the C/C run-time library startup code.
The startup code calls your main or WinMain and execution continues until the main function returns and the C/C library code calls ExitProcess.
Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. The classes that give access to the metadata of a running program are in System.Reflection.
System.Reflection namespace defines the following types to analyze the module's metadata of an assembly:
Assembly, Module, Enum, ParameterInfo, MemberInfo, Type, MethodInfo, ConstructorInfo, FieldInfo, EventInfo, and PropertyInfo
3. Thread vs Process
• A Process is inert. A process never
executes anything 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
address space.
• If two or more threads run in the
context of a single process they
share a common address space.
• They can execute the same code
and manipulate the same data.
• Threads sharing a common process
can share kernel object handles
because the handles belong to the
process, not individual threads.
4. Starting a Process
• Every time a process starts, the system creates a primary thread.
• The thread begins execution with the C/C run-time library startup
code.
• The startup code calls your main or WinMain and execution continues
until the main function returns and the C/C library code calls
ExitProcess.
5. Scheduling Threads
• Windows 2000, NT and Win98 are preemptive multi-tasking systems. Each
task is scheduled to run for some brief time period before another task is given
control of CPU.
• Threads are the basic unit of scheduling on current Win32 platforms. A thread
may be in one of three possible states
o running
o blocked or suspended, using virtually no CPU cycles
o ready to run, using virtually no CPU cycles
• A running task is stopped by the scheduler if
• it is blocked waiting for some system event or resource
6. Con’t
• its time slice expires and is placed back on the queue of ready to run threads
• it is suspended by putting itself to sleep for some time
• it is suspended by some other thread
• it is suspended by the operating system while the OS takes care of some other
critical activity.
• Blocked threads become ready to run when an event or resource they wait on
becomes available.
• Suspended threads become ready to run when their sleep interval has expired or
suspend count is zero.
7. Benefits of using Threads
• Keeping user interfaces responsive even if required processing takes a long
time to complete.
• handle background tasks with one or more threads
• service the user interface with a dedicated thread
• Your program may need to respond to high priority events. In this case, the
design is easier to implement if you assign that event handler to a high
priority thread.
• Take advantage of multiple processors available for a computation.
• Avoid low CPU activity when a thread is blocked waiting for response from
a slow device or human by allowing other threads to continue.
8. Potential Problems with Threads
• Conflicting access to shared memory
• one thread begins an operation on shared memory, is suspended, and leaves
that memory region incompletely transformed
• a second thread is activated and accesses the shared memory in the
corrupted state, causing errors in its operation and potentially errors in the
operation of the suspended thread when it resumes
• Race Conditions occur when correct operation depends on the order of
completion of two or more independent activities
• the order of completion is not deterministic Starvation
• a high priority thread dominates CPU resources, preventing lower priority
threads from running often enough or at all.
9. Synchronization
• A program may need multiple threads to share some data.
• If access is not controlled to be sequential, then shared data may become
corrupted.
• One thread accesses the data, begins to modify the data, and then is put to sleep
because its time slice has expired. The problem arises when the data is in an
incomplete state of modification.
• Another thread awakes and accesses the data, that is only partially modified. The
result is very likely to be corrupt data.
• The process of making access serial is called serialization or synchronization.
10. Thread Synchronization
• Synchronizing threads means that every access to data shared between threads is protected so that when any
thread starts an operation on the shared data no other thread is allowed access until the first thread is done.
• The principle means of synchronizing access to shared data are
• Interlocked increments
• only for incrementing or decrementing integers
• Critical Sections
• Good only inside one process
• Mutexes
• Named mutexes can be shared by threads in different processes.
• Events
• Useful for synchronization as well as other event notifications.
28. Types of Binding
• There are two kinds of Binding from C# to COM – Early Binding and Late Binding.
• Early Binding can be done by creating a runtime callable wrapper, which the C#
client can use for invoking COM objects. That’s what happens when you make a
reference in a C# client to a COM server.
• Late Binding can be done even without the creation of a runtime callable
wrapper. We will see how.
29. Late Binding
• Late Binding is done with the help of the C# Reflection APIs.
• The Type class and the Activator class of the C# Reflection API is used
for this purpose.
• The C# client only needs to know the server’s Program ID for runtime
invocation. The following code shows how to accomplish that.
30. Using C# Reflection for Late Binding
//Get IDispatch Interface from the COM Server. Here the Server’s Program ID is “Component.InsideDCOM”
Type objType = Type.GetTypeFromProgID(“Component.InsideDCOM”);
//Create an instance of the COM object from the type obtained
object objSum = Activator.CreateInstance(objType);
object c;
object[] myArgument = {100,200};
//Invoke a Method on the COM Server which implements IDispatch Interface and get the result
c = objType.InvokeMember("Sum", BindingFlags.InvokeMethod, null, objSum, myArgument);
//Print the result
Console.WriteLine(“Sum of 100 and 200 is “ + c);
32. Reflection
• Reflection is the ability of a managed code to read its own metadata
for the purpose of finding assemblies, modules and type information
at runtime. The classes that give access to the metadata of a running
program are in System.Reflection.
• System.Reflection namespace defines the following types to analyze
the module's metadata of an assembly:
• Assembly, Module, Enum, ParameterInfo, MemberInfo, Type,
MethodInfo, ConstructorInfo, FieldInfo, EventInfo, and PropertyInfo.
33. Assembly Metadata
• An assembly is a logical DLL or EXE, which is described by manifest,
the detailed description (metadata) of an assembly. The .NET
compiler produces a portable executable PE file for CLR with the
extensions of .exe or .dll. This PE file is mainly comprised of metadata
and IL (Intermediate Language).
34. • When a compiler builds a binary (dll or exe), the code gets complied into
the Intermediate Language (IL) and then packaged in an assembly. An
assembly contains metadata which has the information about the type of
classes and their definitions including their members, fields, constructors,
properties, methods, function etc.
• By using refection, an assembly can be inspected.
Type _type = Type.GetType("ReflectionConcept.Employee");
Or
Type _type = typeof(Employee);
35.
36. Uses of Reflections
• Refection is heavily used by developer IDEs or UI designers such as
Visual Studio. The Property window of a .NET application uses
refection to show list of properties.
• Late binding can be achieved by using refection. Meaning, reflection
gives developers a way to use code that is not available at compile
time. By using refection, the new instances of types can be created
dynamically, which don’t have information at compile time.
• In a Web application, whenever developers drags and drop a control
from Toolbox to the Form, the code is written for you. Here reflection
plays the role and used to read control properties and events. Thease
are listed in the Properties window.
37. • It allows view attribute information at runtime.
• It allows examining various types in an assembly and instantiate these
types.
• It allows late binding to methods and properties
• It allows creating new types at runtime and then performs some tasks
using those types.