SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Concept of Thread




Munmun Das Bhowmik
Objectives




● Resource sharing
● Performance

● Parallel computing
Few terms to be used :

●   Processor : runs a code stream
●   Function : a particular code stream
●   Schedule : time slice given by processor
●   Process : instance of a program being executed
●   Registers : physical storage of CPU
●   Kernel : interface between hardware & software
●   Thread : an execution context for processor
●   Thread context : The register set, stacks & private
    storage area are known as context of thread.
What is a Thread ?
light-weight process
is the smallest sequence of programmed instructions that
can be managed independently by an operating system
scheduler
Benefits :



•   Resourse sharing : On a single-processor machine, the
    operating system is switching rapidly between the threads,
    giving the appearance of simultaneous execution.
•   Responsiveness : Maintain a responsive user interface
    while background tasks are executing
•   Distinguish tasks of varying priority
•   Economy : Perform operations that consume a large
    amount of time without stopping the rest of the application.
•   Another example is a web server - Multiple threads allow
    for multiple requests to be satisfied simultaneously,
    without having to service requests sequentially or to fork
    off separate processes for every incoming request. ( The
    latter is how this sort of thing was done before the
    concept of threads was developed. A daemon would
    listen at a port, fork off a child for every incoming request
    to be processed, and then go back to listening to the
    port. )
User & Kernel threads :

●   User threads :
    ●   User threads are scheduled in user space by the
        process itself. Usually these are provided as a
        feature of a computer language.
●   Kernel threads :
    ●   These can be scheduled across different CPU's in
        a multiprocessor system
    ●   Kernel threads interact intelligently with the I/O
        subsystems of the Kernel. This means that while
        one of your threads waits on I/O, the others will
        continue to run
Models :




 User thread(N:1) :    Kernel thread (1:1):     Hybrid (M:N) :
  user library
                                                ●
                                                 N number of application
 ●                     ●
                        simplest possible
  no kernel                                     threads onto some M number
 ●
                       threading implementation
 modifications                                  of kernel entities, or "virtual
                       ●
                        scheduling/             processors."
 flexible & low cost   synchronization
 ●
  thread may block
                                                ●
                                                 are more complex to
                       coordination             implement than either kernel
 entire process, no
 parallelism
                       ●
                         less overhead than     or user threads, because
 ●
  Eg : Win 32          process, suitable for    changes to both kernel and
                       parallel application     user-space code are required
                                                ●
                                                 Eg : Windows 7
Questions ?

●   What happens when too many user threads are created ?
●   Does all sequential process converted to mutiple threads
    has benefits ?
●   Which type of thread is used to create new tab in web
    browser ?
●   What type of threads are handled by device drivers ?
●   What is hardware virtualization ?
Threads in C# library
Namespace : System.Threading
States :




  The ThreadState property is useful for diagnostic purposes.
C# supports parallel execution of code through multithreading

 ●   A C# client program (Console, WPF, or Windows Forms) starts
     in a single thread created automatically by the CLR and
     operating system (the “main” thread), and is made multithreaded
     by creating additional threads.




 ●   WCF, ASP.NET, and Web Services applications are
     implicitly multithreaded
If threads are so good, why not use them everywhere?



   ●
       When mutiple threads tries to access shared data
   ●
       Concurrent access to variables
   ●
       To avoid race conditions : .This is a scenario where
       two or more threads are competing(racing) to
       read/write from a shared memory location leading to
       the possibilities of leaving the program in an
       inconsistent state.
Synchronization
Block methods : Sleep , Wait, Task.Wait
Lock Constructs :
Construct                    Purpose                                  Cross process

Lock(Monitor.Enter/          only one thread can access a resource    -
Monitor.Exit)                or enter the critical zone

Mutex                                                                 yes

SemaphoreSlim (.Net 4.0)     Not more than a specified number of      -
                             threads can access a resource or enter
Semaphore                    the critical zone                        yes


ReaderWriterLockSlim (.Net   Allows multiple readers to coexist       -
3.5)                         with a single writer

ReaderWriterLock                                                      -
Protecting shared data in safer way

●   Thread – safety concept :


                    wait()
              T1                       Shared data
                   T2      Exclusive
              T3
                   Sleep()   lock



  While waiting on a Sleep or Join, a thread is blocked
and so does not consume CPU resources.
Points to consider while designing
    Threading has resource requirements & potential
    conflicts.
●   Few threads should be used as it will minimize the
    operating system resources & improving performance.
●   Memory consumed for context those required by
    AppDomain objects , processes & threads.
●   Consumes processor time. If most of current threads are
    in one process, then other threads processes are
    scheduled less frequently.
●   Thread pooling / thread spawning
●   Shared access to resources creates conflicts.
●   Failure to synchronize causes deadlocks or race
    conditions.
Questions :

●   Benefits of thread pooling ?
●   How to handle long running threads ?
●   What about the performance ?
●   What is the mechanism for Worker thread ?
●   Different ways to create muti-threaded applications
    in .Net ?
    ●   Explicit thread class , task parallel library , lambda
        functions , BeginInvoke , BackgroundWorker
Thank you !

Weitere ähnliche Inhalte

Was ist angesagt?

Multi threaded rtos
Multi threaded rtosMulti threaded rtos
Multi threaded rtos
James Wong
 
Multithreading computer architecture
 Multithreading computer architecture  Multithreading computer architecture
Multithreading computer architecture
Haris456
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading models
anoopkrishna2
 
FIne Grain Multithreading
FIne Grain MultithreadingFIne Grain Multithreading
FIne Grain Multithreading
Dharmesh Tank
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
Sonali Chauhan
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.ppt
Luis Goldster
 
Graphics processing uni computer archiecture
Graphics processing uni computer archiectureGraphics processing uni computer archiecture
Graphics processing uni computer archiecture
Haris456
 

Was ist angesagt? (20)

Thread
ThreadThread
Thread
 
PThreads Vs Win32 Threads
PThreads  Vs  Win32 ThreadsPThreads  Vs  Win32 Threads
PThreads Vs Win32 Threads
 
Multithreaded processors ppt
Multithreaded processors pptMultithreaded processors ppt
Multithreaded processors ppt
 
Multi threaded rtos
Multi threaded rtosMulti threaded rtos
Multi threaded rtos
 
Multithreading computer architecture
 Multithreading computer architecture  Multithreading computer architecture
Multithreading computer architecture
 
Memory models
Memory modelsMemory models
Memory models
 
4 threads
4 threads4 threads
4 threads
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading models
 
Ch4 Threads
Ch4 ThreadsCh4 Threads
Ch4 Threads
 
FIne Grain Multithreading
FIne Grain MultithreadingFIne Grain Multithreading
FIne Grain Multithreading
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
OS_Ch5
OS_Ch5OS_Ch5
OS_Ch5
 
Threads
ThreadsThreads
Threads
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Multicore Processors
Multicore ProcessorsMulticore Processors
Multicore Processors
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.ppt
 
Thread management
Thread management Thread management
Thread management
 
Graphics processing uni computer archiecture
Graphics processing uni computer archiectureGraphics processing uni computer archiecture
Graphics processing uni computer archiecture
 

Ähnlich wie Concept of thread

OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
Trinh Phuc Tho
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 

Ähnlich wie Concept of thread (20)

Architecture of web servers
Architecture of web serversArchitecture of web servers
Architecture of web servers
 
Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
 
Thread
ThreadThread
Thread
 
Threads
ThreadsThreads
Threads
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
W-9.pptx
W-9.pptxW-9.pptx
W-9.pptx
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
Wiki 2
Wiki 2Wiki 2
Wiki 2
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
4.Process.ppt
4.Process.ppt4.Process.ppt
4.Process.ppt
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingVTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computing
 
Coding For Cores - C# Way
Coding For Cores - C# WayCoding For Cores - C# Way
Coding For Cores - C# Way
 
network ram parallel computing
network ram parallel computingnetwork ram parallel computing
network ram parallel computing
 
Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
 
dos slide share.pptx
dos slide share.pptxdos slide share.pptx
dos slide share.pptx
 
Deep Dive into Node.js Event Loop.pdf
Deep Dive into Node.js Event Loop.pdfDeep Dive into Node.js Event Loop.pdf
Deep Dive into Node.js Event Loop.pdf
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
 

Concept of thread

  • 2. Objectives ● Resource sharing ● Performance ● Parallel computing
  • 3. Few terms to be used : ● Processor : runs a code stream ● Function : a particular code stream ● Schedule : time slice given by processor ● Process : instance of a program being executed ● Registers : physical storage of CPU ● Kernel : interface between hardware & software ● Thread : an execution context for processor ● Thread context : The register set, stacks & private storage area are known as context of thread.
  • 4. What is a Thread ? light-weight process is the smallest sequence of programmed instructions that can be managed independently by an operating system scheduler
  • 5. Benefits : • Resourse sharing : On a single-processor machine, the operating system is switching rapidly between the threads, giving the appearance of simultaneous execution. • Responsiveness : Maintain a responsive user interface while background tasks are executing • Distinguish tasks of varying priority • Economy : Perform operations that consume a large amount of time without stopping the rest of the application.
  • 6. Another example is a web server - Multiple threads allow for multiple requests to be satisfied simultaneously, without having to service requests sequentially or to fork off separate processes for every incoming request. ( The latter is how this sort of thing was done before the concept of threads was developed. A daemon would listen at a port, fork off a child for every incoming request to be processed, and then go back to listening to the port. )
  • 7. User & Kernel threads : ● User threads : ● User threads are scheduled in user space by the process itself. Usually these are provided as a feature of a computer language. ● Kernel threads : ● These can be scheduled across different CPU's in a multiprocessor system ● Kernel threads interact intelligently with the I/O subsystems of the Kernel. This means that while one of your threads waits on I/O, the others will continue to run
  • 8. Models : User thread(N:1) : Kernel thread (1:1): Hybrid (M:N) : user library ● N number of application ● ● simplest possible no kernel threads onto some M number ● threading implementation modifications of kernel entities, or "virtual ● scheduling/ processors." flexible & low cost synchronization ● thread may block ● are more complex to coordination implement than either kernel entire process, no parallelism ● less overhead than or user threads, because ● Eg : Win 32 process, suitable for changes to both kernel and parallel application user-space code are required ● Eg : Windows 7
  • 9. Questions ? ● What happens when too many user threads are created ? ● Does all sequential process converted to mutiple threads has benefits ? ● Which type of thread is used to create new tab in web browser ? ● What type of threads are handled by device drivers ? ● What is hardware virtualization ?
  • 10. Threads in C# library Namespace : System.Threading States : The ThreadState property is useful for diagnostic purposes.
  • 11. C# supports parallel execution of code through multithreading ● A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. ● WCF, ASP.NET, and Web Services applications are implicitly multithreaded
  • 12. If threads are so good, why not use them everywhere? ● When mutiple threads tries to access shared data ● Concurrent access to variables ● To avoid race conditions : .This is a scenario where two or more threads are competing(racing) to read/write from a shared memory location leading to the possibilities of leaving the program in an inconsistent state.
  • 13. Synchronization Block methods : Sleep , Wait, Task.Wait Lock Constructs : Construct Purpose Cross process Lock(Monitor.Enter/ only one thread can access a resource - Monitor.Exit) or enter the critical zone Mutex yes SemaphoreSlim (.Net 4.0) Not more than a specified number of - threads can access a resource or enter Semaphore the critical zone yes ReaderWriterLockSlim (.Net Allows multiple readers to coexist - 3.5) with a single writer ReaderWriterLock -
  • 14. Protecting shared data in safer way ● Thread – safety concept : wait() T1 Shared data T2 Exclusive T3 Sleep() lock While waiting on a Sleep or Join, a thread is blocked and so does not consume CPU resources.
  • 15. Points to consider while designing Threading has resource requirements & potential conflicts. ● Few threads should be used as it will minimize the operating system resources & improving performance. ● Memory consumed for context those required by AppDomain objects , processes & threads. ● Consumes processor time. If most of current threads are in one process, then other threads processes are scheduled less frequently. ● Thread pooling / thread spawning ● Shared access to resources creates conflicts. ● Failure to synchronize causes deadlocks or race conditions.
  • 16. Questions : ● Benefits of thread pooling ? ● How to handle long running threads ? ● What about the performance ? ● What is the mechanism for Worker thread ? ● Different ways to create muti-threaded applications in .Net ? ● Explicit thread class , task parallel library , lambda functions , BeginInvoke , BackgroundWorker