SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
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

Weitere Àhnliche Inhalte

Was ist angesagt?

Multithreading
MultithreadingMultithreading
MultithreadingA B Shinde
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationAnas Ebrahim
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
Semaphores
SemaphoresSemaphores
SemaphoresMohd Arif
 
Threads .ppt
Threads .pptThreads .ppt
Threads .pptmeet darji
 
Distributed & parallel system
Distributed & parallel systemDistributed & parallel system
Distributed & parallel systemManish Singh
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategiesDr. Loganathan R
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Presentation on flynn’s classification
Presentation on flynn’s classificationPresentation on flynn’s classification
Presentation on flynn’s classificationvani gupta
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronizationShakshi Ranawat
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating SystemsDr Sandeep Kumar Poonia
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Mukesh Chinta
 

Was ist angesagt? (20)

Multithreading
MultithreadingMultithreading
Multithreading
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Amdahl`s law -Processor performance
Amdahl`s law -Processor performanceAmdahl`s law -Processor performance
Amdahl`s law -Processor performance
 
Distributed & parallel system
Distributed & parallel systemDistributed & parallel system
Distributed & parallel system
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Presentation on flynn’s classification
Presentation on flynn’s classificationPresentation on flynn’s classification
Presentation on flynn’s classification
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronization
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems8. mutual exclusion in Distributed Operating Systems
8. mutual exclusion in Distributed Operating Systems
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Shared memory
Shared memoryShared memory
Shared memory
 
Java threads
Java threadsJava threads
Java threads
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
 

Andere mochten auch

Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#Rizwan Ali
 
Reproduction-PUC_II
Reproduction-PUC_IIReproduction-PUC_II
Reproduction-PUC_IIManisha Chhatre
 
Processor / CPU Scheduling
Processor / CPU SchedulingProcessor / CPU Scheduling
Processor / CPU SchedulingIzaz Roghani
 
Programação Concorrente - Aula 02
Programação Concorrente - Aula 02Programação Concorrente - Aula 02
Programação Concorrente - Aula 02thomasdacosta
 
Address Binding Scheme
Address Binding SchemeAddress Binding Scheme
Address Binding SchemeRajesh Piryani
 
Logical Clocks (Distributed computing)
Logical Clocks (Distributed computing)Logical Clocks (Distributed computing)
Logical Clocks (Distributed computing)Sri Prasanna
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-GalvinSonali Chauhan
 
Synchronization in distributed systems
Synchronization in distributed systems Synchronization in distributed systems
Synchronization in distributed systems SHATHAN
 
OS Process and Thread Concepts
OS Process and Thread ConceptsOS Process and Thread Concepts
OS Process and Thread Conceptssgpraju
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling AlgorithmsShubhashish Punj
 

Andere mochten auch (11)

Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Reproduction-PUC_II
Reproduction-PUC_IIReproduction-PUC_II
Reproduction-PUC_II
 
Processor / CPU Scheduling
Processor / CPU SchedulingProcessor / CPU Scheduling
Processor / CPU Scheduling
 
Programação Concorrente - Aula 02
Programação Concorrente - Aula 02Programação Concorrente - Aula 02
Programação Concorrente - Aula 02
 
Address Binding Scheme
Address Binding SchemeAddress Binding Scheme
Address Binding Scheme
 
Threads
ThreadsThreads
Threads
 
Logical Clocks (Distributed computing)
Logical Clocks (Distributed computing)Logical Clocks (Distributed computing)
Logical Clocks (Distributed computing)
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Synchronization in distributed systems
Synchronization in distributed systems Synchronization in distributed systems
Synchronization in distributed systems
 
OS Process and Thread Concepts
OS Process and Thread ConceptsOS Process and Thread Concepts
OS Process and Thread Concepts
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 

Ähnlich wie Thread (20)

OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
4.Process.ppt
4.Process.ppt4.Process.ppt
4.Process.ppt
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Bglrsession4
Bglrsession4Bglrsession4
Bglrsession4
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Networking threads
Networking threadsNetworking threads
Networking threads
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
 
thread os.pptx
thread os.pptxthread os.pptx
thread os.pptx
 
Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
 
Thread
ThreadThread
Thread
 
Os
OsOs
Os
 
multi-threading
multi-threadingmulti-threading
multi-threading
 
Threads
ThreadsThreads
Threads
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Treads
TreadsTreads
Treads
 
4 threads
4 threads4 threads
4 threads
 

Mehr von Mohd Arif

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcpMohd Arif
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarpMohd Arif
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocolMohd Arif
 
Project identification
Project identificationProject identification
Project identificationMohd Arif
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniquesMohd Arif
 
Presentation
PresentationPresentation
PresentationMohd Arif
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peerMohd Arif
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systemsMohd Arif
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdpMohd Arif
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgetingMohd Arif
 
Network management
Network managementNetwork management
Network managementMohd Arif
 
Networing basics
Networing basicsNetworing basics
Networing basicsMohd Arif
 
Loaders
LoadersLoaders
LoadersMohd Arif
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformMohd Arif
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and sslMohd Arif
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psecMohd Arif
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardwareMohd Arif
 
Heap sort
Heap sortHeap sort
Heap sortMohd Arif
 

Mehr von Mohd Arif (20)

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcp
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocol
 
Project identification
Project identificationProject identification
Project identification
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniques
 
Presentation
PresentationPresentation
Presentation
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peer
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systems
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgeting
 
Network management
Network managementNetwork management
Network management
 
Networing basics
Networing basicsNetworing basics
Networing basics
 
Loaders
LoadersLoaders
Loaders
 
Lists
ListsLists
Lists
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platform
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and ssl
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psec
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardware
 
Heap sort
Heap sortHeap sort
Heap sort
 

KĂŒrzlich hochgeladen

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

KĂŒrzlich hochgeladen (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

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