SlideShare a Scribd company logo
Scheduling  Thread
What is Thread
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.
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.
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
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.
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.
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.
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.
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.
Private Assembly Deployment
Shared Assembly Deployment
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Scheduling  Thread
Late Binding
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.
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.
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);
Reflection
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.
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).
• 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);
Scheduling  Thread
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.
• 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.

More Related Content

PPT
Introto netthreads-090906214344-phpapp01
PDF
Concurrency and parallel in .net
PPT
Intro To .Net Threads
PDF
.Net Threading
PPTX
Multi core programming 2
PDF
Sync, async and multithreading
PPTX
.NET Multithreading/Multitasking
Introto netthreads-090906214344-phpapp01
Concurrency and parallel in .net
Intro To .Net Threads
.Net Threading
Multi core programming 2
Sync, async and multithreading
.NET Multithreading/Multitasking

Similar to Scheduling Thread (20)

PPT
Web services, WCF services and Multi Threading with Windows Forms
PPT
Introduction to c_sharp
PPTX
.Net framework
PPT
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
ODP
Concept of thread
ODP
Multithreading 101
PPT
Dot Net Framework
PPT
.Net Session Overview
PPT
Inside .net framework
PPTX
Memory models in c#
PPTX
Threading
PPTX
Coding For Cores - C# Way
PPTX
PERTEMUAN 1 - MENGENAL ENVIRONTMENT PROGRAM VISUAL C#.pptx
PDF
Threading in c#
PDF
Dotnet interview qa
PPTX
Multithreading and concurrency.pptx
PDF
.Net Multithreading and Parallelization
DOCX
C# Unit 1 notes
PPS
09 gui 13
PPT
David buksbaum a-briefintroductiontocsharp
Web services, WCF services and Multi Threading with Windows Forms
Introduction to c_sharp
.Net framework
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
Concept of thread
Multithreading 101
Dot Net Framework
.Net Session Overview
Inside .net framework
Memory models in c#
Threading
Coding For Cores - C# Way
PERTEMUAN 1 - MENGENAL ENVIRONTMENT PROGRAM VISUAL C#.pptx
Threading in c#
Dotnet interview qa
Multithreading and concurrency.pptx
.Net Multithreading and Parallelization
C# Unit 1 notes
09 gui 13
David buksbaum a-briefintroductiontocsharp
Ad

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Pharma ospi slides which help in ospi learning
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
Yogi Goddess Pres Conference Studio Updates
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial diseases, their pathogenesis and prophylaxis
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Weekly quiz Compilation Jan -July 25.pdf
Cell Structure & Organelles in detailed.
FourierSeries-QuestionsWithAnswers(Part-A).pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Pharma ospi slides which help in ospi learning
Ad

Scheduling Thread

  • 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.
  • 11. Private Assembly Deployment Shared Assembly Deployment
  • 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);
  • 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.