SlideShare a Scribd company logo
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

PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)Ralf Eggert
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsPaul Groth
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform EngineeringJemma Hussein Allen
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupCatarinaPereira64715
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsVlad Stirbu
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Alison B. Lowndes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...Sri Ambati
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 

Recently uploaded (20)

PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 

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.