Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Chapter 4: Threads
Operating Systems
Lecturer: Dalya Samer
4.2 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Chapter4 Outlines
▪ Introduction
▪ Multicore Programming
▪ Multithreading Models
▪ Benefits of threads
▪ Thread Libraries
4.3 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Introduction (1/6)
▪ A thread is a basic unit of CPU utilization; it comprises
a thread ID, a program counter, a register set, and a stack.
▪ It shares with other threads belonging to the same
process its code section, data section, and other
operating-system resources, such as open files and
signals.
4.4 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Introduction (2/6)
▪ Let's say, for example, a program is not capable of
drawing pictures while reading keystrokes. The program
must give its full attention to the keyboard input lacking
the ability to handle more than one event at a time.
▪ The ideal solution to this problem is the seamless
execution of two or more sections of a program at the
same time. Threads allows us to do this.
4.5 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Introduction (3/6)
4.6 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Introduction (4/6)
▪ Most software applications that run on modern computers are
multithreaded.
• An application typically is implemented as a separate process with
several threads of control.
➢ 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.7 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Introduction (5/6)
•Multithreaded Server Architecture
4.8 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Introduction (6/6)
▪ Finally, most operating-system kernels are now
multithreaded. Several threads operate in the kernel, and
each thread performs a specific task, such as managing
devices, managing memory, or interrupt handling.
4.9 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multicore Programming
▪ Multithreaded programming provides a mechanism for more
efficient use of these multicore or multiprocessor systems.
Concurrent execution on single-core system
Parallelism on a multi-core system
4.10 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (1/6)
▪ Support for threads may be provided either at the user
level, for user threads, or by the kernel, for kernel
threads.
▪ User threads are supported above the kernel and are
managed without kernel support, whereas kernel threads
are supported and managed directly by the operating
system.
4.11 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
User and Kernel Threads
4.12 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (2/6)
▪ Ultimately, a relationship must exist between user threads
and kernel threads.
▪ We look at three common models:
➢ the one-to-one model
➢ the many-to-one model
➢ the many-to-many model.
4.13 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (3/6)
•One-to-One model (1/3)
4.14 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (3/6)
•One-to-One model (2/3)
➢Each user-level thread maps to kernel thread
➢Creating a user-level thread creates a kernel thread
➢More concurrency than many-to-one
➢Examples
▪Windows
▪Linux
4.15 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (3/6)
•One-to-One model (3/3)
➢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. Linux, along with the family of Windows operating
systems, implement the one-to-one model.
4.16 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (4/6)
•Many-to-One model
4.17 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (5/6)
•Many-to-Many Model (1/2)
4.18 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (5/6)
•Many-to-Many Model (2/2)
➢Allows many user level threads to be mapped to many
kernel threads.
➢ Allows the operating system to create a sufficient
number of kernel threads.
➢Allows the developer to create as many user threads
as she wishes, it does not result in true concurrency,
because the kernel can schedule only one thread at a
time.
4.19 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Multithreading Models (6/6)
▪ Similar to Many to Many, except that it allows a user thread to
be bound to kernel thread
•Two-level Model
4.20 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Benefits of threads
▪ Responsiveness – may allow continued execution if part of
process is blocked, especially important for user interfaces.
▪ Resource Sharing – threads share resources of process,
easier than shared memory or message passing.
▪ Economy – cheaper than process creation, thread
switching lower overhead than context switching.
▪ Scalability – process can take advantage of multicore
architectures.
4.21 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Thread Libraries
▪ Thread library provides programmer with API for
creating and managing threads.
▪ User Threads - management done by user-level threads
library
▪ Three primary thread libraries are in use today:
• POSIX Pthreads
• Windows threads
• Java threads
4.22 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Pthreads Example
4.23 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Pthreads Example (Cont.)
4.24 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Windows Multithreaded C Program
4.25 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
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
• Standard practice is to implement Runnable interface
4.26 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
Java Threads
Implementing Runnable interface:
Creating a thread:
Waiting on a thread:
Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition
End of Chapter 4

Introduction to Threads, User and Kernel threads .pdf

  • 1.
    Silberschatz, Galvin andGagne ©2018 Operating System Concepts – 10th Edition Chapter 4: Threads Operating Systems Lecturer: Dalya Samer
  • 2.
    4.2 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Chapter4 Outlines ▪ Introduction ▪ Multicore Programming ▪ Multithreading Models ▪ Benefits of threads ▪ Thread Libraries
  • 3.
    4.3 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Introduction (1/6) ▪ A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. ▪ It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files and signals.
  • 4.
    4.4 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Introduction (2/6) ▪ Let's say, for example, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. ▪ The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this.
  • 5.
    4.5 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Introduction (3/6)
  • 6.
    4.6 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Introduction (4/6) ▪ Most software applications that run on modern computers are multithreaded. • An application typically is implemented as a separate process with several threads of control. ➢ 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.
  • 7.
    4.7 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Introduction (5/6) •Multithreaded Server Architecture
  • 8.
    4.8 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Introduction (6/6) ▪ Finally, most operating-system kernels are now multithreaded. Several threads operate in the kernel, and each thread performs a specific task, such as managing devices, managing memory, or interrupt handling.
  • 9.
    4.9 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multicore Programming ▪ Multithreaded programming provides a mechanism for more efficient use of these multicore or multiprocessor systems. Concurrent execution on single-core system Parallelism on a multi-core system
  • 10.
    4.10 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (1/6) ▪ Support for threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads. ▪ User threads are supported above the kernel and are managed without kernel support, whereas kernel threads are supported and managed directly by the operating system.
  • 11.
    4.11 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition User and Kernel Threads
  • 12.
    4.12 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (2/6) ▪ Ultimately, a relationship must exist between user threads and kernel threads. ▪ We look at three common models: ➢ the one-to-one model ➢ the many-to-one model ➢ the many-to-many model.
  • 13.
    4.13 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (3/6) •One-to-One model (1/3)
  • 14.
    4.14 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (3/6) •One-to-One model (2/3) ➢Each user-level thread maps to kernel thread ➢Creating a user-level thread creates a kernel thread ➢More concurrency than many-to-one ➢Examples ▪Windows ▪Linux
  • 15.
    4.15 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (3/6) •One-to-One model (3/3) ➢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. Linux, along with the family of Windows operating systems, implement the one-to-one model.
  • 16.
    4.16 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (4/6) •Many-to-One model
  • 17.
    4.17 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (5/6) •Many-to-Many Model (1/2)
  • 18.
    4.18 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (5/6) •Many-to-Many Model (2/2) ➢Allows many user level threads to be mapped to many kernel threads. ➢ Allows the operating system to create a sufficient number of kernel threads. ➢Allows the developer to create as many user threads as she wishes, it does not result in true concurrency, because the kernel can schedule only one thread at a time.
  • 19.
    4.19 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Multithreading Models (6/6) ▪ Similar to Many to Many, except that it allows a user thread to be bound to kernel thread •Two-level Model
  • 20.
    4.20 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Benefits of threads ▪ Responsiveness – may allow continued execution if part of process is blocked, especially important for user interfaces. ▪ Resource Sharing – threads share resources of process, easier than shared memory or message passing. ▪ Economy – cheaper than process creation, thread switching lower overhead than context switching. ▪ Scalability – process can take advantage of multicore architectures.
  • 21.
    4.21 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Thread Libraries ▪ Thread library provides programmer with API for creating and managing threads. ▪ User Threads - management done by user-level threads library ▪ Three primary thread libraries are in use today: • POSIX Pthreads • Windows threads • Java threads
  • 22.
    4.22 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Pthreads Example
  • 23.
    4.23 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Pthreads Example (Cont.)
  • 24.
    4.24 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Windows Multithreaded C Program
  • 25.
    4.25 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition 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 • Standard practice is to implement Runnable interface
  • 26.
    4.26 Silberschatz, Galvinand Gagne ©2018 Operating System Concepts – 10th Edition Java Threads Implementing Runnable interface: Creating a thread: Waiting on a thread:
  • 27.
    Silberschatz, Galvin andGagne ©2018 Operating System Concepts – 10th Edition End of Chapter 4