SlideShare a Scribd company logo
Part II
Process Management
    Chapter 4: Threads


                         1
What Is a Thread?
§ A thread, also known as lightweight process
  (LWP), is a basic unit of CPU execution.
§ A thread has a thread ID, a program counter, a
  register set, and a stack. Thus, it is similar to a
  process has.
§ However, a thread shares with other threads in
  the same process its code section, data section,
  and other OS resources (e.g., files and signals).
§ A process, or heavyweight process, has a single
  thread of control.

                                                    2
Single Threaded and
           Multithreaded Process
Single-threaded process   Multithreaded Process




                                                  3
Benefits of Using Threads
§ Responsiveness: Other parts (i.e., threads) of a
  program may still be running even if one part
  (e.g., a thread) is blocked.
§ Resource Sharing: Threads of a process, by
  default, share many system resources (e.g., files
  and memory).
§ Economy: Creating and terminating processes,
  allocating memory and resources, and context
  switching processes are very time consuming.
§ Utilization of Multiprocessor Architecture:
  Multiple CPUs can run multiple threads of the
  same process. No program change is necessary.
                                                      4
User and Kernel Threads: 1/3
q User Threads:
   vUser threads are supported at the user level.
    The kernel is not aware of user threads.
   vA library provides all support for thread
    creation, termination, joining, and scheduling.
   vThere is no kernel intervention, and, hence,
    user threads are usually more efficient.
   vUnfortunately, since the kernel only recognizes
    the containing process (of the threads), if one
    thread is blocked, every other threads of the same
    process are also blocked because the containing
    process is blocked.
                                                    5
User and Kernel Threads: 2/3
qKernel threads:
 vKernel threads are directly supported by the
   kernel. The kernel does thread creation,
   termination, joining, and scheduling in kernel
   space.
 vKernel threads are usually slower than the user
   threads.
 vHowever, blocking one thread will not cause other
   threads of the same process to block. The kernel
   simply runs other threads.
 vIn a multiprocessor environment, the kernel can
   schedule threads on different processors.      6
User and Kernel Threads: 3/3




                               7
Multithreading Models
qDifferent systems support threads in different
 ways. Here are three commonly seen thread
 models:
 vMany-to-One Model:One kernel thread (or
   process) has multiple user threads. Thus, this
   is a user thread model.
 vOne-to-One Model: one user thread maps to
   one kernel thread (e.g., Linux and Windows).
 vMany-to-Many Model: Multiple user threads
   maps to a number of kernel threads.
                                                8
Many-to-One Model




                    9
One-to-One Model: 1/2
An Extreme Case: Traditional Unix




                                    10
One-to-One Model: 2/2




                        11
Many-to-Many Model




                     12
Thread Issues

§   How Does a Thread Fork?
§   Thread Cancellation
§   Signal Handling
§   Thread-Specific Data
§   What Is Thread-Safe?



                              13
How Does a Thread Fork?
qIf a thread forks, does the new process:
  vduplicate all threads?
  vcontain only the forking thread (i.e., single-
    threaded)?
qSome systems have two fork system calls, one
 for each case.



                                                    14
Thread Cancellation: 1/2
qThread cancellation means terminating a thread
 before it has completed. The thread that is to be
 cancelled is the target thread.
qThere are two types:
  vAsynchronous Cancellation: the target thread
   terminates immediately.
  vDeferred Cancellation: The target thread can
   periodically check if it should terminate,
   allowing the target thread an opportunity to
   terminate itself in an orderly fashion. The
   point a thread can terminate itself is a
   cancellation point.
                                                15
Thread Cancellation: 2/2
qProblem: With asynchronous cancellation, if the
 target thread owns some system-wide resources, the
 system may not be able to reclaim all recourses
 owned by the target thread.
qWith deferred cancellation, the target thread
 determines the time to terminate itself. Reclaiming
 resources is not a problem.
qMost systems implement asynchronous cancellation
 for processes (e.g., use the kill system call) and
 threads.
qPthread supports deferred cancellation.
                                                16
Signal Handling
§ Signals is a way the OS uses to notify a
  process that some event has happened.
§ Once a signal occurs, who is going to
  handle it? The process, or one of the
  threads?
§ This is a very complex issue and will be
  discussed later in this course.


                                             17
Thread-Specific Data/Thread-Safe
qData that a thread needs for its own operation are
 thread-specific.
qPoor support for thread-specific data could cause
 problem. For example, while threads have their
 own stacks, they share the heap.
qWhat if two malloc() or new are executed at the
 same time requesting for memory from the heap?
 Or, two printf or cout are run simultaneously?
qIf a library can be used by multiple threads
 properly, it is a thread-safe one.
                                                 18
Thread Pool
qWhile we know that managing threads are more
 efficient than managing processes, creating and
 terminating threads are still not free.
qAfter a process is created, one can immediately
 create a number of threads and have them waiting.
qWhen a new task occurs, one can wake up one of
 the waiting threads and assign it the work. After
 this thread completes the task, it goes back to wait.
qIn this way, we save the number of thread creation
 and termination.
qThese threads are said in a thread pool.
                                                   19

More Related Content

PPTX
Software Evolution
PPTX
System calls
PDF
6 cpu scheduling
PPTX
Process in operating system
PPT
Memory Management in OS
PPT
Os Threads
PPTX
Threads (operating System)
PPT
Chapter 3 - Processes
Software Evolution
System calls
6 cpu scheduling
Process in operating system
Memory Management in OS
Os Threads
Threads (operating System)
Chapter 3 - Processes

What's hot (20)

PPTX
Osi model vs TCP/IP
PPT
Chapter 1: Introduction to Operating System
PDF
Deadlock Avoidance - OS
PPT
Operating Systems - "Chapter 4: Multithreaded Programming"
PPTX
SCHEDULING ALGORITHMS
PPTX
Session Layer
PPT
Services provided by os
PDF
Unit II - 3 - Operating System - Process Synchronization
PPT
Object Oriented Design
PPTX
Message passing in Distributed Computing Systems
PPT
Coupling and cohesion
PPT
Chapter 3: Processes
PPTX
Multiprogramming&timesharing
PPTX
Multithreading
PDF
Event Driven programming(ch1 and ch2).pdf
PDF
Operating systems system structures
PDF
Incremental model
PPTX
Object Oriented Design
PPTX
Process scheduling
Osi model vs TCP/IP
Chapter 1: Introduction to Operating System
Deadlock Avoidance - OS
Operating Systems - "Chapter 4: Multithreaded Programming"
SCHEDULING ALGORITHMS
Session Layer
Services provided by os
Unit II - 3 - Operating System - Process Synchronization
Object Oriented Design
Message passing in Distributed Computing Systems
Coupling and cohesion
Chapter 3: Processes
Multiprogramming&timesharing
Multithreading
Event Driven programming(ch1 and ch2).pdf
Operating systems system structures
Incremental model
Object Oriented Design
Process scheduling
Ad

Viewers also liked (11)

PPT
Threads And Synchronization in C#
PPTX
Reproduction-PUC_II
PPT
Processor / CPU Scheduling
PPTX
Programação Concorrente - Aula 02
PDF
Address Binding Scheme
PPTX
Threads
PPT
Logical Clocks (Distributed computing)
PPT
Operating System-Threads-Galvin
PPT
Synchronization in distributed systems
PPT
OS Process and Thread Concepts
PPT
CPU Scheduling Algorithms
Threads And Synchronization in C#
Reproduction-PUC_II
Processor / CPU Scheduling
Programação Concorrente - Aula 02
Address Binding Scheme
Threads
Logical Clocks (Distributed computing)
Operating System-Threads-Galvin
Synchronization in distributed systems
OS Process and Thread Concepts
CPU Scheduling Algorithms
Ad

Similar to Thread (20)

PPTX
Networking threads
PDF
Threads operating system slides easy understand
PDF
Multithreaded Programming in oprating system
PPT
PDF
Multithreaded Programming Part- III.pdf
PDF
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
PDF
4 threads
PPTX
Engineeering Operating systemsOS UNIT 3 Threads.pptx
PPTX
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
PPT
Operating System 4
PPT
Operating System 4 1193308760782240 2
PDF
The Thread Chapter 4 of Operating System
PPTX
Threads, signal and socket system calls.pptx
PPTX
Lecture 3 threads
PPT
chapter4-processes nd processors in DS.ppt
PPT
Ch04 threads
PPTX
OS Module-2.pptx
PPT
15 threads
DOC
PDF
Sucet os module_2_notes
Networking threads
Threads operating system slides easy understand
Multithreaded Programming in oprating system
Multithreaded Programming Part- III.pdf
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
4 threads
Engineeering Operating systemsOS UNIT 3 Threads.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
Operating System 4
Operating System 4 1193308760782240 2
The Thread Chapter 4 of Operating System
Threads, signal and socket system calls.pptx
Lecture 3 threads
chapter4-processes nd processors in DS.ppt
Ch04 threads
OS Module-2.pptx
15 threads
Sucet os module_2_notes

More from Mohd Arif (20)

PPT
Bootp and dhcp
PPT
Arp and rarp
PPT
User datagram protocol
PPT
Project identification
PPT
Project evalaution techniques
PPT
Presentation
PPT
Pointers in c
PPT
Peer to-peer
PPT
Overview of current communications systems
PPT
Overall 23 11_2007_hdp
PPT
Objectives of budgeting
PPT
Network management
PPT
Networing basics
PPT
Loaders
PPT
Lists
PPT
Iris ngx next generation ip based switching platform
PPT
Ip sec and ssl
PPT
Ip security in i psec
PPT
Intro to comp. hardware
PPT
Heap sort
Bootp and dhcp
Arp and rarp
User datagram protocol
Project identification
Project evalaution techniques
Presentation
Pointers in c
Peer to-peer
Overview of current communications systems
Overall 23 11_2007_hdp
Objectives of budgeting
Network management
Networing basics
Loaders
Lists
Iris ngx next generation ip based switching platform
Ip sec and ssl
Ip security in i psec
Intro to comp. hardware
Heap sort

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Encapsulation theory and applications.pdf
PDF
Mushroom cultivation and it's methods.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Spectroscopy.pptx food analysis technology
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
OMC Textile Division Presentation 2021.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Empathic Computing: Creating Shared Understanding
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Assigned Numbers - 2025 - Bluetooth® Document
A comparative study of natural language inference in Swahili using monolingua...
Encapsulation theory and applications.pdf
Mushroom cultivation and it's methods.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectroscopy.pptx food analysis technology
SOPHOS-XG Firewall Administrator PPT.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
OMC Textile Division Presentation 2021.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Spectral efficient network and resource selection model in 5G networks
A Presentation on Artificial Intelligence

Thread

  • 1. Part II Process Management Chapter 4: Threads 1
  • 2. What Is a Thread? § A thread, also known as lightweight process (LWP), is a basic unit of CPU execution. § A thread has a thread ID, a program counter, a register set, and a stack. Thus, it is similar to a process has. § However, a thread shares with other threads in the same process its code section, data section, and other OS resources (e.g., files and signals). § A process, or heavyweight process, has a single thread of control. 2
  • 3. Single Threaded and Multithreaded Process Single-threaded process Multithreaded Process 3
  • 4. Benefits of Using Threads § Responsiveness: Other parts (i.e., threads) of a program may still be running even if one part (e.g., a thread) is blocked. § Resource Sharing: Threads of a process, by default, share many system resources (e.g., files and memory). § Economy: Creating and terminating processes, allocating memory and resources, and context switching processes are very time consuming. § Utilization of Multiprocessor Architecture: Multiple CPUs can run multiple threads of the same process. No program change is necessary. 4
  • 5. User and Kernel Threads: 1/3 q User Threads: vUser threads are supported at the user level. The kernel is not aware of user threads. vA library provides all support for thread creation, termination, joining, and scheduling. vThere is no kernel intervention, and, hence, user threads are usually more efficient. vUnfortunately, since the kernel only recognizes the containing process (of the threads), if one thread is blocked, every other threads of the same process are also blocked because the containing process is blocked. 5
  • 6. User and Kernel Threads: 2/3 qKernel threads: vKernel threads are directly supported by the kernel. The kernel does thread creation, termination, joining, and scheduling in kernel space. vKernel threads are usually slower than the user threads. vHowever, blocking one thread will not cause other threads of the same process to block. The kernel simply runs other threads. vIn a multiprocessor environment, the kernel can schedule threads on different processors. 6
  • 7. User and Kernel Threads: 3/3 7
  • 8. Multithreading Models qDifferent systems support threads in different ways. Here are three commonly seen thread models: vMany-to-One Model:One kernel thread (or process) has multiple user threads. Thus, this is a user thread model. vOne-to-One Model: one user thread maps to one kernel thread (e.g., Linux and Windows). vMany-to-Many Model: Multiple user threads maps to a number of kernel threads. 8
  • 10. One-to-One Model: 1/2 An Extreme Case: Traditional Unix 10
  • 13. Thread Issues § How Does a Thread Fork? § Thread Cancellation § Signal Handling § Thread-Specific Data § What Is Thread-Safe? 13
  • 14. How Does a Thread Fork? qIf a thread forks, does the new process: vduplicate all threads? vcontain only the forking thread (i.e., single- threaded)? qSome systems have two fork system calls, one for each case. 14
  • 15. Thread Cancellation: 1/2 qThread cancellation means terminating a thread before it has completed. The thread that is to be cancelled is the target thread. qThere are two types: vAsynchronous Cancellation: the target thread terminates immediately. vDeferred Cancellation: The target thread can periodically check if it should terminate, allowing the target thread an opportunity to terminate itself in an orderly fashion. The point a thread can terminate itself is a cancellation point. 15
  • 16. Thread Cancellation: 2/2 qProblem: With asynchronous cancellation, if the target thread owns some system-wide resources, the system may not be able to reclaim all recourses owned by the target thread. qWith deferred cancellation, the target thread determines the time to terminate itself. Reclaiming resources is not a problem. qMost systems implement asynchronous cancellation for processes (e.g., use the kill system call) and threads. qPthread supports deferred cancellation. 16
  • 17. Signal Handling § Signals is a way the OS uses to notify a process that some event has happened. § Once a signal occurs, who is going to handle it? The process, or one of the threads? § This is a very complex issue and will be discussed later in this course. 17
  • 18. Thread-Specific Data/Thread-Safe qData that a thread needs for its own operation are thread-specific. qPoor support for thread-specific data could cause problem. For example, while threads have their own stacks, they share the heap. qWhat if two malloc() or new are executed at the same time requesting for memory from the heap? Or, two printf or cout are run simultaneously? qIf a library can be used by multiple threads properly, it is a thread-safe one. 18
  • 19. Thread Pool qWhile we know that managing threads are more efficient than managing processes, creating and terminating threads are still not free. qAfter a process is created, one can immediately create a number of threads and have them waiting. qWhen a new task occurs, one can wake up one of the waiting threads and assign it the work. After this thread completes the task, it goes back to wait. qIn this way, we save the number of thread creation and termination. qThese threads are said in a thread pool. 19