SlideShare a Scribd company logo
1 of 31
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition,
Chapter 4: Threads
Changes by MA Doman 2013
4.2 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Chapter 4: Threads
 Overview
 Multithreading Models
 Thread Libraries
 Threading Issues
 Operating System Examples
 Windows XP Threads
 Linux Threads
4.3 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Objectives
 To introduce the notion of a thread — a fundamental unit of CPU utilization
that forms the basis of multithreaded computer systems
 To discuss the APIs for the Pthreads, Win32, and Java thread libraries
 To examine issues related to multithreaded programming
4.4 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Thread Overview
 Threads are mechanisms that permit an application to perform multiple
tasks concurrently.
 A web browser might have one thread display images or text while another
thread retrieves data from the network, for example. A word processor may
have a thread for displaying graphics, another thread for responding to
keystrokes from the user, and a third thread for performing spelling and
grammar checking in the background.
4.5 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Thread Overview
 Threads are mechanisms that permit an application to perform multiple
tasks concurrently.
 Thread is a basic unit of CPU utilization. A thread is a flow of execution
through the process code, with its own program counter that keeps track of
which instruction to execute next, system registers which hold its current
working variables.
 Thread ID
 Program counter
 Register set
 Stack
 A single program can contain multiple threads
 Threads share with other threads belonging to the same process
 Code, data, open files…….
4.6 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Single and Multithreaded Processes
Traditional (heavyweight)
process has a single thread
of control
heavyweight process lightweight process
4.7 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Threads in Memory
Memory is allocated for a process in segments or parts:
0000000
Increasing
virtual
address
Text ( program code)
Initialized data
Un-initialized data
Heap
Stack for main thread
argv, environment
Stack for thread 2
Stack for thread 3
Stack for thread 1
Main thread executing here
Thread 1 executing here
Thread 3 executing here
Thread 2 executing here
4.8 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Benefits
 Responsiveness
Interactive application can delegate background functions to a thread and keep
running. Multithreading an interactive application may allow a program to
continue running even if part of it is blocked or is performing a lengthy operation,
thereby increasing responsiveness to the user.
 Resource Sharing
Several different threads can access the same address space. Threads share
the memory and the resources of the process to which they belong by default.
 Economy
Allocating memory and new processes is costly. Threads are much ‘cheaper’ to
initiate. In Solaris, for example, creating a process is about thirty times slower
than is creating a thread, and context switching is about five times slower.
 Scalability
Use threads to take advantage of multiprocessor architecture. where threads
may be running in parallel on different processing cores. A single-threaded
process can run on only one processor, regardless how many are available
4.9 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Multithreaded Server Architecture
 In certain situations, a single application may be required to perform several
similar tasks. For example, a web server accepts client requests for web
pages, images, sound, and so forth. A busy web server may have
several(perhaps thousands of) clients concurrently accessing it.
 Solution?
 One solution is to have the server run as a single process that accepts
requests. When the server receives a request, it creates a separate process
to service that request. In fact, this process-creation method was in
common use before threads became popular. Process creation is time
consuming and resource intensive, however.
 It is generally more efficient to use one process that contains multiple
threads. If the web-server process is multithreaded, the server will create a
separate thread that listens for client requests. When a request is made,
rather than creating another process, the server creates a new thread to
service the request and resume listening for additional requests.
4.10 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Multithreaded Server Architecture
thread
thread
thread
thread
4.11 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Concurrent Execution on a Single-core System
Multicore Programming
Parallel Execution on a Multicore System
4.12 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Parallel vs Concurrent
 A system is parallel if it can perform more than one task simultaneously.
 In contrast, a concurrent system supports more than one task by allowing all
the tasks to make progress. Thus, it is possible to have concurrency without
parallelism.
4.13 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Multicore Programming
 Multicore systems putting pressure on programmers, challenges include
 Dividing activities
 What tasks can be separated to run on different processors
 Balance
 Balance work on all processors
 Data splitting
 Separate data to run with the tasks
 Data dependency
 Watch for dependences between tasks
 Testing and debugging
 Harder!!!!
4.14 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Multithreading Models
 Support provided at either
 User level -> user threads
Supported above the kernel and managed without kernel support
They are created and managed by users. They are used at the application
level. There is no involvement of the OS. A good example is when we use
threading in programming like in Java, C#, Python, etc, we use user
threads.
4.15 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Multithreading Models
 Kernel level -> kernel threads
Supported and managed directly by the operating system. All modern
OSes support kernel level threads, allowing the kernel to perform multiple
simultaneous tasks and/or to service multiple kernel system calls
simultaneously.
4.16 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
User Threads
 Thread management done by user-level threads library
 Three primary thread libraries:
 POSIX Pthreads
 Win32 threads
 Java threads
4.17 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Kernel Threads
 Supported by the Kernel
 Examples
 Windows XP/2000
 Solaris
 Linux
 Tru64 UNIX
 Mac OS X
4.18 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Multithreading Models
 Some operating system provide a combined user level thread and Kernel
level thread facility. Solaris is a good example of this combined approach.
In a specific implementation, the user threads must be mapped to kernel
threads, using one of the following strategies.
 Many-to-One
 One-to-One
 Many-to-Many
User Thread – to - Kernel Thread
4.19 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Many-to-One
Many user-level
threads mapped to
single kernel thread
4.20 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Many to one
 The many-to-one model maps many user-level threads
to one kernel thread. Thread management is done by the
thread library in user space, so it is efficient . However,
the entire process will block if a thread makes a blocking
system call. Also, because only one thread can access
the kernel at a time, multiple threads are unable to run in
parallel on multicore systems
 However, very few systems continue to use the model
because of its inability to take advantage of multiple
processing cores.
4.21 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
One-to-One
Each user-level thread maps to kernel thread
 Examples
 Windows NT/XP/2000
 Linux
4.22 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
One-to-one
 The one-to-one model maps each user thread to a
kernel thread. It provides more concurrency than the
many-to-one model by allowing another thread to run
when a thread makes a blocking system call.
 It also allows multiple threads to run in parallel on
multiprocessors.
 The only drawback to this model is that creating a user
thread requires creating the corresponding kernel thread.
Because the overhead of creating kernel threads can
burden the performance of an application, most
implementations of this model restrict the number of
threads supported by the system
4.23 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Many-to-Many Model
Allows many user
level threads to be
mapped to many
kernel threads
Allows the operating
system to create a
sufficient number of
kernel threads
 Example
 Windows NT/2000
with the ThreadFiber
package
4.24 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Thread Libraries
 Thread library provides programmer with API for creating and managing
threads
 Two primary ways of implementing
 Library entirely in user space
 Kernel-level library supported by the OS. Invoking a function in the API
for the library typically results in a system call to the kernel.
 Three main thread libraries in use today:
 POSIX Pthreads
 Win32
 Java
4.25 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Thread Libraries
 Three main thread libraries in use today:
 POSIX Pthreads
 May be provided either as user-level or kernel-level
 A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
 API specifies behavior of the thread library, implementation is up to
development of the library
 Win32
 Kernel-level library on Windows system
 Java
 Java threads are managed by the JVM
 Typically implemented using the threads model provided by
underlying OS
4.26 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
POSIX: Thread Creation
#include <pthread.h>
pthread_create (thread, attr, start_routine, arg)
returns : 0 on success, some error code on failure.
4.27 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
POSIX: Thread Termination
#include <pthread.h>
Void pthread_exit (return_value)
4.28 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Thread Cancellation
Terminating a thread before it has finished
Having made the cancellation request, pthread_cancel() returns
immediately; that is it does not wait for the target thread to
terminate.
So, what happens to the target thread?
What happens and when it happens depends on the thread’s
cancellation state and type.
4.29 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Thread Cancellation
Thread attributes that indicate cancellation state and type
 Two general states
 Thread can not be cancelled.
 PTHREAD_CANCEL_DISABLE
 Thread can be cancelled
 PTHREAD_CANCEL_ENABLE
 Default
4.30 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Thread Cancellation
 When thread is cancelable, there are two general types
 Asynchronous cancellation terminates the target
thread immediately
 PTHREAD_CANCEL_ASYNCHRONOUS
 Deferred cancellation allows the target thread to
periodically check if it should be cancelled
 PTHREAD_CANCEL_DEFERRED
 Cancel when thread reaches ‘cancellation point’
4.31 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition
Semantics of fork() ,exec(), exit()
Does fork() duplicate only the calling thread or all threads?
 If one thread in a program calls fork(), does the new process duplicate all
threads, or is the new process single-threaded? Some UNIX systems have
chosen to have two versions of fork(), one that duplicates all threads and
another that duplicates only the thread that invoked the fork() system call.
 Threads and exec()
 With exec(), the calling program is replaced in memory That is, if a thread
invokes the exec() system call, the program specified in the parameter to
exec() will replace the entire process—including all threads.
 Threads and exit()
If any thread calls exit() or the main thread does a return, ALL threads
immediately vanish. No thread-specific data destructors or cleanup
handlers are executed.

More Related Content

Similar to Threads are using in operating systems.pptx

Similar to Threads are using in operating systems.pptx (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ch4.pptx
ch4.pptxch4.pptx
ch4.pptx
 
ch4.pptx
ch4.pptxch4.pptx
ch4.pptx
 
04 Threads.pptx
04  Threads.pptx04  Threads.pptx
04 Threads.pptx
 
My DIaries
My DIariesMy DIaries
My DIaries
 
Operating Systems - "Chapter 4: Multithreaded Programming"
Operating Systems - "Chapter 4:  Multithreaded Programming"Operating Systems - "Chapter 4:  Multithreaded Programming"
Operating Systems - "Chapter 4: Multithreaded Programming"
 
Ch4 threads
Ch4 threadsCh4 threads
Ch4 threads
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Thread
ThreadThread
Thread
 
Threads
ThreadsThreads
Threads
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
OS memory management
OS memory management OS memory management
OS memory management
 
Ch1 introduction-to-os
Ch1 introduction-to-osCh1 introduction-to-os
Ch1 introduction-to-os
 
J threads-pdf
J threads-pdfJ threads-pdf
J threads-pdf
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Thread (Operating System)
Thread  (Operating System)Thread  (Operating System)
Thread (Operating System)
 
ch22.ppt
ch22.pptch22.ppt
ch22.ppt
 
Architecture of web servers
Architecture of web serversArchitecture of web servers
Architecture of web servers
 
OS Memory Management.pptx
OS Memory Management.pptxOS Memory Management.pptx
OS Memory Management.pptx
 
ch8.ppt
ch8.pptch8.ppt
ch8.ppt
 

Recently uploaded

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
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
🐬 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
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
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
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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
 

Recently uploaded (20)

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
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
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
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 

Threads are using in operating systems.pptx

  • 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition, Chapter 4: Threads Changes by MA Doman 2013
  • 2. 4.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP Threads  Linux Threads
  • 3. 4.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Objectives  To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems  To discuss the APIs for the Pthreads, Win32, and Java thread libraries  To examine issues related to multithreaded programming
  • 4. 4.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Thread Overview  Threads are mechanisms that permit an application to perform multiple tasks concurrently.  A web browser might have one thread display images or text while another thread retrieves data from the network, for example. A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background.
  • 5. 4.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Thread Overview  Threads are mechanisms that permit an application to perform multiple tasks concurrently.  Thread is a basic unit of CPU utilization. A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables.  Thread ID  Program counter  Register set  Stack  A single program can contain multiple threads  Threads share with other threads belonging to the same process  Code, data, open files…….
  • 6. 4.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Single and Multithreaded Processes Traditional (heavyweight) process has a single thread of control heavyweight process lightweight process
  • 7. 4.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Threads in Memory Memory is allocated for a process in segments or parts: 0000000 Increasing virtual address Text ( program code) Initialized data Un-initialized data Heap Stack for main thread argv, environment Stack for thread 2 Stack for thread 3 Stack for thread 1 Main thread executing here Thread 1 executing here Thread 3 executing here Thread 2 executing here
  • 8. 4.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Benefits  Responsiveness Interactive application can delegate background functions to a thread and keep running. Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.  Resource Sharing Several different threads can access the same address space. Threads share the memory and the resources of the process to which they belong by default.  Economy Allocating memory and new processes is costly. Threads are much ‘cheaper’ to initiate. In Solaris, for example, creating a process is about thirty times slower than is creating a thread, and context switching is about five times slower.  Scalability Use threads to take advantage of multiprocessor architecture. where threads may be running in parallel on different processing cores. A single-threaded process can run on only one processor, regardless how many are available
  • 9. 4.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Multithreaded Server Architecture  In certain situations, a single application may be required to perform several similar tasks. For example, a web server accepts client requests for web pages, images, sound, and so forth. A busy web server may have several(perhaps thousands of) clients concurrently accessing it.  Solution?  One solution is to have the server run as a single process that accepts requests. When the server receives a request, it creates a separate process to service that request. In fact, this process-creation method was in common use before threads became popular. Process creation is time consuming and resource intensive, however.  It is generally more efficient to use one process that contains multiple threads. If the web-server process is multithreaded, the server will create a separate thread that listens for client requests. When a request is made, rather than creating another process, the server creates a new thread to service the request and resume listening for additional requests.
  • 10. 4.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Multithreaded Server Architecture thread thread thread thread
  • 11. 4.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Concurrent Execution on a Single-core System Multicore Programming Parallel Execution on a Multicore System
  • 12. 4.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Parallel vs Concurrent  A system is parallel if it can perform more than one task simultaneously.  In contrast, a concurrent system supports more than one task by allowing all the tasks to make progress. Thus, it is possible to have concurrency without parallelism.
  • 13. 4.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Multicore Programming  Multicore systems putting pressure on programmers, challenges include  Dividing activities  What tasks can be separated to run on different processors  Balance  Balance work on all processors  Data splitting  Separate data to run with the tasks  Data dependency  Watch for dependences between tasks  Testing and debugging  Harder!!!!
  • 14. 4.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Multithreading Models  Support provided at either  User level -> user threads Supported above the kernel and managed without kernel support They are created and managed by users. They are used at the application level. There is no involvement of the OS. A good example is when we use threading in programming like in Java, C#, Python, etc, we use user threads.
  • 15. 4.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Multithreading Models  Kernel level -> kernel threads Supported and managed directly by the operating system. All modern OSes support kernel level threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls simultaneously.
  • 16. 4.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition User Threads  Thread management done by user-level threads library  Three primary thread libraries:  POSIX Pthreads  Win32 threads  Java threads
  • 17. 4.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Kernel Threads  Supported by the Kernel  Examples  Windows XP/2000  Solaris  Linux  Tru64 UNIX  Mac OS X
  • 18. 4.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Multithreading Models  Some operating system provide a combined user level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a specific implementation, the user threads must be mapped to kernel threads, using one of the following strategies.  Many-to-One  One-to-One  Many-to-Many User Thread – to - Kernel Thread
  • 19. 4.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Many-to-One Many user-level threads mapped to single kernel thread
  • 20. 4.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Many to one  The many-to-one model maps many user-level threads to one kernel thread. Thread management is done by the thread library in user space, so it is efficient . However, the entire process will block if a thread makes a blocking system call. Also, because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multicore systems  However, very few systems continue to use the model because of its inability to take advantage of multiple processing cores.
  • 21. 4.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition One-to-One Each user-level thread maps to kernel thread  Examples  Windows NT/XP/2000  Linux
  • 22. 4.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition One-to-one  The one-to-one model maps each user thread to a kernel thread. It provides more concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call.  It also allows multiple threads to run in parallel on multiprocessors.  The only drawback to this model is that creating a user thread requires creating the corresponding kernel thread. Because the overhead of creating kernel threads can burden the performance of an application, most implementations of this model restrict the number of threads supported by the system
  • 23. 4.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads  Example  Windows NT/2000 with the ThreadFiber package
  • 24. 4.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Thread Libraries  Thread library provides programmer with API for creating and managing threads  Two primary ways of implementing  Library entirely in user space  Kernel-level library supported by the OS. Invoking a function in the API for the library typically results in a system call to the kernel.  Three main thread libraries in use today:  POSIX Pthreads  Win32  Java
  • 25. 4.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Thread Libraries  Three main thread libraries in use today:  POSIX Pthreads  May be provided either as user-level or kernel-level  A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization  API specifies behavior of the thread library, implementation is up to development of the library  Win32  Kernel-level library on Windows system  Java  Java threads are managed by the JVM  Typically implemented using the threads model provided by underlying OS
  • 26. 4.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition POSIX: Thread Creation #include <pthread.h> pthread_create (thread, attr, start_routine, arg) returns : 0 on success, some error code on failure.
  • 27. 4.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition POSIX: Thread Termination #include <pthread.h> Void pthread_exit (return_value)
  • 28. 4.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Thread Cancellation Terminating a thread before it has finished Having made the cancellation request, pthread_cancel() returns immediately; that is it does not wait for the target thread to terminate. So, what happens to the target thread? What happens and when it happens depends on the thread’s cancellation state and type.
  • 29. 4.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Thread Cancellation Thread attributes that indicate cancellation state and type  Two general states  Thread can not be cancelled.  PTHREAD_CANCEL_DISABLE  Thread can be cancelled  PTHREAD_CANCEL_ENABLE  Default
  • 30. 4.30 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Thread Cancellation  When thread is cancelable, there are two general types  Asynchronous cancellation terminates the target thread immediately  PTHREAD_CANCEL_ASYNCHRONOUS  Deferred cancellation allows the target thread to periodically check if it should be cancelled  PTHREAD_CANCEL_DEFERRED  Cancel when thread reaches ‘cancellation point’
  • 31. 4.31 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8th Edition Semantics of fork() ,exec(), exit() Does fork() duplicate only the calling thread or all threads?  If one thread in a program calls fork(), does the new process duplicate all threads, or is the new process single-threaded? Some UNIX systems have chosen to have two versions of fork(), one that duplicates all threads and another that duplicates only the thread that invoked the fork() system call.  Threads and exec()  With exec(), the calling program is replaced in memory That is, if a thread invokes the exec() system call, the program specified in the parameter to exec() will replace the entire process—including all threads.  Threads and exit() If any thread calls exit() or the main thread does a return, ALL threads immediately vanish. No thread-specific data destructors or cleanup handlers are executed.