0% found this document useful (0 votes)
4 views37 pages

CH 4

Uploaded by

MN Monjurul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views37 pages

CH 4

Uploaded by

MN Monjurul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter 4: Threads &

Concurrency

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline

▪ Overview
▪ Multicore Programming
▪ Multithreading Models
▪ Thread Libraries
▪ Implicit Threading
▪ Threading Issues
▪ Operating System Examples

Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
Objectives

▪ Identify the basic components of a thread, and contrast threads and


processes
▪ Describe the benefits and challenges of designing multithreaded
applications
▪ Illustrate different approaches to implicit threading including thread
pools, fork-join, and Grand Central Dispatch
▪ Describe how the Windows and Linux operating systems represent
threads
▪ Designing multithreaded applications using the Pthreads, Java, and
Windows threading APIs

Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Thread

▪ A thread is a basic unit of CPU utilization; it comprises a thread ID, a


program counter (PC), 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.

▪ A traditional process has a single thread of control. If a process has


multiple threads of control, it can perform more than one task at a
time.

Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Motivation

▪ Most modern applications are multithreaded


▪ Threads run within application
▪ An application typically is implemented as a separate process with
several threads of control. Examples:
• An application that creates photo thumbnails from a collection of
images may use a separate thread to generate a thumbnail from
each separate image.
• A web browser might have one thread display images or text while
another thread retrieves data from the network.
• 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.
▪ Process creation is heavy-weight while thread creation is light-weight
▪ Can simplify code, increase efficiency
▪ Kernels are generally multithreaded

Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded Processes

Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Multithreaded Server Architecture

Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Benefits of Multithreaded
Programming
▪ Responsiveness – may allow continued execution if part of process is
blocked, especially important for user interfaces. Ex: when a user clicks a
button, a single-threaded application would be unresponsive to the user
until the operation had been completed. In contrast, if the time-
consuming operation is performed in a separate, asynchronous thread,
the application remains responsive to the user.
▪ Resource Sharing – threads share resources of process, easier than
shared memory or message passing. The benefit of sharing code and
data is that it allows an application to have several different threads of
activity within the same address space.
▪ Economy – cheaper than process creation, thread switching lower
overhead than context switching. Context switching is typically faster
between threads than between processes.
▪ Scalability – process can take advantage of multicore architectures. A
single-threaded process can run on only one processor, regardless how
many are available.

Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
▪ Multicore or multiprocessor systems puts pressure on
programmers, challenges include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
▪ Parallelism implies a system can perform more than one task
simultaneously
▪ Concurrency supports more than one task making progress
• Single processor / core, scheduler providing concurrency

Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
▪ Concurrent execution on single-core system:

▪ Parallelism on a multi-core system:

Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
▪ Types of parallelism
• Data parallelism – distributes subsets of the same data across multiple
cores, same operation on each. For example, summing the contents of an
array of size N. On a single-core system, one thread would simply sum the
elements [0] . . . [N − 1]. On a dual-core system, however, thread A, running
on core 0, could sum the elements [0] . . . [N∕2 − 1] while thread B, running
on core 1, could sum the elements [N∕2] . . . [N − 1]. The two threads would
be running in parallel on separate computing cores.
• Task parallelism – distributing threads across cores, each thread
performing unique operation. In contrast to that situation, an example of
task parallelism might involve two threads, each performing a unique
statistical operation on the array of elements. The threads again are
operating in parallel on separate computing cores, but each is performing a
unique operation.
• Data parallelism involves the distribution of data across multiple cores, and
task parallelism involves the distribution of tasks across multiple cores.

Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018
Data and Task Parallelism

Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
User Threads and Kernel Threads

▪ User threads - management done by user-level threads library


▪ Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
▪ Kernel threads - Supported by the Kernel
▪ Examples – virtually all general-purpose operating systems, including:
• Windows
• Linux
• Mac OS X
• iOS
• Android

Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads

Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
Multithreading Models

▪ Many-to-One
▪ One-to-One
▪ Many-to-Many

Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
Many-to-One

▪ Many user-level threads mapped to single kernel thread


▪ One thread blocking causes all to block
▪ Multiple threads may not run in parallel on multicore system because
only one may be in kernel at a time
▪ Few systems currently use this model
▪ Examples:
• Solaris Green Threads
• GNU Portable Threads

Operating System Concepts – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018
One-to-One
▪ Each user-level thread maps to 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, and a large
number of kernel threads may burden the performance of a system.
▪ Examples
• Windows
• Linux

Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018
Many-to-Many Model
▪ Allows many user level threads to be mapped to many kernel threads
▪ The many-to-many model (Figure 4.9) multiplexes many user-level
threads to a smaller or equal number of kernel threads.
▪ The number of kernel threads may be specific to either a particular
application or a particular machine (an application may be allocated
more kernel threads on a system with eight processing cores than a
system with four cores).
▪ Windows with the ThreadFiber package
▪ Otherwise not very common

Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Two-level Model
▪ Similar to M:M, except that it allows a user thread to be bound to
kernel thread

Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
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

Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
Pthreads

▪ May be provided either as user-level or kernel-level


▪ A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
▪ Specification, not implementation
▪ API specifies behavior of the thread library, implementation is up to
development of the library
▪ Common in UNIX operating systems (Linux & Mac OS X)

Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018
Pthreads Example

Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018
Pthreads Example (Cont.)

Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018
Pthreads Code for Joining 10 Threads

Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program

Operating System Concepts – 10th Edition 4.25 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program (Cont.)

Operating System Concepts – 10th Edition 4.26 Silberschatz, Galvin and Gagne ©2018
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

Operating System Concepts – 10th Edition 4.27 Silberschatz, Galvin and Gagne ©2018
Java Threads
Implementing Runnable interface:

Creating a thread:

Waiting on a thread:

Operating System Concepts – 10th Edition 4.28 Silberschatz, Galvin and Gagne ©2018
Java Executor Framework

▪ Rather than explicitly creating threads, Java also allows thread


creation around the Executor interface:

▪ The Executor is used as follows:

Operating System Concepts – 10th Edition 4.29 Silberschatz, Galvin and Gagne ©2018
Java Executor Framework

Operating System Concepts – 10th Edition 4.30 Silberschatz, Galvin and Gagne ©2018
Java Executor Framework (Cont.)

Operating System Concepts – 10th Edition 4.31 Silberschatz, Galvin and Gagne ©2018
Implicit Threading

▪ Growing in popularity as numbers of threads increase, program


correctness more difficult with explicit threads
▪ Creation and management of threads done by compilers and run-time
libraries rather than programmers
▪ Five methods explored
• Thread Pools
• Fork-Join
• OpenMP
• Grand Central Dispatch
• Intel Threading Building Blocks

Operating System Concepts – 10th Edition 4.32 Silberschatz, Galvin and Gagne ©2018
Thread Pools
▪ Create a number of threads in a pool where they await work
▪ 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
• Separating task to be performed from mechanics of creating task
allows different strategies for running task
4 i.e,Tasks could be scheduled to run periodically
▪ Windows API supports thread pools:

Operating System Concepts – 10th Edition 4.33 Silberschatz, Galvin and Gagne ©2018
Java Thread Pools

▪ Three factory methods for creating thread pools in Executors class:

Operating System Concepts – 10th Edition 4.34 Silberschatz, Galvin and Gagne ©2018
Java Thread Pools (Cont.)

Operating System Concepts – 10th Edition 4.35 Silberschatz, Galvin and Gagne ©2018
Assignment (Pair Group)
▪ Implement a simple client-server chat application using sockets.
Objective: Implement a simple client-server chat using sockets.
Requirements:
• Server can handle at least 2–3 clients (multi-threaded or select-based).
• Messages sent by one client are broadcast to all others.
• Server logs all messages with timestamps.
OR
▪ Create a client-server program to send a file over TCP sockets.
Objective: Create a client-server program to send a file over TCP sockets.
Requirements:
• Client selects a file to send.
• Server saves it with the same name.
• Show progress (percentage sent).
• Handle large files by sending in chunks.
AND
▪ Submit a program to add 10 inputs from user but in different threads.

Operating System Concepts – 10th Edition 4.36 Silberschatz, Galvin and Gagne ©2018
End of Chapter 4

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

You might also like