SlideShare a Scribd company logo
Threads in Operating
systems
Threads
 Overview
 Multithreading Models
 Thread Libraries
 Threading Issues
 Operating System Examples
 Windows XP Threads
 Linux Threads
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
Single and Multithreaded Processes
Benefits
 Responsiveness
 Resource Sharing
 Economy
 Scalability
Multicore Programming
 Multicore CPU appears as multiple CPUs to OS
 Multithreading makes more efficient use of it
Multicore Programming
 Multicore systems putting
pressure on programmers,
challenges include
 Dividing activities
 Balance
 Data splitting
 Data dependency
 Testing and debugging
Types of Threads
 User threads
 Running user-level code
 Managed by user process
 Needs kernel thread to run system calls, I/O operations, etc.
 Can only be scheduled on CPU through kernel thread
 Kernel threads
 Running kernel-level code
 Managed by kernel
 Can be scheduled on CPU
 A user process needs both user threads and kernel threads to
run
Multithreading Models
 User Threads
 Thread management done by user-level threads library
 Three primary thread libraries:
 POSIX Pthreads
 Win32 threads
 Java threads
 Kernel Threads
 Supported by the Kernel
 Present in practically all OS
 Windows XP/2000
 Solaris
 Linux
 Tru64 UNIX
 Mac OS X
 Relationship between user and kernel threads
 Many-to-One
 One-to-One
 Many-to-Many
Many-to-One
 Many user-level threads mapped to single kernel
thread
 Examples:
 Solaris Green Threads
 GNU Portable Threads
Many-to-One Model
One-to-One
 Each user-level thread maps to kernel thread
 Examples
 Windows NT/XP/2000
 Linux
 Solaris 9 and later
One-to-one Model
Many-to-Many Model
 Allows many user level threads to be mapped to fewer or
equal kernel threads
 Number of kernel threads can vary per system, and per
process
 Allows the operating system to create a sufficient
number of kernel threads
 Windows NT/2000 with the ThreadFiber package
Many-to-Many Model
Two-level Model
 Variation of Many-to-Many, except that it allows a
user thread to be bound to kernel thread
 Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier
Two-level Model
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
 We’ll study three implementations
 POSIX Pthread
 Win32
 Java
POSIX Pthreads
 POSIX standard (IEEE 1003.1c) defines specifications for an
API for thread creation and synchronization
 API specifies behavior of the thread library, implementation
is up to development of the library
 May be provided either as user-level or kernel-level
 Common in UNIX operating systems (Solaris, Linux, Mac OS
X)
 Implemented in C in pthread.h
 pthread_t
 pthread_attr_t
 pthread_create()
 pthread_join()
 pthread_exit()
Win32 Threads
 Very similar to Pthreads
 Function prototypes in windows.h
 CreateThread()
 WaitForSingleObject()
 CloseHandle()
 Implementation in proprietary Microsoft code inside
Windows kernel
Java Threads
 Java threads are managed by the JVM
 Typically implemented using the threads model provided by underlying
OS
 Java threads may be created by:
 Extending Thread class
 Implementing the Runnable interface
 run() function is thread’s executable code
 Thread start() function creates thread, calls run()
 Thread exits automatically at the end of run()
public interface Runnable
{ public abstract void run(); }
Implicit Threading
 Instead of giving developer control through thread
libraries, have threads handled by compilers and run-time
libraries
 OpenMP
 Compiler directives for C/C++
 Programmer inserts preprocessor tags
 Compiler isolates parallelizable sections and creates as many
threads as there are CPUs
 Grand Central Dispatch
 Apple-specific
 Programmer inserts tags in code (like OpenMP)
 GCD manages POSIX threads, assigns work in three FIFO
priority queues
Threading Issues
 Semantics of fork() and exec() system calls
 Thread cancellation of target thread
 Asynchronous or deferred
 Signal handling
 Thread pools
 Thread-specific data
 Scheduler activations
Semantics of fork() and
exec()
 Recall: fork() and exec() functions in Unix handle new
processes
 Process creation, replace process code
 When a process has several threads, and one thread
calls these functions, does it affect all threads or just
the calling one?
Thread Cancellation
 Terminating a target thread before it has finished
 Two general approaches:
 Asynchronous cancellation terminates the target thread
immediately from another thread
 Deferred cancellation allows the target thread to
periodically check if it should cancel itself
Signal Handling
 Signals are used to notify a process that a particular event has
occurred
1. Signal is generated by particular event (typically interrupts)
2. Signal is delivered to a process
3. Signal is handled
 A signal handler is used to process signals
 Default handlers in kernel can be overwritten by user-defined
handler
 How to deliver a process-specific signal in a multithreaded
process?
 Deliver the signal to the thread to which the signal applies
 Deliver the signal to every thread in the process
 Deliver the signal to certain threads in the process
 Assign a specific thread to receive all signals for the process
Thread Pools
 So far, we’re creating threads when needed
 Thread creation/termination overhead
 With no maximum thread number, more and more threads can be
created until they exhaust system resources
 Create a number of threads at process creation in a thread
pool, where they await work and return after work is done
 Advantages:
 Usually slightly faster to service a request with an existing thread
than create a new thread
 Allows the number of threads in the application(s) to be bound to
the size of the pool
 Part of the Win32 API
 QueueUserWorkItem()
Thread Specific Data
 One advantage of threads is no duplication of common
process data
 But some threads need thread-specific data
 Data unique to that thread
 Data that did not exist at process creation time
 We need support for thread-specific data
 Included in Pthreads, Win32 and Java
Scheduler Activations
 Many-to-Many and Two-Level models have a set of kernel threads
and user threads
 Need to keep a balance in the number of each – for best
performance, should be adjusted dynamically
 Require communication between kernel and thread library
 Systems with these models often put a light-weight process
between kernel thread and user thread, to work as a virtual CPU
for the user
 This allows scheduler activation kernel-process communication
scheme
 Kernel provides a set of LWP to process
 Process schedules its own user threads on LWP
 When a thread waits for an event, the kernel warns the process with
an upcall and allocates a new LWP to process
 Process runs upcall handler on new LWP to save thread state, lose
thread’s LWP, and keeps new LWP to schedule other threads
Operating System Examples
 Windows XP Threads
 Linux Thread
Windows XP Threads
 Implements the one-to-one mapping, kernel-level
 Each thread contains
 A thread id
 Register set
 Separate user and kernel stacks, for when running in user or
kernel mode
 Private data storage area
 The register set, stacks, and private storage area are
known as the context of the threads
 The primary data structures of a thread include:
 ETHREAD (origin & parent)
 KTHREAD (kernel thread information)
 TEB (user thread information)
Windows XP Threads
Linux Threads
 Linux does not distinguish between processes and threads, calls them
all tasks
 Task creation is done through clone() system call, with a set of flags
 Each task is represented by a kernel structure task_struct, which
only contains pointers to other data structures instead of storing task
info
 Makes varying levels of sharing between tasks possible

More Related Content

Similar to Threads in Operating systems and concepts (20)

PPTX
OS Module-2.pptx
bleh23
 
PPTX
Topic 4- processes.pptx
DanishMahmood23
 
PPT
Operating Systems - "Chapter 4: Multithreaded Programming"
Ra'Fat Al-Msie'deen
 
PDF
Sucet os module_2_notes
SRINIVASUNIVERSITYEN
 
PDF
Multithreaded Programming in oprating system
YOGENDRAMS
 
PPT
Ch04 threads
Nazir Ahmed
 
PDF
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
abhinandpk2405
 
PPTX
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 
PDF
4 threads
Dr. Loganathan R
 
PPT
Intro To .Net Threads
rchakra
 
PDF
Threads operating system slides easy understand
shamsulhuda34
 
PPT
Chapter 6 os
AbDul ThaYyal
 
PDF
The Thread Chapter 4 of Operating System
mohammadhaidarayoobi
 
PPTX
Threads
Dr. SURBHI SAROHA
 
PPT
Treads
nayanashetty7
 
PPT
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
 
PPT
multi-threading
Ezzat Gul
 
PPTX
Chapter04 new
vmummaneni
 
PPT
OSCh5
Joe Christensen
 
OS Module-2.pptx
bleh23
 
Topic 4- processes.pptx
DanishMahmood23
 
Operating Systems - "Chapter 4: Multithreaded Programming"
Ra'Fat Al-Msie'deen
 
Sucet os module_2_notes
SRINIVASUNIVERSITYEN
 
Multithreaded Programming in oprating system
YOGENDRAMS
 
Ch04 threads
Nazir Ahmed
 
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
abhinandpk2405
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 
4 threads
Dr. Loganathan R
 
Intro To .Net Threads
rchakra
 
Threads operating system slides easy understand
shamsulhuda34
 
Chapter 6 os
AbDul ThaYyal
 
The Thread Chapter 4 of Operating System
mohammadhaidarayoobi
 
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
 
multi-threading
Ezzat Gul
 
Chapter04 new
vmummaneni
 

Recently uploaded (20)

PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
community health nursing question paper 2.pdf
Prince kumar
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Ad

Threads in Operating systems and concepts

  • 2. Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP Threads  Linux Threads
  • 3. 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
  • 5. Benefits  Responsiveness  Resource Sharing  Economy  Scalability
  • 6. Multicore Programming  Multicore CPU appears as multiple CPUs to OS  Multithreading makes more efficient use of it
  • 7. Multicore Programming  Multicore systems putting pressure on programmers, challenges include  Dividing activities  Balance  Data splitting  Data dependency  Testing and debugging
  • 8. Types of Threads  User threads  Running user-level code  Managed by user process  Needs kernel thread to run system calls, I/O operations, etc.  Can only be scheduled on CPU through kernel thread  Kernel threads  Running kernel-level code  Managed by kernel  Can be scheduled on CPU  A user process needs both user threads and kernel threads to run
  • 9. Multithreading Models  User Threads  Thread management done by user-level threads library  Three primary thread libraries:  POSIX Pthreads  Win32 threads  Java threads  Kernel Threads  Supported by the Kernel  Present in practically all OS  Windows XP/2000  Solaris  Linux  Tru64 UNIX  Mac OS X  Relationship between user and kernel threads  Many-to-One  One-to-One  Many-to-Many
  • 10. Many-to-One  Many user-level threads mapped to single kernel thread  Examples:  Solaris Green Threads  GNU Portable Threads
  • 12. One-to-One  Each user-level thread maps to kernel thread  Examples  Windows NT/XP/2000  Linux  Solaris 9 and later
  • 14. Many-to-Many Model  Allows many user level threads to be mapped to fewer or equal kernel threads  Number of kernel threads can vary per system, and per process  Allows the operating system to create a sufficient number of kernel threads  Windows NT/2000 with the ThreadFiber package
  • 16. Two-level Model  Variation of Many-to-Many, except that it allows a user thread to be bound to kernel thread  Examples  IRIX  HP-UX  Tru64 UNIX  Solaris 8 and earlier
  • 18. 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  We’ll study three implementations  POSIX Pthread  Win32  Java
  • 19. POSIX Pthreads  POSIX standard (IEEE 1003.1c) defines specifications for an API for thread creation and synchronization  API specifies behavior of the thread library, implementation is up to development of the library  May be provided either as user-level or kernel-level  Common in UNIX operating systems (Solaris, Linux, Mac OS X)  Implemented in C in pthread.h  pthread_t  pthread_attr_t  pthread_create()  pthread_join()  pthread_exit()
  • 20. Win32 Threads  Very similar to Pthreads  Function prototypes in windows.h  CreateThread()  WaitForSingleObject()  CloseHandle()  Implementation in proprietary Microsoft code inside Windows kernel
  • 21. Java Threads  Java threads are managed by the JVM  Typically implemented using the threads model provided by underlying OS  Java threads may be created by:  Extending Thread class  Implementing the Runnable interface  run() function is thread’s executable code  Thread start() function creates thread, calls run()  Thread exits automatically at the end of run() public interface Runnable { public abstract void run(); }
  • 22. Implicit Threading  Instead of giving developer control through thread libraries, have threads handled by compilers and run-time libraries  OpenMP  Compiler directives for C/C++  Programmer inserts preprocessor tags  Compiler isolates parallelizable sections and creates as many threads as there are CPUs  Grand Central Dispatch  Apple-specific  Programmer inserts tags in code (like OpenMP)  GCD manages POSIX threads, assigns work in three FIFO priority queues
  • 23. Threading Issues  Semantics of fork() and exec() system calls  Thread cancellation of target thread  Asynchronous or deferred  Signal handling  Thread pools  Thread-specific data  Scheduler activations
  • 24. Semantics of fork() and exec()  Recall: fork() and exec() functions in Unix handle new processes  Process creation, replace process code  When a process has several threads, and one thread calls these functions, does it affect all threads or just the calling one?
  • 25. Thread Cancellation  Terminating a target thread before it has finished  Two general approaches:  Asynchronous cancellation terminates the target thread immediately from another thread  Deferred cancellation allows the target thread to periodically check if it should cancel itself
  • 26. Signal Handling  Signals are used to notify a process that a particular event has occurred 1. Signal is generated by particular event (typically interrupts) 2. Signal is delivered to a process 3. Signal is handled  A signal handler is used to process signals  Default handlers in kernel can be overwritten by user-defined handler  How to deliver a process-specific signal in a multithreaded process?  Deliver the signal to the thread to which the signal applies  Deliver the signal to every thread in the process  Deliver the signal to certain threads in the process  Assign a specific thread to receive all signals for the process
  • 27. Thread Pools  So far, we’re creating threads when needed  Thread creation/termination overhead  With no maximum thread number, more and more threads can be created until they exhaust system resources  Create a number of threads at process creation in a thread pool, where they await work and return after work is done  Advantages:  Usually slightly faster to service a request with an existing thread than create a new thread  Allows the number of threads in the application(s) to be bound to the size of the pool  Part of the Win32 API  QueueUserWorkItem()
  • 28. Thread Specific Data  One advantage of threads is no duplication of common process data  But some threads need thread-specific data  Data unique to that thread  Data that did not exist at process creation time  We need support for thread-specific data  Included in Pthreads, Win32 and Java
  • 29. Scheduler Activations  Many-to-Many and Two-Level models have a set of kernel threads and user threads  Need to keep a balance in the number of each – for best performance, should be adjusted dynamically  Require communication between kernel and thread library  Systems with these models often put a light-weight process between kernel thread and user thread, to work as a virtual CPU for the user  This allows scheduler activation kernel-process communication scheme  Kernel provides a set of LWP to process  Process schedules its own user threads on LWP  When a thread waits for an event, the kernel warns the process with an upcall and allocates a new LWP to process  Process runs upcall handler on new LWP to save thread state, lose thread’s LWP, and keeps new LWP to schedule other threads
  • 30. Operating System Examples  Windows XP Threads  Linux Thread
  • 31. Windows XP Threads  Implements the one-to-one mapping, kernel-level  Each thread contains  A thread id  Register set  Separate user and kernel stacks, for when running in user or kernel mode  Private data storage area  The register set, stacks, and private storage area are known as the context of the threads  The primary data structures of a thread include:  ETHREAD (origin & parent)  KTHREAD (kernel thread information)  TEB (user thread information)
  • 33. Linux Threads  Linux does not distinguish between processes and threads, calls them all tasks  Task creation is done through clone() system call, with a set of flags  Each task is represented by a kernel structure task_struct, which only contains pointers to other data structures instead of storing task info  Makes varying levels of sharing between tasks possible